css - 在固定大小的 div 中垂直对齐居中图像

标签 css image position vertical-alignment

我有一个 145px X 145px 的 div。我在这次潜水中有一个 img。 img 可以是任何大小(最长边为 130px)。我希望图像在 div 中垂直居中。我尝试过的所有内容都适用于大多数浏览器,但不适用于 IE7。我需要可以在 IE7 中运行的东西。

最佳答案

这是一个跨浏览器的解决方案:

<div class="img-container"><img src="picture.png" class="cropped"/></div>


div.img-container {
     width: 390px;
     height: 211px;
     position: relative;
     margin-left: auto;
     margin-right: auto;
     overflow: hidden;
 }
img.cropped {
    position: absolute;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

关于css - 在固定大小的 div 中垂直对齐居中图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5005244/

相关文章:

Android Intent 打开图库在 KitKat (4.4) 上不起作用

jQuery动态设置对象的位置?

javascript - 位置 :sticky is triggered 时检测的事件

html - 如何将 3 个水平排列的图像在页面上居中?

javascript - 不在桌面版时隐藏 Logo

css - 缓存背景图片

css - 伪选择器 :before not positioning correctly in IE9, 10

css - vh(视口(viewport))中的高度和 px(像素)中的最大高度

android - 波长照相机

php - 存储在mysql中的图像的背景封面