container with 100% height of section

So here I have a problem with Bootstrap 4 (and also flexbox). Here is the code: 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>

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

I want the .container-fluid to always be 100% of it's parent height, so if section has 100vh, container should also, if it has 50vh it also should have 50vh. How do I do that? I can't center something using flexbox if it's height is the height of h1.


In general, when you want an element to be the height of its parent, make the parent a flex container.

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

An initial setting of a flex container is align-items: stretch . This means that the children of a flex container will automatically stretch the full cross-size of the container. In row-direction that would be the height. Hence, apply display: flex or display: inline-flex to section#intro .

链接地址: http://www.djcxy.com/p/38166.html

上一篇: 红宝石在轨道上

下一篇: 100%高度的容器