当我不知道图像的高度时,如何使用CSS垂直居中图像?图像及其高度将由服务器在运行时提供,并且它可以是任意数量的高度。因此,我创建了一个div内部的div,或多或少地遵循此处找到的教程的第1种方法:http://phrogz.net/css/vertical-align/。但是我认为这仅适用于您知道要居中的内容的高度的情况。那么我该如何垂直居中这个图像呢?
这是失败的CSS代码:
<div class="galleryItem">
<a href="wherever">
<img src="whatever" width="173" height="155">
<div class="galleryTitle">
<div class="galleryImg">
<img src="centerMeVertically.jpg">
</div>
</div>
</a>
</div>
这是失败的CSS代码:
.galleryItem {
float: left;
border-style: solid;
border-width: 1px;
border-color: #b78e18;
margin: 7px 4px;
padding: 5px;
width: 173px;
height: 190px;
position: relative;
}
.galleryTitle {
height: 33px;
width: 173px;
position: relative;
}
.galleryImg {
width: 173px;
height: 30px;
position: absolute;
top: 50%;
margin-top: -15px;
}
.galleryImg > img {
display: block;
margin-left: auto;
margin-right: auto;
}