[英]Align text center to the center of an image
I have some simple social media images, and I need the text to be in line with the center of each image. 我有一些简单的社交媒体图像,我需要文本与每个图像的中心对齐。 At the moment, the text is aligned to the bottom of each image, by default: 目前,默认情况下,文本与每个图像的底部对齐:
http://jsfiddle.net/uT4Ey/ http://jsfiddle.net/uT4Ey/
CSS: CSS:
.img {
background-color: red;
height: 3em;
width: 3em;
display: inline-block;
}
HTML: HTML:
<div class="sm">
<div class="img"></div>
facebook
</div>
<div class="sm">
<div class="img"></div>
instagram
</div>
<div class="sm">
<div class="img"></div>
facebook
</div>
<div class="sm">
<div class="img"></div>
isntagram
</div>
Add vertical-align:middle;
添加vertical-align:middle;
to img
or div
. 到img
或div
。
.img {
background-color: red;
height: 3em;
width: 3em;
display: inline-block;
vertical-align: middle; /*CAN GO HERE*/
}
div {
vertical-align: middle; /*OR HERE*/
}
You should add a margin
to the elements after to ensure that they're not all stuck together. 之后,应在元素上添加margin
,以确保元素不会全部粘在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.