Masonry Layout in CSS3?

后端 未结 5 1476
星月不相逢
星月不相逢 2020-12-06 14:47

I try to use the following structure to generate the layout described below.

child 1
5条回答
  •  情歌与酒
    2020-12-06 15:21

    Flexbox cannot be used to recreate the Masonry layout. Period.

    Flexbox is for controlling how elements flow along either a horizontal row (flex-direction: row, which is the default) or vertical column (flex-direction: column). That means you can only eliminate excess space in one direction: left/right (row) or top/bottom (column). Because flex-direction: column requires an explicit height to enable wrapping, it is entirely unsuitable for this purpose.

    The CSS Multi-column Layout Module is the closest you can get to recreating a Masonry layout using pure CSS, but it still only allows you to eliminate excess space between the elements in one direction: vertically. The key difference between this and Flexbox (using the column direction) is that the Multi-Column module does not require an explicit height and will distribute the contents equally between each of the columns as best it can (this can be controlled via the column-fill property). The gap between the columns is controlled by the column-gap property.

    http://codepen.io/cimmanon/pen/CcGlE

    .my-element {
      -moz-columns: 15em;
      -webkit-columns: 15em;
      columns: 15em;
    }
    

提交回复
热议问题