简体   繁体   English

CSS嵌套的div高度100%不起作用

[英]CSS nested div height 100% doesn't work

I hope that someone can help me about this problem. 我希望有人可以帮助我解决这个问题。 I have structure like this 我有这样的结构

<div id="Div1" style="height:auto">
 <div id="Div2" style="height:100%;">
  <div id="Div3" style="min-height:100%;"></div>
  <div id="Div4" style="height:100%;"></div>
 </div>
</div>

Also I put in my css file 我也把我的css文件

html,body
{
  height: 100%;
}

Problem is that neither Div3 nor Div4 have the expected height of 100%, I check size of first two divs with firbug and they are ok, filling the whole screen. 问题是Div3和Div4都没有达到100%的预期高度,我用firbug检查了前两个div的大小,并且它们没问题,填满了整个屏幕。

Sorry for my bad English, I hope that you understand my question :) 对不起,我的英语不好,我希望你能理解我的问题:)

First of all write height in inline style 首先以内联样式书写高度

 <div id="Div4" height:100%;"></div>

change to 改成

 <div id="Div4" style="height:100%;"></div>

The key is to set the height of the div with id "Div1" to something other than "auto". 关键是将ID为“ Div1”的div的高度设置为“ auto”以外的其他值。 Try 100% or a specific value like this 尝试100%或类似的特定值

<div id="Div1" style="height:100%;">
<div id="Div2" style="height:100%;">
<div id="Div3" style="min-height:100%;"></div>
<div id="Div4" height:100%;"></div>
</div>
</div>

Have a look at this. 看看这个。 When using a percentage, each div will be affected by the height of it's parent. 使用百分比时,每个div都会受到其父级的高度的影响。

In this example the html,body has a height of 100% and the percentage of each div child is then relative to it's parent. 在此示例中, html,body的高度为100% ,每个div子对象的百分比都相对于其父对象。 Note how each div is half the size of it's parent div , each step it shrinks by half the size. 请注意,每个div的大小是其父div一半,它的每一步都会缩小一半。

Updated with all percentage example 更新了所有百分比示例

Simple example 简单的例子

HTML 的HTML

<div>
    <div>
        <div>
            <div></div>
        </div>
    </div>
</div>

CSS 的CSS

html, body {
    height: 100%;
}
div {
    height: 100%;
    background: #F00;
}
div div {
    background: #FF0;
    height: 50%;
}
div div div {
    background: #000;
    height: 50%;
}
div div div div {
    background: #F30;
    height: 50%;
}

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

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