[英]Make div height equal to its parent (100%)
I have a main div that contains two other divs. 我有一个包含其他两个div的主div。 I need that the first one must have the same height of the parent div. 我需要第一个必须具有与父div相同的高度。 The parent div height is not specified in CSS. 父div高度未在CSS中指定。
Here's an example: http://jsfiddle.net/x8dhnh4L/ The pink div must expand to the full height of the parent (red border). 这是一个示例: http : //jsfiddle.net/x8dhnh4L/粉色div必须扩展到父级的整个高度(红色边框)。
One solution I tried is using display:flex
, but it's not IE-friendly (I need a IE8+ compatibility). 我尝试的一种解决方案是使用display:flex
,但是它不是IE友好的(我需要IE8 +兼容性)。 Plus I'd like to achieve this with CSS only, so I'm avoiding JS. 另外,我只想使用CSS来实现这一点,所以我避免使用JS。
You could try using a table layout: 您可以尝试使用表格布局:
set display:table-cell to the childs that need the same height 将display:table-cell设置为需要相同高度的孩子
#container { position: relative; width:600px; border: 1px solid red; display:table; } #content { display: table-cell; background-color:pink; width:400px; } #side-bar { display:table-cell; background-color:yellow; width:170px; padding-left:25px; vertical-align: top; }
here's a working fiddle: http://jsfiddle.net/x8dhnh4L/2/ 这是一个工作的小提琴: http : //jsfiddle.net/x8dhnh4L/2/
As noted in the comments, margins do not work in elements with display:table-cell
. 如评论中所述,在带有display:table-cell
元素中,边距不起作用。 If acceptable, you can use padding-left
instead of margin-left
... You could also add an additional <div>
to separate the 2 columns by 25px. 如果可以接受,则可以使用padding-left
而不是margin-left
...。您还可以添加一个附加的<div>
以将2列分隔25px。
http://jsfiddle.net/x8dhnh4L/1/ http://jsfiddle.net/x8dhnh4L/1/
Set side bar to 将侧栏设置为
float:right;
and set content 并设置内容
height:100%;
A quick solution is to use display:table
for #container
and height:100%
for #content
. 一种快速的解决方案是将display:table
用于#container
,将height:100%
用于#content
。
http://jsfiddle.net/afelixj/x8dhnh4L/5/ http://jsfiddle.net/afelixj/x8dhnh4L/5/
If you actually want the "#content" div to expand to "#container" height, you need to set a height for parent div "#container" and height and float for "#content" 如果您实际上希望将“ #content” div扩展到“ #container”的高度,则需要为父div“ #container”设置一个高度,并为“ #content”设置一个高度和浮点数
#container {
position: relative;
width:600px;
border: 1px solid red;
height: 800px; //whatever height you need
}
#content {
display: inline-block;
background-color:pink;
width:400px;
height:100%;
float: left;
}
This way "#content" height will adjust to "#container" height, but "#side-bar" will take the height it needs to show it's content. 这样,“#content”的高度将调整为“ #container”的高度,但是“#side-bar”将采用显示其内容所需的高度。
With Hanoncs solution the parent div "#container" will adjust to child's div "#content" height. 使用Hanoncs解决方案,父div“ #container”将调整为子div的“ #content”高度。
An easy way around this is using display: table;
解决此问题的一种简单方法是使用display: table;
declaration on the parent element and display: table-cell;
在父元素上声明并display: table-cell;
declaration on the child element. 子元素上的声明。
I would recommend reading Equal Height Columns with Cross-Browser CSS and No Hacks . 我建议阅读使用Cross-Browser CSS和No Hacks的Equal Height Columns 。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.