简体   繁体   English

CSS div高度100%不起作用

[英]CSS div height 100% not working

div2 is 1000px height because of div4, div5 and div6. 由于div4,div5和div6,div2的高度为1000px。 I'm not understanding why div3 isn't getting the 100% height, it is 0px height. 我不明白为什么div3没有达到100%的高度,它是0px的高度。

<div id="div1" class="center" style="width:1024px; height:auto;">
    <div id="div2" style="float:left; width:100%; height:auto;">
        <div id="div3" style="float:left; width:460px; height:100%; background-image:url(img/bgVictorMonteiro.jpg); background-size:100% auto; background-position:bottom; background-repeat:no-repeat;"></div>
        <div id="div4" style="float:left; width:270px; height:1000px;"></div>
        <div id="div5" style="float:left; width:25px; height:1000px;"></div>
        <div id="div6" style="float:left; width:269px; height:1000px;"></div>
    </div>
</div>

问题的根源,因为它是div中的div 100%从其容器中取值,如果父div的高度为50px,则子div上的100%意味着它的高度为50px但是你的父div有auto的高度所以它取任何高度:)希望这有助于:p

Using a percentage based height relies on an explicit height value being set on the parent element: 使用基于百分比的height依赖于在父元素上设置的显式height值:

Specifies a percentage height. 指定百分比高度。 The percentage is calculated with respect to the height of the generated box's containing block. 百分比是根据生成的框的包含块的高度计算的。 If the height of the containing block is not specified explicitly (ie, it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'. 如果未明确指定包含块的高度(即,它取决于内容高度),并且此元素未绝对定位,则该值将计算为“auto”。 A percentage height on the root element is relative to the initial containing block. 根元素的百分比高度相对于初始包含块。

Content height: the 'height' property ( http://www.w3.org/TR/CSS21/visudet.html#the-height-property ) 内容高度:'height'属性( http://www.w3.org/TR/CSS21/visudet.html#the-height-property

In this case the parent element is #div2 which has height: auto; 在这种情况下,父元素是#div2 ,其height: auto; . This means that its height depends on the height of its content and is not explicitly declared. 这意味着它的height取决于其内容的height ,并且未明确声明。

If you were to apply the height explicitly to #div2 (eg height: 1000px; ) then using height: 100%; 如果要将height明确应用于#div2 (例如height: 1000px; ),则使用height: 100%; on #div3 would work. #div3会起作用。

 #div1 { width:1024px; height:auto; } #div2 { float:left; width:100%; height:1000px; } #div3 { float:left; width:460px; height:100%; background-color: red; } #div4 { float:left; width:270px; height:1000px; background-color: green; } #div5 { float:left; width:25px; height:1000px; background-color: blue; } #div6 { float:left; width:269px; height:1000px; background-color: orange; } 
 <div id="div1"> <div id="div2"> <div id="div3"></div> <div id="div4"></div> <div id="div5"></div> <div id="div6"></div> </div> </div> 

One possible way of ensuring that #div3 uses the full height without setting it explicitly on #div2 is to use flexbox . 确保#div3使用全height而不在#div2上明确设置的一种可能方法是使用flexbox

 #div1 { width:1024px; height:auto; } #div2 { float:left; width:100%; height:auto; display: flex; } #div3 { width:460px; background-color: red; } #div4 { width:270px; height:1000px; background-color: green; } #div5 { width:25px; height:1000px; background-color: blue; } #div6 { width:269px; height:1000px; background-color: orange; } 
 <div id="div1"> <div id="div2"> <div id="div3"></div> <div id="div4"></div> <div id="div5"></div> <div id="div6"></div> </div> </div> 

You could set the height of parent div1 to 1000px and set the children's height to :inherit . 您可以将父div1的高度设置为1000px ,并将子项的高度设置为:inherit Here's an example: 这是一个例子:

 #div1 { width:1024px; height:1000px; background:red; overflow:hidden; } #div2 { float:left; width:100%; height:inherit; background:yellow; } #div3 { float:left; width:460px; height:inherit; background-image:url('http://www.ricoh-imaging.co.jp/english/r_dc/caplio/r7/img/sample_04.jpg'); background-size:100% 100%; background-position:bottom; background-repeat:no-repeat; } #div4 { float:left; width:270px; height:inherit; background:violet; } #div5 { float:left; width:25px; height:inherit; background:black; } #div6 { float:left; width:269px; height:inherit; background:blue; } 
 <div id="div1" class="center"> <div id="div2"> <div id="div3"></div> <div id="div4"></div> <div id="div5" ></div> <div id="div6"></div> </div> </div> 

It could be a workaround. 这可能是一种解决方法。 I hope it helps. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM