How to wrap flexbox over multiple rows and columns?

后端 未结 3 2107
囚心锁ツ
囚心锁ツ 2020-12-18 11:15

I\'m trying to build a gallery in form of a masonry, but I can\'t get my head around wrapping of flexboxes?

I got a simple UL list and I\'ve added the style

3条回答
  •  执笔经年
    2020-12-18 11:36

    Flexbox can't do that with its own layout capabilities, it needs some help, so here is a CSS solution that assume the size of the items is given.

    The main trick here is to add an extra element inside the li and make that the "styled" one, and use the li for the main layout.

    The "light green" items gets a left/right margin, to push them accordingly, and with that make space for the "light purple".

    Since a flex item (here the li) can't dynamically grow both vertical and horizontal, we instead use its inner div to take twice the height, and with that enable the requested layout.

    This setup, combined with Flexbox's order property, will now make it very simply to adjust its layout using e.g. media query for a portrait layout (vertically stacked) etc.


    Note, to make this all dynamically sized by its content, either a script or CSS Grid will be needed.

    Here is a great post shedding some light (and more solutions) on Masonry:

    • CSS-only masonry layout but with elements ordered horizontally

    Stack snippet

    .masonry {
      margin: 48px -2px;
      padding-left: 0;
      list-style: none;
      align-items: flex-start;
      flex-wrap: wrap;
      display: flex;
    }
    
    .masonry li {
      flex-basis: calc(100% / 3);
      height: 200px;
      display: flex;
    }
    
    .masonry li div {
      display: relative;
      flex: 1;
      margin: 2px;
      text-align: center;
      display: flex;
      background-color: #C9F4FF;
    }
    
    .masonry li:nth-child(1) div,
    .masonry li:nth-child(7) div {
      display: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: calc(200% - 4px);              /*  twice the height  */
      background-color: #FFB4FF;
    }
    
    .masonry li:nth-child(4),
    .masonry li:nth-child(8) {
      flex-basis: 100%;                      /*  100% width to force wrap  */
    }
    
    .masonry li:nth-child(4) div,
    .masonry li:nth-child(8) div {
      background-color: #B9EDA8;
    }
    
    .masonry li:nth-child(4) div {
      margin-left: calc((100% / 3) + 2px);   /*  pushed to left  */
    }
    
    .masonry li:nth-child(8) div {
      margin-right: calc((100% / 3) + 2px);  /*  pushed to right  */
    }
    
    
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  

提交回复
热议问题