我的文章中有几个带有图像的 div,我正在使用最大宽度/高度和 overflow:hidden 裁剪图像,效果很好,但是它是从顶部裁剪图像,我希望它们垂直居中在 div 中。
.image_div {
max-height: 150px;
max-width:99%;
overflow: hidden;
margin:0px;
text-align:center;
}
.image_div img {
}
我正在使用 max-width/height 来避免在图像很小的情况下有一大块空白。我正在寻找的东西可以用 css 实现吗?
还应该提到 CSS3 解决方案也很棒,因为这是一个移动网站。
最佳答案
<style type="text/css">
.imgsquare {
display: table-cell;
text-align: center;
vertical-align: middle;
padding: 2px;
border: solid 1px #666;
background-color: #333;
border-radius: 4px;
}
.imgsquare * {
vertical-align: middle;
}
.imgsquare {
display: block;
}
.imgsquare span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]>
<style>
.imgsquare span {
display: inline-block;
height: 100%;
}
</style>
<![endif]-->
这会绘制一个正方形 div,其图像以原始纵横比在垂直/水平居中
使用:
<div class="imgsquare" style="width:280px; height:280px;">
<span></span>
<img style="max-width:260px; max-height:260px;" src="yourPicture.jpg" alt="No Image">
</div>
关于css - 隐藏最大宽度/最大高度/溢出的 div 内的垂直对齐图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12697592/