简体   繁体   English

DIV不采用父级高度3列

[英]DIV not taking parent height 3 columns

<div class='main_container' style='overflow:hidden; height:100%; height: auto !important; min-height:100%; width:100%;'>
    <div class='float_left' style='width:100px; height:100%;'>
    </div>
    <div class='float_right' style='width:100px;'>
    </div>
    <div class='clear'></div>
    <div class='content'>
         //elastic content that sometimes makes the page longer or shorter
    </div>
</div>

No matter how many tutorials or examples I looked at, nothing is helping me. 无论我看了多少教程或示例,都无济于事。 I've tried many different combinations. 我尝试了许多不同的组合。 As soon as I set main_container to a px height, the sidebars then, correctly, take up 100% of the height. 一旦将main_container设置为px高度,边栏就会正确地占据高度的100%。 But I can't set a px height for the main container. 但是我不能为主容器设置px高度。


EDIT: 编辑:

example

The content box will not have a static height. 内容框不会具有静态高度。 So far what happens is that main_container adjusts it's height based on the content box, but the two sidebars don't adjust there height based on the main_containers height. 到目前为止,发生的是main_container根据内容框调整其高度,但是两个侧边栏并未根据main_containers的高度调整其高度。

In addition to Adrift's answer, you are also overriding the height: 100% with the following height: auto !important - the latter overrides the height setting, even though it is not the source of the problem. 除了Adrift的答案之外,您还覆盖了height: 100%并具有以下height: auto !important即使后者不是问题的根源,后者也会覆盖高度设置。

Here is a Gist that works on Chrome and most likely also on other modern browsers as well. 这是Gist ,可在Chrome上运行,很可能也可在其他现代浏览器上运行。

This solution uses CSS tables cells that allow the left/right sidebars to take on the height of the central .content panel. 此解决方案使用CSS表格单元格,该单元格允许左侧/右侧边栏占据中央.content面板的高度。

The HTML: HTML:

<div class='main_container'>
    <div class='left_bar'></div>
    <div class='content'>
        <p>Elastic content that sometimes makes the page longer or shorter</p>
        <p>Lorem ipsum dolor sit amet, ...</p>
    </div>
    <div class='right_bar'></div>
</div>

The CSS: CSS:

.main_container {
    display: table;
    width: 100%;
}
.left_bar {
    display: table-cell;
    width: 100px;
    background-color: lightgray;
}
.right_bar {
    display: table-cell;
    width: 100px;
    background-color: lightgreen;
}
.content {
    padding: 0 20px;
}

Demo Fiddle: http://jsfiddle.net/audetwebdesign/zahPD/ 演示小提琴: http : //jsfiddle.net/audetwebdesign/zahPD/

As suggested in other comments, you can specify height: 100% or height: inherit to .main_container as required in your application. 如其他注释中所建议,您可以根据应用程序的要求指定height: 100%height: inherit.main_container

Reference for table-cell : https://developer.mozilla.org/en-US/docs/Web/CSS/display table-cell 参考https : //developer.mozilla.org/en-US/docs/Web/CSS/display

Backwards Compatibility 向后兼容
Works with IE8 and above. 适用于IE8及更高版本。

Div not supports height in percent using xhtml document. Div不支持使用xhtml文档的高度百分比。 You use a trick like this: 您可以使用以下技巧:

.main_container{
    position:fixed;
    top:0;bottom:0;
    right:0;left:0;
}

I've write an example here: http://jsfiddle.net/vF6fY/ take a look to it 我在这里写了一个例子: http : //jsfiddle.net/vF6fY/

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

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