【发布时间】:2016-02-12 22:07:08
【问题描述】:
当使用 flex 创建图像网格时,如何将网格本身水平居中在页面上?我仍然希望图像在每一行上左对齐。我希望它对每行的元素数量是动态的。
Jsfiddle with what I have so far
.gallery {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-content: flex-start;
align-items: flex-start;
}
.gallery-artwork {
width: 400px;
display: flex;
margin: 10px;
}
<div class="gallery">
<div class="gallery-artwork">
<img src="http://placehold.it/400x225">
</div>
<div class="gallery-artwork">
<img src="http://placehold.it/400x225">
</div>
<div class="gallery-artwork">
<img src="http://placehold.it/400x225">
</div>
<div class="gallery-artwork">
<img src="http://placehold.it/400x225">
</div>
<div class="gallery-artwork">
<img src="http://placehold.it/400x225">
</div>
</div>
【问题讨论】:
-
基本上......很难......通常需要JS或隐藏的虚拟元素。
-
我不确定这里是否真的需要详细的规范答案。 Flexbox 不是为网格设计的。它旨在通过分配空白空间来实现更灵活的布局。对于网格,W3C 正在开发CSS Grid Layout Module。在它获得浏览器支持之前,您可以使用Masonry。