css - 如何在两个div中间垂直对齐文本

标签 css

我试图在 boxes div 的中间垂直对齐 + 符号,但是我无法让它工作。我究竟做错了什么?我也想避免使用表格。谢谢(我还附上了代码笔链接)

<div class="boxes">
   <div class="boxes_box">
   </div>
   <div class="boxes_plus">+</div>
   <div class="boxes_box">
   </div>
</div>

CSS

.boxes {
  height: 250px;
}
.boxes_box {
  width: 250px;
  height: 250px;
  display:inline-block;
  background:#000;
}
.boxes_plus {
  display:inline-block;
  height:250px;
  line-height:250px;
  vertical-align:middle;
}

http://codepen.io/anon/pen/aoiGm

最佳答案

使用这个:

.boxes {
  height: 250px;
  display:table;/*Add display table*/
}
.boxes_box {
  width: 250px;
  height: 250px;
  display:inline-block;
  background:#000;
  display:table-cell;/*display table cell here is not necessary*/
}
.boxes_plus {
  display:inline-block;
  height:250px;
  line-height:250px;
  vertical-align:middle;
  display:table-cell;/*Add display table cell*/
}

fiddle

或者你可以简单地删除 line-height:

.boxes_plus {
  display:inline-block;
  height:250px;
  /*line-height:250px;*/
  vertical-align:middle;
}

fiddle

关于css - 如何在两个div中间垂直对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686901/

相关文章:

jquery - 制作我的图像轮播循环

css - border-left 与 maintable 的 1px

java - 使用 itext 从 html 生成 pdf 时不采用 CSS 样式

css - 从下到上垂直展开DIV

css - 隐藏标准的 AddThis 工具提示和图像

javascript - 我如何让盒子始终具有相同的宽度

html - CSS 默认边框颜色

CSS 宽度/高度像素和百分比计算组合?

javascript - GZIP 压缩和缓存我的 JS 和 CSS 文件

reactjs - 如何在不使用 npm-module 或 react-bootstrap 的情况下在 ReactJS、JSX 中使用工具提示?