using flexbox to get pinterest or jQuery masonry layout

前端 未结 4 1703
忘了有多久
忘了有多久 2020-12-16 03:48

Wanted to know if it is possible to get the same type of design layout as pinterest or jQuery masonry using only the new flexbox layout. Here is as far as I got it:

4条回答
  •  心在旅途
    2020-12-16 04:32

    It is entirely possible.

    Thanks to @leopld's original answer, I was able to create one that does not depend on a fixed height.

    By making the flex container position: absolute or position: fixed, you are able to get it to fill the available space dynamically.

    Link to the Codepen: http://codepen.io/anon/pen/Jpnyj?editors=110. I included all the vendor prefixes you'd need at this time.

    Markup

    Styles

    body {
        background: black;
    }
    
    .wrapper {
        position: absolute;
        width: 100%;
        height: 100%;
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-flow: column wrap;
        -ms-flex-flow: column wrap;
        flex-flow: column wrap;
        -webkit-box-align: stretch;
        -webkit-align-items: stretch;
        -ms-flex-align: stretch;
        align-items: stretch;
        -webkit-align-content: stretch;
        -ms-flex-line-pack: stretch;
        align-content: stretch;
    }
    
    .box {
        margin: 5px;
        -webkit-box-flex: 0;
        -webkit-flex: 0 1 auto;
        -ms-flex: 0 1 auto;
        flex: 0 1 auto;
    }
    
    .box-red {
        height: 100px;
        background: red;
    }
    
    .box-blue {
        height: 120px;
        background: blue;
    }
    
    .box-pink {
        height: 144px;
        background: pink;
    }
    
    .box-purple {
        height: 250px;
        background: purple;
    }
    
    .box-green {
        height: 200px;
        background: green;
    }
    
    .box-yellow {
        height: 20px;
        background: yellow;
    }
    
    .box-brown {
        height: 290px;
        background: brown;
    }
    

提交回复
热议问题