Make floating divs the same height

前端 未结 5 960
离开以前
离开以前 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:38

    I would recommend reading this article that explains how to do what you are trying to do. I would put a fiddle up that shows, but its pretty extensive and pure css. http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks

    0 讨论(0)
  • 2020-12-01 16:39

    You could try instead of using float, use display: table-cell. You might find some older browsers don't understand this rule however. See below:

    #wrapper {
        display: table; // See FelipeAls comment below
        width: 300px;
    }
    
    #left {
        display: table-cell;
        width: 50px;
        background: blue;
    }
    
    #right {
        display: table-cell;
        width: 250px;
        background: red;
    }
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-12-01 16:55

    You can do this without using tables, by using this CSS trick.

    Example - http://jsfiddle.net/LMGsv/

    HTML

       <div id="wrapper">
                <div id="columns">
                    <div id="left">text</div>
                    <div id="right">text<br/>another line<br /></div>
                </div>        
        </div>
    

    CSS

    #wrapper {
        float:left;
        width: 300px;
    }
    #columns {
        float:left;
        width:300px;
        background:blue;
    }
    #left {
        float:left;
        width:50px;
        background: blue;
    }
    #right {
        width:250px;
        background: red;
        float:left
    }
    
    0 讨论(0)
  • 2020-12-01 16:59

    There is a much simpler solution I want to point to. Using large padding-bottom: 500em and negative margin-bottom:-500em of the same amount on columns while the wrapper has simply overflow:hidden to cut the columns to the right size.

    Found here: HTML/CSS: Making two floating divs the same height

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