css - 隐藏最大宽度/最大高度/溢出的 div 内的垂直对齐图像?

标签 css overflow

我的文章中有几个带有图像的 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/

相关文章:

c - C 中的简单缓冲区溢出

css - 如何用图像填充单元格

html - 创建一个 7x3 的图像网格

html - CSS删除行之间的间距

html - 文字落后于渐变

html - 带溢出的基于图 block 的布局

javascript - 动画 css/js 波浪不随窗口大小调整大小

html - Google 字体和 Safari SELECT 问题

javascript - 为什么无法应用转换 :none to my elements?

html - 防止 iOS 上的溢出/橡皮筋滚动