【发布时间】:2016-05-13 13:35:35
【问题描述】:
我已经动态生成了缩略图,这些缩略图将被插入到一个流体宽度的容器中。缩略图的宽度/高度设置为 150 像素,边距为 5 像素。我希望瓦片在容器内水平居中,但我希望每个瓦片在彼此下方排列形成一个网格。
我编写了以下代码,但无法让缩略图在容器内居中。如果我删除float:left;,缩略图将居中,但缩略图不再排列在彼此下方以形成网格。
我将如何修改 CSS 以实现在流体宽度容器中居中的缩略图网格?
https://jsfiddle.net/bL7kfq5s/
<div class="wrapper">
<div class="thumb"><img src="http://www.placehold.it/150x150"></div>
<div class="thumb"><img src="http://www.placehold.it/150x150"></div>
<div class="thumb"><img src="http://www.placehold.it/150x150"></div>
<div class="thumb"><img src="http://www.placehold.it/150x150"></div>
<div class="thumb"><img src="http://www.placehold.it/150x150"></div>
</div>
.wrapper {
width: 600px; /* responsive width */
background: yellow;
text-align: center;
overflow: hidden;
}
.thumb {
display: inline;
float: left;
margin: 0 5px 5px;
width: 150px;
}
【问题讨论】: