html - 内联 block 中的文本垂直居中

标签 html vertical-alignment centering css

我正在尝试使用样式超链接为网站创建一些按钮。除了一个小问题,我已经设法让按钮看起来像我想要的那样。我无法让文本(下面源代码中的“链接”)垂直居中。

不幸的是,第二个按钮显示的文本可能不止一行,所以我无法使用 line-height 将其垂直居中。

我最初的解决方案是使用 display: table-cell; 而不是 inline-block,这解决了 Chrome、Firefox 和 Safari 中的问题,但不是 Internet 中的问题Explorer,所以我认为我需要坚持使用内联 block 。

我将如何在 57px x 100px inline-block 中垂直居中链接文本,并让它适应多行文字?提前致谢。

CSS:

.button {
    background-image:url(/images/categorybutton-off.gif);
    color:#000;
    display:inline-block;
    height:57px;
    width:100px;
    font-family:Verdana, Geneva, sans-serif;
    font-size:10px;
    font-weight:bold;
    text-align:center;
    text-decoration:none;
    vertical-align:middle;
}
.button:hover {
    background-image:url(/images/categorybutton-on.gif);
}
.button:before {
    content:"Click Here For\A";
    font-style:italic;
    font-weight:normal !important;
    white-space:pre;
}

HTML:

<a href="/" class="button">Link</a>
<a href="/" class="button">Link<br />More Details</a>

最佳答案

非常感谢@avrahamcool,非常棒!

我有一点升级。你可以用 css 替换多余的 .Centerer span

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

在此处查看演示:http://jsfiddle.net/modernweb/bXD2V/

注意:这不适用于“内容”属性中的文本。

关于html - 内联 block 中的文本垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485378/

相关文章:

html - 当我单击 iframe(即 Google 脚本 Web 应用程序)内的所有链接时,如何转到另一个页面?

jquery - 通过 JQuery 重用相同的 html/css 代码

css - 具有可变高度的菜单中的链接垂直居中

css - 如何使 CSS 导航菜单居中

android - 居中位图标记(Google Maps Android API v2)

javascript - 单击时跨度子对象

php - 弹出日历

css 在 float 和高度内垂直对齐 100%

html - 如何创建带有下拉菜单的垂直导航栏?

CSS3 背景图片放置