CSS 容器 div 未获取高度
我希望我的容器 div 获得其子级高度的最大值。不知道子 div
的高度。我正在尝试 JSFiddle。容器 div
呈红色。哪个没有出现。为什么?
I want my container div to get the height of max of its children's height. without knowing what height the child div
s are going to have. I was trying out on JSFiddle. The container div
is on red. which is not showing up. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
添加以下属性:
这将强制容器尊重其中所有元素的高度,无论浮动元素如何。
http://jsfiddle.net/gtdfY/3/
更新
最近,我正在开发一个项目需要这个技巧,但需要允许溢出显示,因此,您可以使用伪元素来清除浮动,有效地实现相同的效果,同时允许所有元素溢出。
http://jsfiddle.net/gtdfY/368/
Add the following property:
This will force the container to respect the height of all elements within it, regardless of floating elements.
http://jsfiddle.net/gtdfY/3/
UPDATE
Recently, I was working on a project that required this trick, but needed to allow overflow to show, so instead, you can use a pseudo-element to clear your floats, effectively achieving the same effect while allowing overflow on all elements.
http://jsfiddle.net/gtdfY/368/
您使孩子们漂浮,这意味着他们“漂浮”在容器前面。
为了获得正确的高度,您必须“清除”浮动
div style="clear: Both" 清除浮动并为容器提供正确的高度。请参阅 http://css.maxdesign.com.au/floatutorial/clear.htm 了解更多信息漂浮。
例如。
You are floating the children which means they "float" in front of the container.
In order to take the correct height, you must "clear" the float
The div style="clear: both" clears the floating an gives the correct height to the container. see http://css.maxdesign.com.au/floatutorial/clear.htm for more info on floats.
eg.
是不是很容易呢?
It is not that easier?
尝试在最后一个
之前插入此清除 div
Try inserting this clearing div before the last
</div>
<div style="clear: both; line-height: 0;"> </div>
最好也是最防弹的解决方案是向容器添加
::before
和::after
伪元素。因此,如果您有一个像这样的列表:并且列表中的每个元素都有
float:left
属性,那么您应该添加到您的 css:或者您可以尝试
display:inline-block;
属性,那么你不需要添加任何clearfix。The best and the most bulletproof solution is to add
::before
and::after
pseudoelements to the container. So if you have for example a list like:And every elements in the list has
float:left
property, then you should add to your css:Or you could try
display:inline-block;
property, then you don't need to add any clearfix.我遇到了同样的问题,我提出了四个可行的解决方案:
display: flex;
(这是我最喜欢的解决方案)overflow: auto;
> 或overflow:hidden;
到容器I ran into this same issue, and I have come up with four total viable solutions:
display: flex;
(this is my favorite solution)overflow: auto;
oroverflow: hidden;
to the container我刚刚遇到了同样的问题,经过一些研究,我相信在包含元素上使用
display: flow-root
是正确的方法。来自 MDN 上的
clear
财产:因此,虽然
clear: Both
有效(正如其他答案所暗示的那样),但它实际上旨在根据浮动元素的高度正确地将其他元素定位在同一容器内内部。display: flow-root
应该用于使包含元素尊重其所有子元素的高度,以便其他元素(例如包含元素的兄弟)元素)可以正确定位。I just ran into the same issue and after some research I belive using
display: flow-root
on the containing element is the correct approach.From MDN on the
clear
property:So while
clear: both
works (as other answers suggest), it is actually designed to correctly position other elements inside the same container respecting the height of the floating elements.display: flow-root
should be used to make the containing element respect the heights of all it's children so other elements (e.g. siblings to the containing element) can be positioned properly.