CSS - Equal Height Columns?

前端 未结 11 1111
情歌与酒
情歌与酒 2020-11-21 06:08

In CSS, I can do something like this:

But I\'ve no idea how to change that to something like:


Is this possible with CSS?

相关标签:
11条回答
  • 2020-11-21 06:38

    Another option is to use a framework that has this solved. Bootstrap currently doesn't have an equal height option but Foundation by Zurb does, and you can see how it works here: http://foundation.zurb.com/sites/docs/v/5.5.3/components/equalizer.html

    Here's an example of how you'd use it:

    <div class="row" data-equalizer>
        <div class="large-6 columns panel" data-equalizer-watch>
        </div>
        <div class="large-6 columns panel" data-equalizer-watch>
        </div>
    </div>
    

    Basically they use javascript to check for the tallest element and make the others the same height.

    So, if you want just css this would add more code, but if you are already using a framework then they have already solved this.

    Happy coding.

    0 讨论(0)
  • 2020-11-21 06:41

    You could use CSS tables, like so:

    <style type='text/css">
        .container { display: table; }
        .container .row { display: table-row; }
        .container .row .panel { display: table-cell; }
    </style>
    <div class="container">
        <div class="row">
            <div class="panel">...text...</div>
            <div class="panel">...text...</div>
            <div class="panel">...text...</div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-21 06:44

    You can do this easily with the following JavaScript:

    $(window).load(function() {
        var els = $('div.left, div.middle, div.right');
        els.height(getTallestHeight(els));
    }); 
    
    function getTallestHeight(elements) {
        var tallest = 0, height;
    
        for(i; i < elements.length; i++) {
            height = $(elements[i]).height();
    
            if(height > tallest) 
                tallest = height;
        }
    
        return tallest;
    };
    
    0 讨论(0)
  • 2020-11-21 06:46

    You ca try it... it works for me and all browser compatible...

    <div id="main" style="width:800px; display:table">
    <div id="left" style="width:300px; border:1px solid #666; display:table-cell;"></div>
    <div id="right" style="width:500px; border:1px solid #666; display:table-cell;"></div>
    </div>
    
    0 讨论(0)
  • 2020-11-21 06:48

    Give overflow: hidden to the container and large (and equal) negative margin and positive padding to columns. Note that this method has some problems, e.g. anchor links won't work within your layout.

    Markup

    <div class="container">
        <div class="column"></div>
        <div class="column"></div>
        <div class="column"></div>
    </div>
    

    CSS

    .container {
        overflow: hidden;
    }
    
    .column {
        float: left;    
        margin-bottom: -10000px;
        padding-bottom: 10000px;
    }
    

    The Result

    0 讨论(0)
  • 2020-11-21 06:51

    Responsive answer:

    CSS flexbox is cute, but cutting out IE9 users today is a little insane. On our properties as of Aug 1 2015:

    3% IE9
    2% IE8

    Cutting those out is showing 5% a broken page? Crazy.

    Using a media query the way Bootstrap does goes back to IE8 as does display: table/table-cell. So:

    http://jsfiddle.net/b9chris/bu6Lejw6/

    HTML

    <div class=box>
        <div class="col col1">Col 1<br/>Col 1</div>
        <div class="col col2">Col 2</div>
    </div>
    

    CSS

    body {
        font: 10pt Verdana;
        padding: 0;
        margin: 0;
    }
    
    div.col {
        padding: 10px;
    }
    
    div.col1 {
        background: #8ff;
    }
    div.col2 {
        background: #8f8;
    }
    
    @media (min-width: 400px) {
        div.box {
            display: table;
            width: 100%;
        }
        div.col {
            display: table-cell;
            width: 50%;
        }
    }
    

    I used 400px as the switch between columns and a vertical layout in this case, because jsfiddle panes trend pretty small. Mess with the size of that window and you'll see the columns nicely rearrange themselves, including stretching to full height when they need to be columns so their background colors don't get cut off part-way down the page. No crazy padding/margin hacks that crash into later tags on the page, and no tossing of 5% of your visitors to the wolves.

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