CSS columns with left-right flow

后端 未结 4 981
攒了一身酷
攒了一身酷 2020-12-03 04:53

Let\'s say I have a div which will contain a set of elements (divs), which may have different heights, but all of them will have the same width.

I\'ve a

相关标签:
4条回答
  • 2020-12-03 05:02

    If your layout is always going to be 3 columns wide, you could try using the nth selector on your internal divs.
    You could do this by clearing your 4th item.

    #container {
        overflow: hidden;
        width: 440px;
    }
    #container div {
        background-color: gray;
        width: 110px;
        margin: 5px;
        float: left;
    }
    #container div:nth-child(4) {
        clear: both;
    }
    <div id="container">
        <div id="widget1">1</div>
        <div id="widget2">2</div>
        <div id="widget3">3</div>
        <div id="widget4">4</div>
        <div id="widget5">5</div>
        <div id="widget6">6</div>
        <div id="widget7">7</div>
        <div id="widget7">8</div>
    </div>

    JS Fiddle

    0 讨论(0)
  • 2020-12-03 05:05

    You can use jQuery to rearrange items in columns. In case of 2 cols it would be:

    $('.your-columns-container .your-item:nth-child(even)').appendTo('.your-columns-container');
    

    https://jsfiddle.net/hzvp7sgf/

    And something more complicated for 3 columns:

    $('.your-columns-container .your-item:nth-child(3n - 1)').addClass('container-col2');
    $('.your-columns-container .your-item:nth-child(3n)').addClass('container-col3');
    
    $('.container-col2').appendTo('.your-columns-container').removeClass('container-col2');
    $('.container-col3').appendTo('.your-columns-container').removeClass('container-col3');
    

    https://jsfiddle.net/d4LeLyu5/

    0 讨论(0)
  • 2020-12-03 05:19

    Here's a link on solution! Used only flex.

    html

    - var n = 0;
    ul
      while n < 40
        li= n++
      li
    
    

    css

    ul {
        list-style: none;
        padding: 30px;
        background: #28285e;
        
        li {
            padding: 20px;
            margin: 6px;
            background: white;
            width: calc(50% - 52px);
            height: 100px;
            display: inline-block;
            border: {
                radius: 4px
            }
            
            &:nth-child(3n) {
                height: 40px;
            }
            
                
            &:nth-child(2n + 1) {
                height: 80px;
            }
            
            &:nth-child(even) {
                float: right;
            }
            
            &:nth-child(odd) {
                float: left;
            }
            
            &:last-child {
                clear: both;
                float: none;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:21

    The multi-column specification offers no property to change the distribution of elements among the columns: http://www.w3.org/TR/css3-multicol/. Such a property seems to go against what the module was designed for (recreating how newspaper or magazine articles are laid out).

    None of the other pure CSS solutions will allow you to achieve the effect you are looking for.

    0 讨论(0)
提交回复
热议问题