简体   繁体   English

如何在div中垂直居中放置文本?

[英]How to center text in a div vertically?

I've centered my text in my div but I've only managed to center it horizontally by using text-align: center However I'm having trouble centering the text vertically. 我已经在div居中放置了文本,但是我只能通过使用text-align: center来使其水平居中text-align: center但是,我在垂直居中放置text-align: center遇到了麻烦。 I've tried using vertical-align: middle; 我试过使用vertical-align: middle; but it didn't work 但这没用

Here is a codepen: http://codepen.io/anon/pen/Ioiaq 这是一个Codepen: http ://codepen.io/anon/pen/Ioiaq

the code with the text is right at the bottom of the html panel the text is "INFO" 带有文本的代码位于html面板的底部,文本为“ INFO”

and here's the code so far: 这是到目前为止的代码:

<div id="inf" style="
                     text-align:center;  
                     background-color: white; 
                     position: absolute; top:50%; left: 45%; width: 10%; height: 6%; 
                     z-index: 10;  
                     display: inline-block;">

  <a href="#"><span>INFO</span></a>
  </div>

any ideas? 有任何想法吗?

I don't mind what code is used to fix the alignment it doesn't have to be css. 我不介意使用什么代码来修复对齐方式,而不必是CSS。

要使单条线垂直居中,可以使用CSS line-height,该值与您使用的高度值相同。

line-height with a vertical-align: middle; vertical-align: middle; line-height vertical-align: middle; is probably the best way to go if you aren't using JS. 如果您不使用JS,这可能是最好的方法。

FIDDLE 小提琴

HTML: HTML:

<div id="inf">
    <a href="#"><span>INFO</span></a>
</div>

CSS: CSS:

#inf { 
    text-align:center;  
    background-color: white; 
    z-index: 10;  
    display: inline-block;
    width: 100%;
    line-height: 400px;
    vertical-align: middle;
}

您可以使用CSS属性vertical-align。

vertical-align: middle;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM