.parent {
-webkit-columns: {column width} {number of columns}; /* Chrome, Safari, Opera */
-moz-columns: {column width} {number of columns}; /* Firefox */
columns: {column width} {number of columns};
}
.child {
width: {column width};
}
/* where {column width} is usually fixed size
* and {number of columns} is the maximum number of columns.
* Additionally, to avoid breaks inside your elements, you want to add:
*/
.child {
display: inline-block;
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid-column;
}
<div class="item-list" >
<div class="item-list__item">
Is we miles ready he might going. Own books built put civil fully blind fanny. Projection appearance at of admiration no. As he totally cousins warrant besides ashamed do. Therefore by applauded acuteness supported affection it. Except had sex limits
county enough the figure former add. Do sang my he next mr soon. It merely waited do unable.
</div>
<div class="item-list__item">
Is we miles ready he might going. Own books built put civil fully blind fanny. Projection appearance at of admiration no. As he totally cousins warrant besides ashamed do.
</div>
<div class="item-list__item">
Is we miles ready he might going. Own books built put civil fully blind fanny. Projection appearance at of admiration no. As he totally cousins warrant besides ashamed do. Therefore by applauded acuteness supported affection it. Except had sex limits
</div>
<div class="item-list__item">
Is we miles ready he might going. Own books built put civil fully blind fanny. Projection appearance at of admiration no. As he totally cousins warrant besides ashamed do.
</div>
<div class="item-list__item">
Is we miles ready he might going. Own books built put civil fully blind fanny. Projection appearance at of admiration no. As he totally cousins warrant besides ashamed do. Therefore by applauded acuteness supported affection it. Except had sex limits
</div>
</div>
5 回答
Flexbox是一种“一维”布局系统:它可以沿水平或垂直线对齐项目 .
真正的网格系统是“二维的”:它可以沿水平和垂直线对齐项目 . 换句话说,单元格可以跨越列和行,而flexbox不能这样做 .
这就是Flexbox构建网格的能力有限的原因 . 这也是为什么W3C开发了另一种CSS3技术 Grid Layout (见下文)的原因 .
在具有
flex-flow: row wrap
的Flex容器中,flex项必须换行到新行 .这意味着 a flex item cannot wrap under another item in the same row .
注意上面div#3如何包装在div#1下面,创建一个新行 . 它不能包裹在div#2下面 .
因此,当物品不是行中最高的时,空白区域仍然存在,从而产生难看的间隙 .
图片来源:Jefree Sujit
列包装解决方案
如果切换到
flex-flow: column wrap
,弹性项目将垂直堆叠,并且更容易获得类似网格的布局 . 但是,列方向容器有三个潜在的问题:它水平扩展容器,而不是垂直扩展容器(如Pinterest布局) .
It requires the container to have a fixed height, so the items know where to wrap.
在撰写本文时,它在所有主要浏览器中都存在缺陷the container doesn't expand to accommodate additional columns .
结果,在许多情况下,列方向容器可能不可行 .
其他解决方案
在上面的前两张图片中,考虑将项目2和3包装在一个单独的容器中 . 这个新容器可以是第1项的兄弟 . 完成 .
这是一个详细的例子:Calculator keypad layout with flexbox
一个值得强调的缺点:如果您想使用
order
属性重新排列布局(例如在媒体查询中),此方法可能会消除该选项 .网格布局示例:CSS-only masonry layout but with elements ordered horizontally
你想要的可以通过3种方式实现,CSS明智:
1. flexbox:
.parent {
显示:flex;
弯曲方向:柱;
flex-wrap:wrap;
max-width: / 通常为100%,在相对容器中 /
min-height:; / *我在这里使用vh * /
}
.child {
width:;
显示:块;
}
2. CSS列
(这个解决方案具有内置column-span非常简洁的优点 - 非常方便 Headers ) . 缺点是在列中排序项目(第一列包含项目的前三分之一,依此类推......) . 我为此做了一个jsFiddle .
3.砌体插件
通过JavaScript(砌体插件)计算渲染项目大小后的绝对定位 .
如果您通过
vmin
或vmax
单位设置column-width
并且删除column-count
(第一个片段),则column
方法似乎是一个很好的折衷方案,display:grid
和vmin
也是futur(第二个片段)的选项 .使用vmin进行测试演示
其他链接https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/
display:grid
coud使自动填充也很容易,但需要将 Span 值设置为最高的图像所以行和列可以叠加你可以看到https://css-tricks.com/snippets/css/complete-guide-grid/
您可以根据屏幕截图实现砌体效果,但是您已动态设置外部div的高度
而不是
flexbox
,我建议使用columns这样的网格 . 如您所见,底部图像的间距可以更好,但对于原生CSS解决方案,我认为它非常整洁 . 没有更多JS: