Html 在行内块中垂直居中文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18485378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:44:10  来源:igfitidea点击:

Vertically centering text within an inline-block

htmlvertical-alignmentcenteringcss

提问by Josh Bowe

I'm trying to create some buttons for a website using styled hyperlinks. I have managed to get the button looking how I want, bar one slight issue. I can't get the text ('Link' in the source code below) to vertically center.

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

Unfortunately there may be more than one line of text as demonstrated with the second button so I can't use line-heightto vertically center it.

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

My initial solution was to use display: table-cell;rather than inline-block, and that sorts the issue in Chrome, Firefox and Safari but not in Internet Explorer so I'm thinking I need to stick to inline-block.

我最初的解决方案是使用display: table-cell;而不是inline-block,这对 Chrome、Firefox 和 Safari 中的问题进行了排序,但在 Internet Explorer 中却没有,所以我想我需要坚持使用内联块。

How would I go about vertically centering the link text within the 57pxx 100pxinline-block, and have it cater to multiple lines of text? Thanks in advance.

我将如何在57pxx 中垂直居中链接文本100pxinline-block,并使其适应多行文本?提前致谢。

CSS:

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:

HTML:

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

采纳答案by avrahamcool

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

用一个跨度(居中)包裹你的文本,并在它之前写另一个空跨度(居中)像这样。

HTML:

HTML:

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

CSS

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/

看看这里:http: //jsfiddle.net/xVnQ6/

回答by Rafal Ga?ka

Many thanks @avrahamcool, works like a charm!

非常感谢@avrahamcool,它就像一个魅力!

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

我有一点升级。您可以用 css 替换多余的 .Centerer 跨度

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

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

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

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

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