CSS: Ignoring divs height when floating

后端 未结 3 877
灰色年华
灰色年华 2021-02-14 04:39

I\'m trying to display some pictures. All of them have the same width but different height. I\'m trying to do something like:

相关标签:
3条回答
  • 2021-02-14 05:02

    Sadly, you can't do this with CSS2/3.

    There is jQuery Masonry, a fairly small script that emulates float: top; and works well.

    0 讨论(0)
  • 2021-02-14 05:08

    You can do this with no extra markup with CSS3 column-count, assuming you at least have a single containing element.

    Demo: http://jsfiddle.net/ThinkingStiff/NcxPr/

    HTML:

    <div id="container">
    <img class="image" src="http://farm1.staticflickr.com/205/494701000_744cc3a83a_z.jpg" />
    <img class="image" src="http://farm5.staticflickr.com/4028/4287569889_f6a4fca31b_z.jpg" />
    <img class="image" src="http://farm3.staticflickr.com/2340/2421926504_d8509d0a98_z.jpg" />
    <img class="image" src="http://farm1.staticflickr.com/197/503792921_fedf8ba47e_z.jpg" />
    <img class="image" src="http://farm2.staticflickr.com/1153/741035029_f394e11a1f_z.jpg" />
    <img class="image" src="http://farm7.staticflickr.com/6213/6243090894_8b8dd862cd_z.jpg" />
    <img class="image" src="http://farm2.staticflickr.com/1339/1157653249_dbcc93c158_z.jpg?zz=1" />
    <img class="image" src="http://farm3.staticflickr.com/2570/4220856234_029e5b8348_z.jpg?zz=1" />
    </div>
    

    CSS:

    #container {
        column-count: 3;
        column-fill: balance;
        column-gap: 10px;
        width: 330px;
    }
    
    .image { 
        display: block;
        margin-bottom: 10px;
        width: 100px;
    } 
    

    Output:

    enter image description here

    0 讨论(0)
  • 2021-02-14 05:10

    Are you able to add some extra markup?

    If yes, you could make 3 columns with floating divs:

    <div class="column">
        <img src="1.jpg" />
        <img src="4.jpg" />
    </div>
    
    <div class="column">
        <img src="2.jpg" />
        <img src="5.jpg" />
    </div>
    
    <div class="column">
        <img src="3.jpg" />
        <img src="6.jpg" />
    </div>
    

    Or just use jQuery to do that for you.

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