CSS 容器 div 未获取高度

发布于 2024-12-10 16:52:41 字数 167 浏览 1 评论 0原文

我希望我的容器 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 divs are going to have. I was trying out on JSFiddle. The container div is on red. which is not showing up. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

﹂绝世的画 2024-12-17 16:52:41

添加以下属性:

.c{
    ...
    overflow: hidden;
}

这将强制容器尊重其中所有元素的高度,无论浮动元素如何。
http://jsfiddle.net/gtdfY/3/

更新

最近,我正在开发一个项目需要这个技巧,但需要允许溢出显示,因此,您可以使用伪元素来清除浮动,有效地实现相同的效果,同时允许所有元素溢出。

.c:after{
    clear: both;
    content: "";
    display: block;
}

http://jsfiddle.net/gtdfY/368/

Add the following property:

.c{
    ...
    overflow: hidden;
}

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.

.c:after{
    clear: both;
    content: "";
    display: block;
}

http://jsfiddle.net/gtdfY/368/

节枝 2024-12-17 16:52:41

您使孩子们漂浮,这意味着他们“漂浮”在容器前面。
为了获得正确的高度,您必须“清除”浮动

div style="clear: Both" 清除浮动并为容器提供正确的高度。请参阅 http://css.maxdesign.com.au/floatutorial/clear.htm 了解更多信息漂浮。

例如。

<div class="c">
    <div class="l">

    </div>
    <div class="m">
        World
    </div>
    <div style="clear: both" />
</div>

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.

<div class="c">
    <div class="l">

    </div>
    <div class="m">
        World
    </div>
    <div style="clear: both" />
</div>
祁梦 2024-12-17 16:52:41

是不是很容易呢?

.c {
    overflow: auto;
}

It is not that easier?

.c {
    overflow: auto;
}
许一世地老天荒 2024-12-17 16:52:41

尝试在最后一个 之前插入此清除 div

 

Try inserting this clearing div before the last </div>

<div style="clear: both; line-height: 0;"> </div>

熊抱啵儿 2024-12-17 16:52:41

最好也是最防弹的解决方案是向容器添加 ::before::after 伪元素。因此,如果您有一个像这样的列表:

<ul class="clearfix">
    <li></li>
    <li></li>
    <li></li>
</ul>

并且列表中的每个元素都有 float:left 属性,那么您应该添加到您的 css:

.clearfix::after, .clearfix::before {
     content: '';
     clear: both;
     display: table;
}

或者您可以尝试 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:

<ul class="clearfix">
    <li></li>
    <li></li>
    <li></li>
</ul>

And every elements in the list has float:left property, then you should add to your css:

.clearfix::after, .clearfix::before {
     content: '';
     clear: both;
     display: table;
}

Or you could try display:inline-block; property, then you don't need to add any clearfix.

青春有你 2024-12-17 16:52:41

我遇到了同样的问题,我提出了四个可行的解决方案:

  1. 使容器 display: flex; (这是我最喜欢的解决方案)
  2. 添加 overflow: auto; > 或 overflow:hidden; 到容器
  3. 为容器添加以下 CSS:
.c:after {
    clear: both;
    content: "";
    display: block;
}
  1. 将以下内容作为容器内的最后一项:
<div style="clear: both;"></div>

I ran into this same issue, and I have come up with four total viable solutions:

  1. Make the container display: flex; (this is my favorite solution)
  2. Add overflow: auto; or overflow: hidden; to the container
  3. Add the following CSS for the container:
.c:after {
    clear: both;
    content: "";
    display: block;
}
  1. Make the following the last item inside the container:
<div style="clear: both;"></div>
债姬 2024-12-17 16:52:41

我刚刚遇到了同样的问题,经过一些研究,我相信在包含元素上使用 display: flow-root 是正确的方法。

来自 MDN 上的 clear财产:

如果一个元素只包含浮动元素,它的高度就会消失。如果您希望它始终能够调整大小,以便其内部包含浮动元素,请将元素的 display 属性的值设置为 flow-root。

因此,虽然 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:

If an element contains only floated elements, its height collapses to nothing. If you want it to always be able to resize, so that it contains floating elements inside it, set the value of the element's display property to flow-root.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文