[英]Vertically align a text with an image inside a div
I saw a similar post to this which is here but I noticed that no one answered and that when I do press the jsfiddle, it doesn't center it. 我在这里看到了与此类似的帖子,但我注意到没有人回答,当我按jsfiddle时,它不会居中。
I have this current code and what I'm trying to accomplish is to have the image directly in the middle vertically inside the div. 我有当前代码,我要完成的工作是将图像直接直接放在div内部的中间位置。 I've tried vertical-align and putting it in span and top % but nothing seems to work.
我试过垂直对齐并将其放在span和top%中,但似乎没有任何效果。 I'm also using Chrome so I don't know if that matters.
我也在使用Chrome,所以我不知道这是否重要。 http://jsfiddle.net/y84mx/
http://jsfiddle.net/y84mx/
<div class="hours">
<div id="sun">
<img src="http://jsfiddle.net/img/logo.png" alt="Opening" height="auto" width="20%" align="left" vertical-align="middle" />
<span>Hours</span>
</div>
</div>
Thanks! 谢谢!
You can use: 您可以使用:
#sun {
position:relative;
}
img {
background: #3A6F9A;
position:absolute;
top:0;
bottom:0;
left:0;
margin:auto;
}
For both items set display: inline-block;
对于这两个项目,都
display: inline-block;
Then make sure to match the line-height
of the <span>
the height of the image. 然后确保以匹配
line-height
的的<span>
的图像的高度。
HTML HTML
<div id="hours">
<span id="sun">
<img src="http://jsfiddle.net/img/logo.png" alt="twibuffer" />
</span>
<span> Hours</span>
</div>
CSS CSS
#hours {
margin-bottom:2%;
text-align: center;
}
#hours span {
display: inline-block;
vertical-align: middle;
}
#hours #sun {
border: 1px solid red;
/* to show the centering */
}
Check out this http://jsfiddle.net/chetangawai/y84mx/9/ 看看这个http://jsfiddle.net/chetangawai/y84mx/9/
#sun{
text-align:center;
display:inline-block;
border:solid
}
.hours{
color: #000;
font-size: 40px;
padding-left: 2%;
padding-right: 2%;
}
img {
background: #3A6F9A;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.