html - 如何垂直居中另一列内的 Bootstrap 列?

标签 html css twitter-bootstrap

<分区>

我想将 col-md-12 中的 div 垂直居中。这是结构:

.list-item {
  border-bottom: 1px solid #e7e7e7;
  float: left;
  padding: 15px 0;
  width: 100%;
}
.list-item img {
  width: 60px;
  height: 60px;
}
.col-md-2 {
  float: left;
  width: 16.66666667%;
}
<div class="list-item col-md-12 clearfix">
  <div class="col-md-1">
    <img class="img-circle" src="https://aframe.io/aframe/examples/_skies/puydesancy.jpg">
  </div>
  <div class="col-md-2">
    <strong>
      <a href="#/buildings/1">Central park</a>
    </strong>
  </div>
  <div class="col-md-5">
    <span>The description for central park description for the central park</span>
  </div>
  <div class="col-md-2">
    <span>Category count:</span>
    <strong>3</strong>
  </div>
  <div class="col-md-2">
    <span>Panorama count:</span>
    <strong>7</strong>
  </div>
</div>

我尝试了:vertical-align: middledisplay: inline 但没有用。还有别的办法吗?

这是 JSFiddle:https://jsfiddle.net/wLgfLo3L/

(您需要将浏览器调整为全屏)。

最佳答案

这个 css 是一个非常简单的解决方案。

HTML

<div class="list-item col-md-12 clearfix main">
  <div class="col-md-1 center">
    <img class="img-circle" src="https://aframe.io/aframe/examples/_skies/puydesancy.jpg">
  </div>
  <div class="col-md-2 center">
    <strong>
      <a href="#/buildings/1">Central park</a>
    </strong>
  </div>
  <div class="col-md-5 center">
    <span>The description for central park description for the central park</span>
  </div>
  <div class="col-md-2 center">
    <span>Category count:</span>
    <strong>3</strong>
  </div>
  <div class="col-md-2 center">
    <span>Panorama count:</span>
    <strong>7</strong>
  </div>
</div>

CSS

.main{
  display:table;
}
.center{
  display:table-cell;
  vertical-align:middle;
}

在上面的代码中,我添加了 .main 类到主 div 和 .center 类到你想要居中对齐的 div。

使用 display:table 到 main 和 display:table-cell 到内部 div 你可以将 div 居中对齐 vertical-align:middle 因为这会将 div 的性质转换为表格。

这是更新的 fiddle 链接检查它 https://jsfiddle.net/yudi/wLgfLo3L/1/

关于html - 如何垂直居中另一列内的 Bootstrap 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215876/

相关文章:

javascript - 如何避免 PNG 框架溢出?

java - 单击提交按钮时不执行任何操作

javascript - Textarea.textcontent 没有改变

html - 搜索栏标题

css - 背景重复 : repeat-y; starting position CSS

jquery box-shadow 未应用

javascript - 为什么我的 html 文件突然不工作了?

javascript - Dialogflow Messenger 聊天图标位置

html - 如何在 bootstrap 4 中将按钮右对齐

javascript - 为什么 jstree 在页面加载时会出现损坏的情况