Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 389
grhu Avatar asked Jan 24 '17 14:01

grhu


People also ask

How can I set full height in Bootstrap?

You can also use max-width: 100%; and max-height: 100%; utilities as needed.

What class do you use to create a 100% width container with Bootstrap?

The .container class provides a responsive fixed width container. The .container-fluid class provides a full width container, spanning the entire width of the viewport.

How can I make Bootstrap 4 columns all the same height?

You just have to use class="row-eq-height" with your class="row" to get equal height columns for previous bootstrap versions.

What class makes width 75%?

To set the width of an element to 75%, use the . w-75 class in Bootstrap.


1 Answers

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.

like image 179
Michael Benjamin Avatar answered Nov 07 '22 03:11

Michael Benjamin