css - 旧的在 div 问题中居中图像(图像大小可变 - div 大小固定)

标签 css

我看到了很多关于这个的修复..

但我们的问题是图像在容器 div 中的绝对定位。

我们的图像可以是任意大小(在合理范围内 - php 可以根据需要调整大小但按比例缩放)所以我们有图像集的最大宽度和高度。

示例:html

<a id="product_image_preview"  href="item.php"><img id="image" class="preloader" src="images/products/oakbookcase.jpg" title="Product Title" alt="Product Title" /></a>

我们处理这个问题的 CSS 是:

示例 CSS:

#product_image_preview { width:260px;height:140px;border:1px solid     #aaaaaa;position:relative;line-height:140px;}
#product_image_preview #image {margin:0px auto;vertical-align:middle;max-width:260px;max-height:140px;}

一切正常,问题是,容器 div #product_image_preview 中的图像水平居中,但不是垂直居中。

知道我在哪里搞砸了。

我们的容器 div 总是 260px x 140px

最佳答案

http://jsfiddle.net/dcGZm/13/

该代码应该可以解决您的问题。我从这个链接稍作修改:http://www.brunildo.org/test/img_center.html ,使用 :after 伪元素而不是空跨度。

应该在 IE8 及更高版本中工作。

a {
    background: #000;
    vertical-align: middle; 
    display: table-cell;
    height: 260px;
    width: 140px;
    text-align: center
}

a:after {
    content: ' ';
    height: 100%;
    display: inline-block;
    width: 1px;
    vertical-align: middle
}

a img {
    max-height: 200px;
    max-width: 100px;
    vertical-align: middle
} 

关于css - 旧的在 div 问题中居中图像(图像大小可变 - div 大小固定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8874254/

相关文章:

css - 如何在 CSS 中选择多个 id?

html - 如何通过可能有多个选择名称来修复 undefined index 错误

html - 如何根据屏幕尺寸加载不同的主页

html - 无法定位多个图像 - CSS

CSS 是否在单独的文件中?

javascript - 使用 jquery 添加多个过滤器

html - 将 iframe 对齐到标题下方

html - Div `100%` 高度和 `40px` 底部边距

html - 如何强制分隔线的位置,并将 Logo 放置在特定位置?

css - 如何在 JAVAFX 中调用存在于不同包中的 CSS 文件