[英]CSS Text Vertical Align Middle
I got a problem. 我有问题 I am trying to make an image with a hover. 我正在尝试将鼠标悬停在图像上。 I got some text in the div. 我在div中有一些文字。 Now i want the text to be centered of the div. 现在,我希望文本以div为中心。 I used: 我用了:
display: table-cell;
vertical-align: middle;
The text is Almost in the middle of the Div. 文本几乎在Div的中间。 When i add more text to it, you will see that the text isnt centered. 当我向其中添加更多文本时,您将看到文本未居中。
Live Example: http://jsfiddle.net/DennisBetman/6TkkE/ 实时示例: http : //jsfiddle.net/DennisBetman/6TkkE/
I hope some of you know the problem and can help me. 希望你们中的一些人知道问题所在并能对我有所帮助。
You made your <h1>
have a position:absolute
, but there was no container that it was positioned relative to. 您已将<h1>
设置为position:absolute
,但是没有相对于它定位的容器。
I took position:absolute
and height:100%
off of the <h1>
. 我的position:absolute
是<h1>
position:absolute
和height:100%
。 then I added position:relative
to its container .text
然后我添加了position:relative
对于它的容器.text
Here's the fiddle to show this: http://jsfiddle.net/6TkkE/5/ 这是显示此的小提琴: http : //jsfiddle.net/6TkkE/5/
Here is a nice workaround for this, which worked fine for my purpose: Get a container of your size (relative is fine too). 这是一个不错的解决方法,它对我的目的很好用:获取一个您大小的容器(相对来说也很好)。 Make it's position relative, place a span or p inside it with absolute position. 使其相对位置,在其中以绝对位置放置一个跨度或p。 Give it a line-height, maybe 30px. 给它一个行高,也许是30px。 Depends on your font size. 取决于您的字体大小。 Then center it with left and top, fix the margin and the width must be 100% to apply text-align: center. 然后将其居中与左侧和顶部居中,固定边距,并且宽度必须为100%才能应用text-align:center。
.container {
position: relative;
width: 800px;
height: 600px;
}
.container > span {
position: absolute;
left: 50%;
top: 50%;
margin-top: -15px;
margin-left: -50%;
line-height: 30px;
width: 100%;
text-align: center;
}
...
<div class="container">
<span>Vertical and Horizontal align.</span>
</div>
Worked fine for me! 对我来说很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.