【发布时间】:2015-03-16 04:14:39
【问题描述】:
我有一个容器 (.inner)。它包含一个方形 div (.square) 并且是响应式的。我需要将其中的图像垂直和水平居中。我不想使用背景 url 属性。我的 html 标记:
<div class="thumb">
<div class="square">
<a href="">
<img src=""/>
</a>
</div>
</div>
我的正方形在 css 中的工作原理:
.thumb {
position: relative;
width: 100%;
}
.square{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
overflow:hidden;
display: inline-block;
vertical-align: middle;
}
目前,我使用 javascript 为图像添加了一个类,用于横向图像,以使带有 css 的纵向和横向图像适应方形容器的高度和宽度。
img {
max-width: 100%;
height: auto;
width: auto;
}
img.landscape {
max-width: none;
height: 100%;
width:auto!important;
}
但我不知道如何使它们居中。我已经在这里阅读了很多文章,但没有一篇有效。感谢帮助
这是我正在搜索的图片,如果可能的话,也应该垂直工作:
【问题讨论】:
-
将
display: block添加到img -
不知道这应该如何使图像居中 - 尝试过没有成功
-
你想要这样的东西jsfiddle.net/bL6snazL/1
-
你应该看看 Bootstrap,它是一个非常好用的响应式框架。 bootply.com/DUalkrD6uRgetbootstrap.com/components/#thumbnails