Search code examples
htmlcssbootstrap-4vertical-alignment

How to make child content vertically in center as respect to parent container in bootstrap 4


I have been trying to make my child content vertically centered as per the parent container. But the child container is always in relative to parent container. Below is the code I have tried:

HTML

<section id="welcome" class="bg-primary d-flex flex-column">
        <h1>Positions</h1>
        <div class="container text-center my-auto">
           Centered content
        </div>
     </section>

CSS

      #welcome{
        min-height:150px;
        width: 200px;
     }

What I am looking is to make Centered Content text vertically centered as per the whole section.

Here is the codepen link: CodePen


Solution

  • Just make h1 position-absolute to remove it from relative positioning within the DOM...

    https://codepen.io/anon/pen/EeVXbe

    <section id="welcome" class="bg-primary d-flex flex-column">
          <h1 class="position-absolute">Positions</h1>
          <div class="container text-center my-auto">
               Centered content
          </div>
    </section>