Bootstrap 4 - 容器截面高度为100%

grh*_*rhu 8 html css flexbox twitter-bootstrap

所以这里我有Bootstrap 4(以及flexbox)的问题.这是代码:HTML:

<section id="intro">  
    <div class="container-fluid">
        <div class="row align-items-center">
            <div class="col align-self-center">
                <h1>We design things</h1>
            </div>
        </div>
    </div>
</section>
Run Code Online (Sandbox Code Playgroud)

和CSS:

#intro {
    min-height: 65vh;
    background-image: url("../img/intro.png");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}
.container-fluid {
    height: 100%;
    min-height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

我希望.container-fluid始终是它的父高度的100%,所以如果section有100vh,容器也应该,如果它有50vh它也应该有50vh.我怎么做?如果它的高度是h1的高度,我无法使用flexbox居中.

Mic*_*l_B 7

通常,当您希望元素为其父元素的高度时,请将父元素设置为Flex容器.

#intro {
    min-height: 65vh;
    background-image: url("../img/intro.png");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    display: flex; /* NEW */
}
Run Code Online (Sandbox Code Playgroud)

Flex容器的初始设置是align-items: stretch.这意味着Flex容器的子级将自动拉伸容器的完整交叉大小.在行方向上,这将是高度.因此,申请display: flexdisplay: inline-flexsection#intro.

  • 添加显示:flex到.container-fluid修复它,但我确定Boostrap 4,它应该是由flexbox构建的默认情况下 - 事实证明只有行有flex属性这很奇怪.谢谢您帮忙. (2认同)