How do I keep two side-by-side divs the same height?

后端 未结 22 2767
自闭症患者
自闭症患者 2020-11-21 07:56

I have two divs side by side. I\'d like the height of them to be the same, and stay the same if one of them resizes. I can\'t figure this one out though. Ideas?

To c

22条回答
  •  孤独总比滥情好
    2020-11-21 08:08

    I was having the same problem so i created this small function using jquery as jquery is part of every web application nowadays.

    function fEqualizeHeight(sSelector) {
        var sObjects = $(sSelector);
    
        var iCount = sObjects.length;
    
        var iHeights = [];
    
        if (iCount > 0) {
            $(sObjects).each(function () {
                var sHeight = $(this).css('height');
                var iHeight = parseInt(sHeight.replace(/px/i,''));
                iHeights.push(iHeight);
            });
    
            iHeights.sort(function (a, b) {
                return a - b
            });
    
            var iMaxHeight = iHeights.pop();
    
            $(sSelector).each(function () {
                $(this).css({
                    'height': iMaxHeight + 'px'
                });
            });
        }
    }
    

    You can call this function on page ready event

    $(document).ready(function(){
       fEqualizeHeight('.columns');
    });
    

    I hope this works for you.

提交回复
热议问题