多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素。
另一种方法是添加line-height:normal,也能实现多个inline-block元素的对齐或inline-block容器内文本的垂直居中。

例如:

<h3>Vertical-align of multiple elements by default not possible</h3>
<div class="block">
<div class="inner inner1">Inline-Block</div>
<div class="inner inner2">Inline-Block</div>
</div> <h3>With an added pseudo element it's possible</h3>
<div class="block2">
<div class="inner">Inline-Block</div>
<div class="inner">Inline-Block</div>
</div> <h3>It also works with just an added line-height and nothing else</h3>
<div class="block3">
<div class="inner inner3">Inline-Block with some lengthy text to show that it also works with multiple lines.</div>
<div class="inner inner3">Inline-Block.</div>
</div> <h3>Button with vertically centered mult-line text</h3>
<div class="block4">
<div class="inner inner4">Inline-Block with centered text.</div>
</div>
/* By default vertical-align ist not possible, only different elements can be vertically aligned among eachother */
.block {
background: red;
height: 100px;
} .inner {
display: inline-block;
vertical-align: middle;
background: yellow;
padding: 3px 5px;
} .inner1 {
height: 30px;
} .inner2 {
height: 20px;
} /* With an added fake (pseudo) element it works. IMPORTANT: There must be no spaces between the elements in the source, else it doesn't work! */
.block2 {
background: orange;
height: 80px;
} /* Fake (pseudo) element, that enables vertical-align */
.block2:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
} /* Also works if you set line-height instead of the height (or together with the same value as the height). No pseudo-element needed. */
.block3 {
background: green;
/*height: 120px;*/
line-height: 120px;
} .inner3 {
width: 30%;
line-height: normal; /* Reset line-height for the child. IMPORTANT: Must be "normal", no integer value allowed! */
} .block4 {
background: magenta;
line-height: 60px;
} .inner4 {
line-height: normal; /* Reset line-height for the child. */
background: none;
} /* Miscellaneous styling */
@import url(https://fonts.googleapis.com/css?family=PT+Sans:400,700); h3, div {
font-family: 'PT Sans', sans-serif;
} .block, .block2, .block3 {
border: 5px dotted rgba(0,0,0,.4);
background-clip: padding-box;
width: 50%;
} .block4 {
text-align: center;
width: 20%;
box-shadow: 3px 3px 0 black;
} h3 {
margin: 40px 0 7px;
}

出处: https://codepen.io/edge0703/pen/iHJuA

最新文章

  1. Hibernatel框架基础使用
  2. 使用CocoaPods过程中 Unable to find a specification for
  3. ie8中遇到的兼容问题以及解决方案
  4. hdu4067
  5. ABP入门系列(4)——领域层定义仓储并实现
  6. Saltstack远程执行(四)
  7. Jenkins入门总结
  8. 某硕笔试题mysql数据库部分(较为全面)
  9. unity3d基础02
  10. 初识MariaDB存储引擎
  11. [POJ 1155] TELE (树形dp)
  12. EditText 光标不显示问题
  13. jquery-object.js
  14. HDU 2514 Another Eight Puzzle(DFS)
  15. HDU - 5276 YJC tricks time
  16. webpack4.0学习记录
  17. jQuery的on绑定事件在mobile safari(iphone / ipad / ipod)上无法使用的解决方案
  18. 使用Fiddler进行手机端抓包
  19. Nginx设置网站维护页面
  20. Python day1 ---python基础1.1

热门文章

  1. SQL 值得记住的点
  2. cuda中当元素个数超过线程个数时的处理案例
  3. Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章
  4. 认识/etc/passwd和/etc/shadow
  5. pycharm配置Git托管
  6. 华东交通大学2018年ACM“双基”程序设计竞赛 K
  7. UVA_1025 a Spy in the Metro 有向无环图的动态规划问题
  8. spark测试几个hadoop的典型例子
  9. Storm: 性能优化 (转载)
  10. appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource