How to make divs stack without spaces and retain order on smaller sizes - in Bootstrap

后端 未结 7 1048
不思量自难忘°
不思量自难忘° 2021-01-18 02:47

I really don\'t know how to put this in words.

When I have 2 divs in a row, each has different height, so the next row have unwanted space.

But stack correct

7条回答
  •  被撕碎了的回忆
    2021-01-18 03:39

    Revised answer

    I have sorted out some code with the help of this answer https://stackoverflow.com/a/32565224/3956566

    I have used a small width to demonstrate it, that is for you to customise. Also put any changes in css in a site.css file, not bootstrap and call the site.css after the bootstrap, so changes are overridden.

    Css:

    div {outline:solid 1px red; }
    
      .left {
     } 
      .right {
     }  
    

    Html:

    DIV 1
    DIV 2
    DIV 3
    DIV 4
    DIV 5
    DIV 6
    DIV 7
    DIV 8

    JQuery:

    if ($(window).width() > 400) {
        $('#div1').append($('.left'));
        $('#div2').append($('.right'));
    }
    if ($(window).width() < 400) {
        $('#div3').append($('#1'));
        $('#div3').append($('#2'));
        $('#div3').append($('#3'));
        $('#div3').append($('#4'));
        $('#div3').append($('#5'));
        $('#div3').append($('#6'));
        $('#div3').append($('#7'));
        $('#div3').append($('#8'));
    }
    
    
    $('#div1').append($('.left'));
    $('#div2').append($('.right'));
    
    $( window ).resize(function() {
        if($(window).width()>400){
            $('#div1').append($('.left'));
            $('#div2').append($('.right'));  
        }
        if($(window).width()<400)
        {
            // These need to be added in order, as appending them to the
            // first two divs reorders them.
            $('#div3').append($('#1'));
            $('#div3').append($('#2'));
            $('#div3').append($('#3'));
            $('#div3').append($('#4'));
            $('#div3').append($('#5'));
            $('#div3').append($('#6'));
            $('#div3').append($('#7'));
            $('#div3').append($('#8'));
        }
    });
    

    See fiddle:

    https://jsfiddle.net/kermit77/vo439fte/4/


    First answer

    Beyond putting in calculations determining individual div height and then adjusting margins accordingly (for the larger layouts only!);
    i.e.

    div 1 height = a, div 2 height = b

    div diff = (a-b)

    if diff >0

    div 4 top margin = -diff

    else

    div 3 top-margin = -diff;

    But you would need to keep a running total of odd numbered div heights and even numbered div heights and adjust accordingly.

    I've trimmed down the widths to test it. You need to clear left on every 3rd div to prevent it being placed on the right hand side.

    DIV 1
    DIV 2
    DIV 3
    DIV 4

    For every 3rd div clear left.

    css:

    div {outline:solid 1px red; }
    .row {
        margin-right: 2px;
        margin-left: 2px;
    }
    @media (min-width: 500px) {
    .container {
        max-width: 1170px;
    }
    .col-lg-6 {
        max-width:50%;
        width: 50%;
        float:left;
        clear:right;
    }  
    }
    @media (min-width: 150px) {
    .container {
        max-width: 500px;
    }
    .col-sm-12 {
        width: 100%;
    }
    }
    

提交回复
热议问题