必承其重 | 欲带皇冠 2013-12-12 15:43 采纳率: 0%
浏览 591
已采纳

vertical-align with Bootstrap 3

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

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="row">
  <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 grid system in Bootstrap uses float: left, not display:inline-block, so the property vertical-align doesn't work. I tried using margin-top to fix it, but I think this is not a good solution for the responsive design.

</div>

转载于:https://stackoverflow.com/questions/20547819/vertical-align-with-bootstrap-3

  • 写回答

23条回答 默认 最新

  • bug^君 2013-12-12 16:17
    关注

    This answer presents a hack, but I would highly recommend you to use flexbox (as stated in @Haschem answer), since it's now supported everywhere.

    Demos link:
    - Bootstrap 3
    - Bootstrap 4 alpha 6

    You still can use a custom class when you need it:

    .vcenter {
        display: inline-block;
        vertical-align: middle;
        float: none;
    }
    <div class="row">
        <div class="col-xs-5 col-md-3 col-lg-1 vcenter">
            <div style="height:10em;border:1px solid #000">Big</div>
        </div><!--
        --><div class="col-xs-5 col-md-7 col-lg-9 vcenter">
            <div style="height:3em;border:1px solid #F00">Small</div>
        </div>
    </div>

    Bootply

    Using inline-block adds extra space between blocks if you let a real space in your code (like ...</div> </div>...). This extra space breaks our grid if column sizes add up to 12:

    <div class="row">
        <div class="col-xs-6 col-md-4 col-lg-2 vcenter">
            <div style="height:10em;border:1px solid #000">Big</div>
        </div>
        <div class="col-xs-6 col-md-8 col-lg-10 vcenter">
            <div style="height:3em;border:1px solid #F00">Small</div>
        </div>
    </div>
    

    Here, we've got extra spaces between <div class="[...] col-lg-2"> and <div class="[...] col-lg-10"> (a carriage return and 2 tabs/8 spaces). And so...

    Enter image description here

    Let's kick this extra space!!

    <div class="row">
        <div class="col-xs-6 col-md-4 col-lg-2 vcenter">
            <div style="height:10em;border:1px solid #000">Big</div>
        </div><!--
        --><div class="col-xs-6 col-md-8 col-lg-10 vcenter">
            <div style="height:3em;border:1px solid #F00">Small</div>
        </div>
    </div>
    

    Enter image description here

    Notice the seemingly useless comments <!-- ... -->? They are important -- without them, the whitespace between the <div> elements will take up space in the layout, breaking the grid system.

    Note: the Bootply has been updated

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(22条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?