Make floating divs the same height

前端 未结 5 962
离开以前
离开以前 2020-12-01 16:24

I have 2 divs side by side. I don\'t know the height of them upfront, it changed according to the content. Is there a way to make sure they will always be the same height, e

5条回答
  •  一整个雨季
    2020-12-01 16:45

    Antony answer works ok, but you need all the divs to have the same parent and to have a wrapper, I have a solution that use javascript but works with any kind of element, they just need to have the same selector.

      function setEqualHeight(selector, triggerContinusly) {
    
        var elements = $(selector)
        elements.css("height", "auto")
        var max = Number.NEGATIVE_INFINITY;
    
        $.each(elements, function(index, item) {
            if ($(item).height() > max) {
                max = $(item).height()
            }
        })
    
        $(selector).css("height", max + "px")
    
        if (!!triggerContinusly) {
            $(document).on("input", selector, function() {
                setEqualHeight(selector, false)
            })
    
           $(window).resize(function() {
                setEqualHeight(selector, false)
           })
        }
    
    
    }
    
        setEqualHeight(".sameh", true) 
    

    http://jsfiddle.net/83WbS/2/

提交回复
热议问题