css - 如何使浮动div的父级100%高度?
这是HTML:
Test
这是CSS:
#inner {
float: left;
height: 100%;
}
在使用Chrome开发人员工具进行检查后,内部div的高度为0px。
如何强制它为父div的高度的100%?
Nathan Osman asked 2019-02-21T10:36:13Z
11个解决方案
123 votes
对于#inner高度要根据其内容,并且其高度为float,使两个元素绝对定位。
更多细节可以在css height属性的规范中找到,但实质上,#inner必须忽略float高度,如果#inner的高度是#outer,除非绝对定位#outer。 那么#inner的高度将为0,除非#inner本身绝对定位。
#outer {
position:absolute;
height:auto; width:200px;
border: 1px solid red;
}
#inner {
position:absolute;
height:100%;
width:20px;
border: 1px solid black;
}
text
但是......通过绝对定位#inner,将忽略float设置,因此您需要明确选择#inner的宽度,并在#outer中添加填充以伪造我怀疑您想要的文本包装。 例如,下面,#outer的填充是#inner +3的宽度。 方便(因为整点是#inner高度为100%)没有必要在#inner下面包装文本,所以这看起来就像#inner浮动。
#outer2{
padding-left: 23px;
position:absolute;
height:auto;
width:200px;
border: 1px solid red;
}
#inner2{
left:0;
position:absolute;
height:100%;
width:20px;
border: 1px solid black;
}
text
我删除了之前的答案,因为它基于对您的目标的错误假设。
Chadwick answered 2019-02-21T10:36:56Z
112 votes
对于父母:
display: flex;
你应该添加一些前缀[http://css-tricks.com/using-flexbox/]
编辑:唯一的缺点是IE像往常一样,IE9不支持flex。[http://caniuse.com/flexbox]
编辑2:正如@toddsby所指出的那样,对齐项目是针对父级的,其默认值实际上是拉伸。 如果你想为孩子提供不同的价值,那就是align-self属性。
编辑3:jsFiddle:[https://jsfiddle.net/bv71tms5/2/]
Aiphee answered 2019-02-21T10:38:00Z
72 votes
实际上,只要定位了父元素,就可以将子元素的高度设置为100%。 即,如果您不希望父母绝对定位。 让我进一步解释一下:
#outer2 {
padding-left: 23px;
position: relative;
height:auto;
width:200px;
border: 1px solid red;
}
#inner2 {
left:0;
position:absolute;
height:100%;
width:20px;
border: 1px solid black;
}
Nathaniel Schweinberg answered 2019-02-21T10:38:31Z
23 votes
只要您不需要支持早于IE8的Internet Explorer版本,您就可以使用.inner来完成此任务:
HTML:
Menu or Whatever
Page contents...
CSS:
.inner {
display: table-cell;
}
这将强制具有.inner类的每个元素占据其父元素的完整高度。
Nathan Osman answered 2019-02-21T10:39:15Z
15 votes
我举了一个例子来解决你的问题。
你必须做一个包装,漂浮它,然后绝对你的div,并给它100%的高度。
HTML
CSS:
.container {
width: 100%;
position:relative;
}
.left {
width: 50%;
background-color: rgba(0, 0, 255, 0.6);
float: left;
}
.right-wrapper {
width: 48%;
float: left;
}
.right {
height: 100%;
position: absolute;
}
说明:.right div是绝对定位的。 这意味着它的宽度和高度,以及顶部和左侧位置将基于第一个父div绝对或相对定位来计算,如果在CSS中显式声明了width或height属性; 如果它们不是声明的,那么将根据父容器(.right-wrapper)计算这些属性。
因此,DIV的100%高度将基于.container最终高度计算,并且.right位置的最终位置将基于父容器计算。
Mandarinazul answered 2019-02-21T10:40:20Z
15 votes
这是实现这一目标的更简单方法:
设置三个元素的容器(#outer)display:table
并设置元素本身(#inner)display:table-cell
删除浮动。
成功。
#outer{
display: table;
}
#inner {
display: table-cell;
float: none;
}
感谢@Itay in Floated div,100%身高
leopinzon answered 2019-02-21T10:41:39Z
10 votes
如果你准备使用一个小jQuery,答案很简单!
$(function() {
$('.parent').find('.child').css('height', $('.parent').innerHeight());
});
这适用于将单个元素浮动到具有100%高度的父级的一侧,而通常环绕的其他浮动元素保持在一侧。
希望这有助于jQuery的粉丝们。
Obi-Dan answered 2019-02-21T10:43:31Z
3 votes
这是具有相同高度的网格系统的代码示例。
#outer{
width: 100%;
margin-top: 1rem;
display: flex;
height:auto;
}
上面是外部div的CSS
#inner{
width: 20%;
float: left;
border: 1px solid;
}
上面是内部div
希望这对你有所帮助
Biswajit answered 2019-02-21T10:44:43Z
1 votes
这对我有帮助。
#outer {
position:relative;
}
#inner {
position:absolute;
top:0;
left:0px;
right:0px;
height:100%;
}
改变右边:左边:设置更好的#inner宽度。
sevenkey answered 2019-02-21T10:45:26Z
0 votes
类似的情况下,当您需要多个子元素具有相同的高度时,可以使用flexbox解决:
[https://css-tricks.com/using-flexbox/]
对于父元素设置display: flex;,对于子元素设置为flex: 1;,它们都具有相同的高度。
romaroma answered 2019-02-21T10:46:35Z
-1 votes
尝试
#outer{overflow: auto;}
显示更多选项:你如何让浮动元素的父母崩溃?
Rodrigo Barreiro answered 2019-02-21T10:47:15Z