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

标签 html css twitter-bootstrap flexbox

所以这里我遇到了 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>

和 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%;
}

我希望 .container-fluid 始终是其父高度的 100%,因此如果部分有 100vh,容器也应该有,如果它有 50vh,它也应该有 50vh。我怎么做?如果它的高度是 h1 的高度,我不能使用 flexbox 居中。

最佳答案

一般来说,当你想要一个元素是它父元素的高度时,让父元素成为一个 flex 容器。

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

flex 容器的初始设置是align-items: stretch。这意味着 flex 容器的子容器将自动拉伸(stretch)容器的整个cross-size。在行方向上,这将是高度。因此,将 display: flexdisplay: inline-flex 应用于 section#intro

关于html - Bootstrap 4 - 截面高度为 100% 的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41830769/

相关文章:

html - 我的标题保持固定,但我的 body 仍在滚动

html - CSS - 对其中的许多图像使用 1 个图像

HTML/CSS 对齐两个输入和下面的文本

javascript - 响应式复选框或 bool 值切换

html - chrome 中意外的下划线 css 神器

javascript - Bootstrap 下拉菜单不会响应

html - 如何在网络上显示iTunesconnect销售和财务报告?

html - 阻止背景图像落后于页眉/页脚

html - 标题属性工具提示和 iframe

javascript - IE7+ 的复选框/单选框样式