2009-08-04 13 views
23

मैं div एनवी को विस्तारित करने के लिए कैसे प्राप्त कर सकता हूं या ऊंचाई को अपने मूल div (कंटेनर) के समान कैसे प्राप्त कर सकता हूं?कंटेनर फिट करने के लिए अपनी ऊंचाई का आकार बदलने के लिए div कैसे प्राप्त करें?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<meta http-equiv="Content-Style-Type" content="text/css" /> 
<meta http-equiv="Content-Script-Type" content="text/javascript" /> 
<title></title> 
<style type="text/css"> 
* {border:0; padding:0; margin:0;}/* Set everything to "zero" */ 
#container { 
    margin-left: auto; 
    margin-right: auto; 
    border: 1px solid black; 
    overflow: auto; 
    width: 800px; 
} 
#nav { 
    width: 19%; 
    border: 1px solid green; 
    float:left; 
} 
#content { 
    width: 79%; 
    border: 1px solid red; 
    float:right; 
} 
</style> 


<div id=container> 
    <div id=nav> 
     <ul> 
      <li>Menu</li> 
      <li>Menu</li> 
      <li>Menu</li> 
      <li>Menu</li> 
      <li>Menu</li> 
     </ul> 
    </div> 
    <div id=content> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam fermentum consequat ligula vitae posuere. Mauris dolor quam, consequat vel condimentum eget, aliquet sit amet sem. Nulla in lectus ac felis ultrices dignissim quis ac orci. Nam non tellus eget metus sollicitudin venenatis sit amet at dui. Quisque malesuada feugiat tellus, at semper eros mollis sed. In luctus tellus in magna condimentum sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur vel dui est. Aliquam vitae condimentum dui. Praesent vel mi at odio blandit pellentesque. Proin felis massa, vestibulum a hendrerit ut, imperdiet in nulla. Sed aliquam, dolor id congue porttitor, mauris turpis congue felis, vel luctus ligula libero in arcu. Pellentesque egestas blandit turpis ac aliquet. Sed sit amet orci non turpis feugiat euismod. In elementum tristique tortor ac semper.</p> 
    </div> 
</div> 

</body> 
</html> 

उत्तर

26

सरल तरीका

आप 0 और position: absolute को एनएवी के दोनों top और bottom गुण स्थापित करने के साथ इस लक्ष्य को हासिल कर सकते हैं नहीं होगा। कंटेनर को position: relative पर सेट करें।

See how this is done

है कि लेख के नीचे, वहाँ डीन एडवर्ड्स IE7 (और IE8) js लाइब्रेरी का लिंक है कि आप IE6 आगंतुकों के लिए शामिल होना चाहिए है। यह एक जेएस लाइब्रेरी है जो वास्तव में IE6 (या 8) की तरह व्यवहार करती है जब आप इसे शामिल करते हैं। मिठाई! ... निश्चित रूप से इसके साथ कीड़े हैं, लेकिन कम से कम आप जानते हैं कि उनके साथ कैसे निपटें।

Dean Edwards' IE7 and 8 JS libraries

आधुनिक तरीका (flexbox)

IE11 + और सभी आधुनिक ब्राउज़रों flexbox समर्थन करते हैं।

.container { 
    display: flex; 
    flex-direction: column; 
} 

.child { 
    flex-grow: 1; 
} 
+3

आपको नौसेना पर ही 'स्थिति: पूर्ण' सेट करना होगा। इस लेख में उल्लेख किया गया है, लेकिन मैंने सोचा कि इसे यहां उल्लेख करना अच्छा होगा। – Muhd

+0

@Muhd अद्यतन उत्तर – kmiyashiro

0

आप शायद निम्नलिखित घोषणा का उपयोग करना चाहते करने जा रहे हैं:

height: 100%;

यह जो इसे माता पिता div भरने कर देगा, उसके कंटेनर ऊंचाई के 100% के div की ऊंचाई सेट हो जाएगा ।

4

आपको कंटेनर की ऊंचाई निर्दिष्ट करने की आवश्यकता होगी और फिर नौसेना की ऊंचाई 100% पर सेट करनी होगी।

संपादित करें: एक त्वरित रूप से देखा गया था और ऐसा लगता है कि ऊंचाई संपत्ति माता-पिता की ऊंचाई पर लागू होती है, इसलिए आपको वास्तव में कंटेनर की ऊंचाई निर्धारित करने की आवश्यकता होगी।

एनबी: शरीर तत्व की ऊंचाई 100% तक सेट करना केवल ब्राउज़र विंडो की ऊंचाई पर स्केल करता है। कोई भी सामग्री जो एक पृष्ठ से अधिक goeds एक ही पृष्ठभूमि आदि

+0

यह काम करता है, लेकिन मैं कंटेनर के लिए निश्चित ऊंचाई नहीं चाहता था। ; | – ParoX

+0

एनबी के संबंध में: न्यूनतम ऊंचाई नहीं: 100%; इस – ParoX

+0

@ ब्रायन की मदद करें, इसे आजमाएं। – schubySteve

2

दुर्भाग्यवश, इसे प्राप्त करने का कोई मूर्ख-प्रमाण तरीका नहीं है। एक ब्लॉक केवल तभी अपने कंटेनर की ऊंचाई तक फैल जाएगा यदि यह तैरता नहीं है। फ़्लोट किए गए ब्लॉक दस्तावेज़ प्रवाह के बाहर माना जाता है।

जावास्क्रिप्ट का उपयोग किये बिना निम्नलिखित करने का एक तरीका फॉक्स-कॉलम नामक तकनीक के माध्यम से है।

मूल रूप से फ्लोट किए गए तत्वों के मूल तत्वों के लिए background-image लागू करना शामिल है जो आपको विश्वास दिलाता है कि दोनों तत्व एक ही ऊंचाई हैं।

अधिक जानकारी यहां उपलब्ध है:

A List Apart: Articles: Faux Columns

+0

यह मेरे पास है, मैं इसे दूर करने की कोशिश कर रहा हूं। – ParoX

1

एक और एक सरल विधि है। आपको सीएसएस में अधिक कोड करने की आवश्यकता नहीं है।बस जावा स्क्रिप्ट समेत और स्क्रिप्ट के अंदर div "id" दर्ज करने से आप कॉलम की समान ऊंचाई प्राप्त कर सकते हैं ताकि आप कंटेनर के लिए ऊंचाई फिट कर सकें। यह प्रमुख ब्राउज़रों में काम करता है।

स्रोत कोड:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<meta http-equiv="Content-Style-Type" content="text/css" /> 
<meta http-equiv="Content-Script-Type" content="text/javascript" /> 
<title></title> 
<style type="text/css"> 
* {border:0; padding:0; margin:0;}/* Set everything to "zero" */ 
#container { 
    margin-left: auto; 
    margin-right: auto; 
    border: 1px solid black; 
    overflow: auto; 
    width: 800px;  
} 
#nav { 
    width: 19%; 
    border: 1px solid green; 
    float:left; 
} 
#content { 
    width: 79%; 
    border: 1px solid red; 
    float:right; 
} 
</style> 

<script language="javascript"> 
var ddequalcolumns=new Object() 
//Input IDs (id attr) of columns to equalize. Script will check if each corresponding column actually exists: 
ddequalcolumns.columnswatch=["nav", "content"] 

ddequalcolumns.setHeights=function(reset){ 
var tallest=0 
var resetit=(typeof reset=="string")? true : false 
for (var i=0; i<this.columnswatch.length; i++){ 
if (document.getElementById(this.columnswatch[i])!=null){ 
if (resetit) 
document.getElementById(this.columnswatch[i]).style.height="auto" 
if (document.getElementById(this.columnswatch[i]).offsetHeight>tallest) 
tallest=document.getElementById(this.columnswatch[i]).offsetHeight 
} 
} 
if (tallest>0){ 
for (var i=0; i<this.columnswatch.length; i++){ 
if (document.getElementById(this.columnswatch[i])!=null) 
document.getElementById(this.columnswatch[i]).style.height=tallest+"px" 
} 
} 
} 

ddequalcolumns.resetHeights=function(){ 
this.setHeights("reset") 
} 

ddequalcolumns.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload) 
var tasktype=(window.addEventListener)? tasktype : "on"+tasktype 
if (target.addEventListener) 
target.addEventListener(tasktype, functionref, false) 
else if (target.attachEvent) 
target.attachEvent(tasktype, functionref) 
} 

ddequalcolumns.dotask(window, function(){ddequalcolumns.setHeights()}, "load") 
ddequalcolumns.dotask(window, function(){if (typeof ddequalcolumns.timer!="undefined") clearTimeout(ddequalcolumns.timer); ddequalcolumns.timer=setTimeout("ddequalcolumns.resetHeights()", 200)}, "resize") 


</script> 

<div id=container> 
    <div id=nav> 
     <ul> 
       <li>Menu</li> 
       <li>Menu</li> 
       <li>Menu</li> 
       <li>Menu</li> 
       <li>Menu</li> 
     </ul> 
    </div> 
    <div id=content> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam fermentum consequat ligula vitae posuere. Mauris dolor quam, consequat vel condimentum eget, aliquet sit amet sem. Nulla in lectus ac felis ultrices dignissim quis ac orci. Nam non tellus eget metus sollicitudin venenatis sit amet at dui. Quisque malesuada feugiat tellus, at semper eros mollis sed. In luctus tellus in magna condimentum sollicitudin. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur vel dui est. Aliquam vitae condimentum dui. Praesent vel mi at odio blandit pellentesque. Proin felis massa, vestibulum a hendrerit ut, imperdiet in nulla. Sed aliquam, dolor id congue porttitor, mauris turpis congue felis, vel luctus ligula libero in arcu. Pellentesque egestas blandit turpis ac aliquet. Sed sit amet orci non turpis feugiat euismod. In elementum tristique tortor ac semper.</p> 
    </div> 
</div> 

</body> 
</html> 

आप इस स्क्रिप्ट में divs के किसी भी नहीं शामिल कर सकते हैं।

ddequalcolumns.columnswatch = [ "नव", "सामग्री"]

ऊपर लाइन इसकी पर्याप्त में बदलाव लाते हैं।

इसे आजमाएं।

1

मेरे पास एक ही समस्या थी, मैंने इसे कुछ जावास्क्रिप्ट का उपयोग करके हल किया।

<script type="text/javascript"> 
var theHeight = $("#PrimaryContent").height() + 100; 
$('#SecondaryContent').height(theHeight); 
</script> 
-2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
#top, #bottom { 
    height: 100px; 
    width: 100%; 
    position: relative; 
} 
#container { 
    overflow: hidden; 
    position: relative; 
    width: 100%; 
} 
#container .left { 
    height: 550px; 
    width: 55%; 
    position: relative; 
    float: left; 
    background-color: #3399FF; 
} 
#container .right { 
    height: 100%; 
    position: absolute; 
    right: 0; 
    left: 55%; 
    bottom: 0px; 
    top: 0px; 
    background-color: #3366CC; 
} 
</style> 
</head> 

<body> 
<div id="top"></div> 
<div id="container"> 
    <div class="left"></div> 
    <div class="right"></div> 
</div> 
<div id="bottom"></div> 
</body> 
</html> 
+0

-1 आपका कोड पूरी तरह से इस प्रश्न का उत्तर दे सकता है, लेकिन मैं इसे एक ऐसे स्थान में कॉपी करने के लिए बहुत आलसी हूं जहां मैं इसे देख सकता हूं। तो, [fiddle] (http://www.jsfiddle.com/) आपका उत्तर दें और वर्णन करें कि यह क्या करता है। – bishop

0

चाल का उपयोग कर position:absolute, position:relative और top/left/bottom/right: 0px अपनी स्थिति के लिए उपयुक्त नहीं है, तो आप की कोशिश कर सकते:

#nav { 
    height: inherit; 
} 

यह हमारे पृष्ठों में से एक पर काम किया है, हालांकि मुझे यकीन है कि नहीं कर रहा हूँ सफल होने के लिए वास्तव में अन्य स्थितियों की क्या आवश्यकता थी!

संबंधित मुद्दे

 संबंधित मुद्दे