vertical-align with Bootstrap 3

后端 未结 25 2456
庸人自扰
庸人自扰 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:46

        div{
            border: 1px solid;
        }
        span{
            display: flex;
            align-items: center;
        }
        .col-5{
            width: 100px;
            height: 50px;
            float: left;
            background: red;
        }
        .col-7{
            width: 200px;
            height: 24px;
    
            float: left;
            background: green;
        }
        <span>
            <div class="col-5">
            </div>
            <div class="col-7"></div>
        </span>

    0 讨论(0)
  • 2020-11-21 05:46

    I ran into the same situation where I wanted to align a few div elements vertically in a row and found that Bootstrap classes col-xx-xx applies style to the div as float: left.

    I had to apply the style on the div elements like style="Float:none" and all my div elements started vertically aligned. Here is the working example:

    <div class="col-lg-4" style="float:none;">
    

    JsFiddle Link

    Just in case someone wants to read more about the float property:

    W3Schools - Float

    0 讨论(0)
  • 2020-11-21 05:47

    For latest version of bootstrap you can use align-items-center. Use align-items utilities on flexbox containers to change the alignment of flex items on the cross axis (the y-axis to start, x-axis if flex-direction: column). Choose from start, end, center, baseline, or stretch (browser default).

    <div class="d-flex align-items-center">
        ...
    </div>
    
    0 讨论(0)
  • 2020-11-21 05:49

    Following the accepted answer, if you do not wish to customize the markup, for separation of concerns or simply because you use a CMS, the following solution works fine:

    .valign {
      font-size: 0;
    }
    
    .valign > [class*="col"] {
      display: inline-block;
      float: none;
      font-size: 14px;
      font-size: 1rem;
      vertical-align: middle;
    }
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="row valign">
       <div class="col-xs-5">
          <div style="height:5em;border:1px solid #000">Big</div>
       </div>
       <div class="col-xs-5">
           <div style="height:3em;border:1px solid #F00">Small</div>
       </div>
     </div>

    The limitation here is that you cannot inherit font size from the parent element because the row sets the font size to 0 in order to remove white space.

    0 讨论(0)
  • 2020-11-21 05:50

    I prefer this method as per David Walsh Vertical center CSS:

    .children{
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }
    

    The transform isn't essential; it just finds the center a little more accurately. Internet Explorer 8 may be slightly less centered as a result, but it is still not bad - Can I use - Transforms 2d.

    0 讨论(0)
  • 2020-11-21 05:50

    I elaborated a bit on zessx's answer, in order to make it easier to use when mixing different column sizes on different screen sizes.

    If you use Sass, you can add this to your scss-file:

    @mixin v-col($prefix, $min-width) {
        @media (min-width: $min-width) {
            .col-#{$prefix}-v {
                display: inline-block;
                vertical-align: middle;
                float: none;
            }
        }
    }
    
    @include v-col(lg, $screen-lg);
    @include v-col(md, $screen-md);
    @include v-col(sm, $screen-sm);
    @include v-col(xs, $screen-xs);
    

    Which generates the following CSS (that you can use directly in your stylesheets, if you are not using Sass):

    @media (min-width: 1200px) {
        .col-lg-v {
            display: inline-block;
            vertical-align: middle;
            float: none;
        }
    }
    
    @media (min-width: 992px) {
        .col-md-v {
            display: inline-block;
            vertical-align: middle;
            float: none;
        }
    }
    
    @media (min-width: 768px) {
        .col-sm-v {
            display: inline-block;
            vertical-align: middle;
            float: none;
        }
    }
    
    @media (min-width: 480px) {
        .col-xs-v {
            display: inline-block;
            vertical-align: middle;
            float: none;
        }
    }
    

    Now you can use it on your responsive columns like this:

    <div class="container">
        <div class="row">
            <div class="col-sm-7 col-sm-v col-xs-12">
                <p>
                    This content is vertically aligned on tablets and larger. On mobile it will expand to screen width.
                </p>
            </div><div class="col-sm-5 col-sm-v col-xs-12">
                <p>
                    This content is vertically aligned on tablets and larger. On mobile it will expand to screen width.
                </p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-7 col-md-v col-sm-12">
                <p>
                    This content is vertically aligned on desktops and larger. On tablets and smaller it will expand to screen width.
                </p>
            </div><div class="col-md-5 col-md-v col-sm-12">
                <p>
                    This content is vertically aligned on desktops and larger. On tablets and smaller it will expand to screen width.
                </p>
            </div>
        </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题