我希望在图片上叠加白色半透明背景的文本,当人们鼠标悬停在该图片上时。我在这里找到了包含以下代码原始类型的答案。现在我想将文本垂直居中对齐到图片中间。
这是可能的吗?
HTML:
<div class="image">
<img src="http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg" alt="" />
</div>
CSS:
.image {
position:relative;
width:400px;
height:400px;
}
.image img {
width:100%;
vertical-align:top;
}
.image:after {
content: 'Hello';
color: #000000;
vertical-align: middle;
text-align: center;
position: absolute;
width:100%;
height:100%;
top:0;
left:0;
background: rgba(255,255,255,0.7);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.image:hover:after {
opacity:1;
}