How to vertically center a container in Bootstrap?

后端 未结 9 1435
囚心锁ツ
囚心锁ツ 2020-11-22 09:06

I\'m looking for a way to vertically center the container div inside the jumbotron and to set it in the middle of the page.

The .jum

相关标签:
9条回答
  • 2020-11-22 09:43

    for bootstrap4 vertical center of few items

    d-flex for flex rules

    flex-column for vertical direction on items

    justify-content-center for centering

    style='height: 300px;' must have for set points where center be calc or use h-100 class

    then for horizontal center div d-flex justify-content-center and some container

    so we have hierarhy of 3 tag: div-column -> div-row -> div-container

         <div class="d-flex flex-column justify-content-center bg-secondary" 
            style="height: 300px;">
            <div class="d-flex justify-content-center">
               <div class=bg-primary>Flex item</div>
            </div>
            <div class="d-flex justify-content-center">
               <div class=bg-primary>Flex item</div>
            </div>
          </div> 
    
    0 讨论(0)
  • 2020-11-22 09:43

    Give the container class

    .container{
        height: 100vh;
        width: 100vw;
        display: flex;
    }
    

    Give the div that's inside the container:

    align-content: center;
    

    All the content inside this div will show up in the middle of the page.

    0 讨论(0)
  • 2020-11-22 09:45

    In Bootstrap 4:

    to center the child horizontally, use bootstrap-4 class:

    justify-content-center
    

    to center the child vertically, use bootstrap-4 class:

     align-items-center
    

    but remember don't forget to use d-flex class with these it's a bootstrap-4 utility class, like so

    <div class="d-flex justify-content-center align-items-center" style="height:100px;">
        <span class="bg-primary">MIDDLE</span>    
    </div>
    

    Note: make sure to add bootstrap-4 utilities if this code does not work

    I know it's not the direct answer to this question but it may help someone

    0 讨论(0)
提交回复
热议问题