所以我有一个大的 div 标签。这里的 css 样式是:
.thumbcontainer {
width: 900px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
display: block;
overflow: hidden;
}
我有 4 个 div 标签。他们的CSS样式:
.thumbnails {
float: left;
margin-top: auto;
margin-right: 10px;
margin-bottom: auto;
margin-left: 10px;
}
最后是代码:
<div class="thumbcontainer">
<div class="thumbnails">
<a href="gallery/1.jpg" style="" rel="lightbox[gallery]"><img src="gallery/1-thumbnail.jpg" width="150" height="112" alt=""/></a>
</div>
<div class="thumbnails">
<a href="gallery/2.jpg" rel="lightbox[gallery]"><img src="gallery/2-thumbnail.jpg" width="150" height="112" alt=""/></a>
</div>
<div class="thumbnails">
<a href="gallery/3.jpg" rel="lightbox[gallery]"><img src="gallery/3-thumbnail.jpg" width="150" height="112" alt=""/></a>
</div>
<div class="thumbnails">
<a href="gallery/4.jpg" rel="lightbox[gallery]"><img src="gallery/4-thumbnail.jpg" width="150" height="112" alt=""/></a>
</div>
</div> <!-- End of thumbcontainer div -->
它有效,但它们之间显然有 20(10x2) 个像素左对齐。 我尝试将 thumbcontainer 更改为文本居中对齐但没有做任何事情。
最佳答案
您可以删除 float ,并将各个 .thumbnail div 设置为 display:inline-block。
要使 .thumbnail div 居中,您需要将父元素设置为 text-align:center。
因此您将添加以下内容:
.thumbcontainer {
text-align:center;
}
.thumbnails {
float: none; // or remove float property
display:inline-block;
}
关于css - 在一个 div 容器标签中居中多个嵌套的 div 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18926569/