How to vertical-align text that runs multiple lines

旧巷老猫 提交于 2019-12-05 17:45:11
Mr Lister

Based on a proposed a solution for a similar problem here, you can do something like this.

  1. Put the link texts inside spans.
  2. Give these spans display:inline-block and the proper widths; which are the original widths of the li items minus the images and the paddings.

.main-services {
  overflow: auto;
  padding: 17px 0 9px 0;
}
.main-services li {
  list-style: none;
  float: left;
  border-right: 1px dashed #E53B00;
  padding-right: 14px;
}
.main-services li a {
  display: block;
  height: 78px;
  color: #ED5D04;
  text-decoration: none;
}
.main-services li a img {
  vertical-align: middle;
}
.main-services li a span {
  display: inline-block;
  vertical-align: middle;
}
.service-1 span { width: 85px; }
.service-2 span { width: 131px; }
.service-3 span { width: 151px; }
<ul class="main-services border-common">
  <li class="service-1">
    <a href="#">
      <img src="http://farm8.staticflickr.com/7177/6928101513_9288b942e8_t.jpg" alt="blah" />
      <span>Some text goes here</span>
    </a>
  </li>
  <li class="service-2">
    <a href="#">
      <img src="http://farm8.staticflickr.com/7177/6928101513_9288b942e8_t.jpg" alt="blah" />
      <span>More text here</span>
    </a>
  </li>
  <li class="service-3">
    <a href="#">
      <img src="http://farm8.staticflickr.com/7177/6928101513_9288b942e8_t.jpg" alt="blah" />
      <span>More text goes here but this text overruns</span>
    </a>
  </li>
</ul>

Or check out the update to the fiddle (including the original reset stylesheet): http://jsfiddle.net/MrLister/5vxSP/15/

Note: this won't work in IE8.

Hades

HTML is very shoddy when it comes to vertical-align. The only way I've found to reliably do this is to do as follows...

<div>
    <span style="display: inline-block; vertical-align: middle; height: [The height of your box here]"></span>
    <span style="display: inline-block; vertical-align: middle;">Put your multi-line content here</span>
</div>

vertical-align in CSS aligns the inline element it is applied to with other inline elements around it. Only on tables does it align within the table cell.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!