twitter-bootstrap 为什么我的div没有高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23877099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:48:22  来源:igfitidea点击:

Why does my div have no height?

htmlcsstwitter-bootstraplayout

提问by Hugo

I'm trying to mimic the row-column mechanism that twitter's bootstrap css framework uses.

我正在尝试模仿 twitter 的 bootstrap css 框架使用的行列机制。

I have a div containing some other divs which themselves contain come content. In my element inspector, it shows the container as having no height. Shouldn't the height of the div be the height of the elements it contains?

我有一个包含一些其他 div 的 div,这些 div 本身包含内容。在我的元素检查器中,它显示容器没有高度。div的高度不应该是它包含的元素的高度吗?

<div class="container">
    <div class="row yellow">
        <div class="column-1 red">
            column-1
        </div>

        <div class="column-1 blue">
            column-1
        </div>

        <div class="column-1 green">
            column-1
        </div>

        <div class="column-1 orange">
            column-1
        </div>

        <div class="column-1 red">
            column-1
        </div>

        <div class="column-1 blue">
            column-1
        </div>

        <div class="column-1 green">
            column-1
        </div>

        <div class="column-1 orange">
            column-1
        </div>

        <div class="column-1 red">
            column-1
        </div>
    </div>
</div>

Here is a jsfiddle:

这是一个jsfiddle:

HTML/CSS in question

有问题的 HTML/CSS

回答by web-tiki

The reason why the height or the containers is 0 is because you are floating all the contentinside them and floated elements don't expand the height (or width) of their parents.

高度或容器为 0 的原因是因为您正在浮动其中的所有内容,并且浮动元素不会扩展其父项的高度(或宽度)。

As you are floating all elents in rowit has 0 height and therfore so has .container.

当您漂浮时,其中的所有元素row都具有 0 高度,因此.container.

To prevent this, you can :

为了防止这种情况,您可以:

  1. set overflow:hidden;on the containers demo
  2. clear the float with a block element at the end of the container with clear:both;demo
  1. overflow:hidden;在容器演示中设置
  2. 使用演示在容器末尾使用块元素清除浮动clear:both;

You can also check this question for reference : How do you keep parents of floated elements from collapsing?

您还可以查看此问题以供参考:如何防止浮动元素的父级崩溃?