Same height column bootstrap 3 row responsive

后端 未结 16 1705
难免孤独
难免孤独 2020-11-30 00:04

Hi I have four divs in a bootstrap row. I want all divs in this row to have the same height and not break responsibility. I don\'t know how to do this without breaking respo

相关标签:
16条回答
  • 2020-11-30 00:26

    paulalexandru has a great answer that I used to solve my issue. However it doesn't address the problem that occurred for me when I resized the window. Sometimes the text would extend past the height specified and bleed into text in the row below.

    So I added a second function that would detect the window resize, reset the height to auto and then set the height on the div if greater than 967. The 967 number can be changed to whatever number you wish to use when responsive brings everything down to just one column.

    $(window).resize(function () {
    
            // reset height to auto.
            $(".div-name1").height("auto");  
            $(".div-name1").height("auto");            
    
            // check if the width is greater than 967 under 967 responsive switches to only 1 column per row
            if ($(window).width() > 967) {
                // resize the height
                myresize1(".row-name1", ".div-name-row1");
                myresize1(".row-name2", ".div-name-row2");
             }
    
                function myresize1(name1, name2) {
                    $(name1).each(function () {
                        var heights = $(this).find(name2).map(function () {
                            console.log($(this).height() + " - ");
                            return $(this).height();
                        }).get(),
    
                        maxHeight = Math.max.apply(null, heights);
                        console.log(maxHeight + " - ");
                        $(name2).height(maxHeight);
                    });
                };            
        });
    
    0 讨论(0)
  • 2020-11-30 00:29

    Bootstrap's experiment, as mentioned by @cvrebert, works for me on Microsoft Internet Explorer (version 11) and Mozilla Firefox (version 37), but does not work for me on Google Chrome (version 42, 64-bit).

    Inspired by @cvrebert's and @Maxime's answers, I came up with this solution, that works for me on those three browsers. I decided to share it, so maybe others can use it too.

    Suppose you have something like this in your HTML:

    <div class="row row-eq-height">
        <div class="col-eq-height col-xs-3">
            Your content
        </div>
        <div class="col-eq-height col-xs-3">
            Your content
        </div>
        <div class="col-eq-height col-xs-3">
            Your content
        </div>
        <div class="col-eq-height col-xs-3">
            Your content
        </div>
    </div>
    

    Add this to your JS:

    $(document).ready(function() {
        $(".row-eq-height").each(function() {
            var heights = $(this).find(".col-eq-height").map(function() {
                return $(this).outerHeight();
            }).get(), maxHeight = Math.max.apply(null, heights);
    
            $(this).find(".col-eq-height").outerHeight(maxHeight);
        });
    });
    

    It uses jQuery's .outerHeight() function to calculate and set the inner div's heights.

    Also, I added this to my CSS, I'm not sure if this helps, but it surely does not hurt:

    .row-eq-height {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
    }
    
    0 讨论(0)
  • 2020-11-30 00:31

    For all the div's to have the same height you have to specfy a fixed height for the inner div (div class="well"),

    CSS

    .index_div_item{
        display: block;
        height: 400px;
    }
    
    .index_div_item a .well{
        height: 400px;
    }
    

    You can follow this link demo for example.

    0 讨论(0)
  • 2020-11-30 00:33

    FYI - I did this to solve my issue to include responsive breakpoints which match the breakpoints in bootstrap. (I use LESS variables from bootstrap to synchronise the breakpoints but below is the compiled css version)

    @media (min-width: 0px) and (max-width: 767px) {
      .fsi-row-xs-level {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
      }
    }
    @media (min-width: 768px) and (max-width: 991px) {
      .fsi-row-sm-level {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
      }
    }
    @media (min-width: 992px) and (max-width: 1199px) {
      .fsi-row-md-level {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
      }
    }
    @media (min-width: 1200px) {
      .fsi-row-lg-level {
        display: -webkit-box;
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
      }
    }
    

    then added the classes to the parent which were required.

    <div class="row fsi-row-lg-level fsi-row-md-level">
    <div class="col-sm-4">column 1</div>
    <div class="col-sm-4">column 2</div>
    <div class="col-sm-4">column 3</div>
    </div>
    
    0 讨论(0)
  • 2020-11-30 00:33

    I had same kind of situation wherein each column within a row should be of same height. By considering above answers and with little trick, it was easy to implement and resolve the issue of window resize also.

    $(document).ready(function() {
        //Initiate equalize on load
        equalize();
    });
    
    //Equalize on resizing of window
    $(window).resize(function() {
        removeHeights();
        equalize();
    });
    
    function equalize(){
    
        $(".container .row").each(function() {
            var heights = $(this).find("p").map(function() {
              return $(this).height();
            }).get(),
    
            maxHeight = Math.max.apply(null, heights);
    
          $(this).find("p").height(maxHeight);
    
        });
    }
    
    function removeHeights(){
    
        $(".container .row").each(function() {
    
          $(this).find("p").height("auto");
    
        });
    }
    

    Check the demo here - https://jsfiddle.net/3Lam7z68/ (Resize the output pane to see the each div column size's changing)

    0 讨论(0)
  • 2020-11-30 00:34

    I'm a bit late to the game, but....

    This will align the heights of items in a row with a class of "row-height-matched". It matches the cells based on their top value so if they move underneath each other when the page gets smaller, I don't set the height.

    I know there are a number of CSS versions out there, but all of them break my layout design.

    $(window).resize(function () {
        var rows = $(".row.row-height-matched");
    
        rows.each(function () {
            var cells = $(this).children("[class^='col-']");
    
            cells.css("height", "");
    
            var j = [];
    
            cells.each(function () {
                j.push($(this).offset().top);
            });
    
            var u = $.unique($(j));
    
            u.each(function () {
                var top = $(this)[0];
    
                var topAlignedCells = cells.filter(function () {
                    return $(this).offset().top == top;
                })
    
                var max = 0;
    
                topAlignedCells.each(function () {
                    max = Math.max(max, $(this).height());
                })
    
                topAlignedCells.filter(function () {
                    return $(this).height() != max;
                }).height(max);
            })
    
        })
    })
    
    0 讨论(0)
提交回复
热议问题