【发布时间】:2016-01-25 14:53:23
【问题描述】:
我正在尝试将img 居中在包含div 中,其中img 填充(最小)包含div 的宽度和高度的 100%,这意味着图像会自动缩放以保持图像比例。我很容易将img 与顶部、底部、左侧或右侧对齐,但我希望将img 垂直和水平居中。到目前为止,我一直无法找到解决方案,因此非常感谢您的帮助。
HTML
<section id="hero-image">
<img src="https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg">
</section>
CSS
#hero-image {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
background: red;
}
#hero-image img {
position: absolute;
min-height: 100%;
height: auto;
min-width: 100%;
width: auto;
margin: auto;
right: 0;
left: 0;
top: 0;
z-index: 0;
}
【问题讨论】:
-
不清楚你想要达到什么目标。请尝试更好地指定