Vertically centering text within an inline-block

杀马特。学长 韩版系。学妹 提交于 2019-11-28 08:08:27
avrahamcool

Wrap your text with a span (Centered), and write another empty span just before it(Centerer) like this.

HTML:

<a href="..." class="button">
  <span class="Centerer"></span>
  <span class='Centered'>Link</span>
</a>

CSS

.Centered
{
    vertical-align: middle;
    display: inline-block;
}

.Centerer
{
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

Take a look here: http://jsfiddle.net/xVnQ6/

Many thanks @avrahamcool, works like a charm!

I have a little upgrade. You can replace redundant .Centerer span with css

.button:before {
  content: '';
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}

See demo here: http://jsfiddle.net/modernweb/bXD2V/

NOTE: This will not work with text in "content" attribute.

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