vertical-align with Bootstrap 3

后端 未结 25 2226
庸人自扰
庸人自扰 2020-11-21 05:34

I\'m using Twitter Bootstrap 3, and I have problems when I want to align vertically two div, for example — JSFiddle link:

相关标签:
25条回答
  • 2020-11-21 05:58

    Try this in the CSS of the div:

    display: table-cell;
    vertical-align: middle;
    
    0 讨论(0)
  • 2020-11-21 05:58

    I thought I'd share my "solution" in case it helps anyone else who isn't familiar with the @media queries themselves.

    Thanks to @HashemQolami's answer, I built some media queries that would work mobile-up like the col-* classes so that I could stack the col-* for mobile but display them vertically-aligned in the center for larger screens, e.g.

    <div class='row row-sm-flex-center'>
        <div class='col-xs-12 col-sm-6'></div>
        <div class='col-xs-12 col-sm-6'></div>
    </div>
    

    .

    .row-xs-flex-center {
        display:flex;
        align-items:center;
    }
    @media ( min-width:768px ) {
        .row-sm-flex-center {
            display:flex;
            align-items:center;
        }
    }
    @media ( min-width: 992px ) {
        .row-md-flex-center {
            display:flex;
            align-items:center;
        }
    }
    @media ( min-width: 1200px ) {
        .row-lg-flex-center {
            display:flex;
            align-items:center;
        }
    }
    

    More complicated layouts that require a different number of columns per screen resolution (e.g. 2 rows for -xs, 3 for -sm, and 4 for -md, etc.) would need some more advanced finagling, but for a simple page with -xs stacked and -sm and larger in rows, this works fine.

    0 讨论(0)
  • 2020-11-21 06:00

    If you are using the Less version of Bootstrap, and compiling into CSS yourself, you can use some utility classes to use with Bootstrap classes.

    I've found these to work fantastically well where I want to preserve the responsiveness and configurability of the Bootstrap grid system (like using -md or -sm), but I want all columns in a given row to all have the same vertical height (so that I can then vertically align their content and have all columns in the row share a common middle).

    CSS/Less:

    .display-table {
        display: table;
        width: 100%;
    }
    .col-md-table-cell {
        @media (min-width: @screen-md-min) {
            display: table-cell;
            vertical-align: top;
            float: none;
        }
    }
    .col-sm-table-cell {
        @media (min-width: @screen-sm-min) {
            display: table-cell;
            vertical-align: top;
            float: none;
        }
    }
    .vertical-align-middle {
        vertical-align: middle;
    }
    

    HTML:

    <div class="row display-table">
        <div class="col-sm-5 col-sm-table-cell vertical-align-middle">
            ...
        </div>
        <div class="col-sm-5 col-sm-table-cell vertical-align-middle">
            ...
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-21 06:01

    Update 2020

    I know the original question was for Bootstrap 3, but now that Bootstrap 4 has been released, here is some updated guidance on vertical center.

    Important! Vertical center is relative to the height of the parent

    If the parent of the element your trying to center has no defined height, none of the vertical centering solutions will work!

    Bootstrap 4

    Now that Bootstrap 4 is flexbox by default there are many different approaches to vertical alignment using: auto-margins, flexbox utils, or the display utils along with vertical align utils. At first "vertical align utils" seems obvious, but these only work with inline and table display elements. Here are some Bootstrap 4 vertical centering options..


    1 - Vertical Center Using Auto Margins:

    Another way to vertically center is to use my-auto. This will center the element within its container. For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.

    <div class="row h-100">
        <div class="col-sm-12 my-auto">
            <div class="card card-block w-25">Card</div>
        </div>
    </div>
    

    Bootstrap 4 - Vertical center using auto-margins Demo

    my-auto represents margins on the vertical y-axis and is equivalent to:

    margin-top: auto;
    margin-bottom: auto;
    

    2 - Vertical Center with Flexbox:

    Since Bootstrap 4 .row is now display:flex you can simply use align-self-center on any column to vertically center it...

           <div class="row">
               <div class="col-6 align-self-center">
                    <div class="card card-block">
                     Center
                    </div>
               </div>
               <div class="col-6">
                    <div class="card card-inverse card-danger">
                        Taller
                    </div>
              </div>
        </div>
    

    or, use align-items-center on the entire .row to vertically center align all col-* in the row...

           <div class="row align-items-center">
               <div class="col-6">
                    <div class="card card-block">
                     Center
                    </div>
               </div>
               <div class="col-6">
                    <div class="card card-inverse card-danger">
                        Taller
                    </div>
              </div>
        </div>
    

    Bootstrap 4 - Vertical center different height columns Demo


    3 - Vertical Center Using Display Utils:

    Bootstrap 4 has display utils that can be used for display:table, display:table-cell, display:inline, etc.. These can be used with the vertical alignment utils to align inline, inline-block or table cell elements.

    <div class="row h-50">
        <div class="col-sm-12 h-100 d-table">
            <div class="card card-block d-table-cell align-middle">
                I am centered vertically
            </div>
        </div>
    </div>
    

    Bootstrap 4 - Vertical center using display utils Demo

    Also see: Vertical Align Center in Bootstrap 4


    Bootstrap 3

    Flexbox method on the container of the item(s) to center:

    .display-flex-center {
        display: flex;
        align-items: center;
    }
    

    Transform translateY method:

    .transform-center-parent {
        position: relative;
        transform-style: preserve-3d;
    }
    
    .transform-center {
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }
    

    Display inline method:

    .display-inline-block {
        display: inline;
    }
    .display-inline-block > div {
        display: inline-block;
        float: none;
        vertical-align: middle;
    }
    

    Demo of Bootstrap 3 centering methods

    0 讨论(0)
  • 2020-11-21 06:02

    I ran into this same issue. In my case I did not know the height of the outer container, but this is how I fixed it:

    First set the height for your html and body elements so that they are 100%. This is important! Without this, the html and body elements will simply inherit the height of their children.

    html, body {
       height: 100%;
    }
    

    Then I had an outer container class with:

    .container {
      display: table;
      width: 100%;
      height: 100%; /* For at least Firefox */
      min-height: 100%;
    }
    

    And lastly the inner container with class:

    .inner-container {
      display: table-cell;
      vertical-align: middle;
    }
    

    HTML is as simple as:

    <body>
       <div class="container">
         <div class="inner-container">
            <p>Vertically Aligned</p>
         </div>
       </div>
    </body>
    

    This is all you need to vertically align contents. Check it out in fiddle:

    Jsfiddle

    0 讨论(0)
  • 2020-11-21 06:03

    I've spent a lot of time trying solutions from here and none of them helped me. After a few hours, I belive this will allow u to center vert any child element in BS3.

    CSS

    .vertical-center {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    

    HTML

    <div class="col-md-6 vertical-center">
        <div class="container">
            <div class="row">
            </div> 
        </div>   
    </div>
    
    0 讨论(0)
提交回复
热议问题