grhu
grhu

Reputation: 470

Bootstrap 4 - 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.

Upvotes: 9

Views: 25494

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371619

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.

Upvotes: 8

Related Questions