Sibling divs match height in container

后端 未结 4 1793
情话喂你
情话喂你 2020-12-14 17:24

I have three divs in a container: http://jsfiddle.net/fBe9y/

One div has a lot of content. How do I get the other two divs, with less content, to match

相关标签:
4条回答
  • 2020-12-14 18:10

    You can solve this using flexbox

    .container{
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
        -webkit-flex-wrap: wrap;
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
    }
    
    .tile{
        display: -webkit-flex;
        display: -ms-flexbox;
        display: flex;
    }
    
    0 讨论(0)
  • 2020-12-14 18:12

    This is a very common question. Take a look at this article... it has all the answers:

    http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks

    Now, here's a quick fiddle of putting that to use. Try clicking on any of the "Column #" text elements to remove them from the document... the columns will resize nicely :)

    http://jsfiddle.net/UnsungHero97/qUT3d/9/

    HTML

    <div id="container3">
        <div id="container2">
            <div id="container1">
                <div id="col1">Column 1</div>
                <div id="col2">Column 2</div>
                <div id="col3">Column 3</div>
            </div>
        </div>
    </div>
    

    CSS

    #container3 {
        float:left;
        width:100%;
        background:green;
        overflow:hidden;
        position:relative;
    }
    #container2 {
        float:left;
        width:100%;
        background:yellow;
        position:relative;
        right:30%;
    }
    #container1 {
        float:left;
        width:100%;
        background:red;
        position:relative;
        right:40%;
    }
    #col1 {
        float:left;
        width:26%;
        position:relative;
        left:72%;
        overflow:hidden;
    }
    #col2 {
        float:left;
        width:36%;
        position:relative;
        left:76%;
        overflow:hidden;
    }
    #col3 {
        float:left;
        width:26%;
        position:relative;
        left:80%;
        overflow:hidden;
    }​
    
    0 讨论(0)
  • 2020-12-14 18:18

    HTML

    <div id="container">
      <div id="div1">1</div>
      <div id="div2">2</div>
      <div id="div3">3</div>
    </div>
    

    CSS

    #container {
      display: flex;
      align-items: stretch;
    }
    
    #div1 {
      display: flex;
    }
    
    #div2 {
      display: flex;
    }
    
    #div3 {
      display: flex;
    }
    

    this 'display: flex;' and 'align-items: stretch;' in the container should make all the children div same height, as long as it is of the desired height.

    0 讨论(0)
  • 2020-12-14 18:21

    I recommend using display: table-row; and display: table-cell; for this. In short, what you do is make a table layout, but using <div> tags, and then style them to behave like a table.

    This is better than just using a table for semantic and accessibility reasons.

    But generally speaking, CSS does not give you many ways to refer to an element's siblings this way. The <table> tag does, but then it confuses screen readers and things.

    If you wanted more rows, you would have more .container <div>s, and then create another <div> wrapping them all, and give it display: table;.

    So with the same HTML you had, this CSS does what you want:

    .container
    {
        display: table-row;
    }
    
    .tile
    {
        display: table-cell;
        width: 100px;
        background: #eee;
        border: 1px solid black;
    }​
    

    See Fiddle.

    Of note: while display: table; et al. are widely supported, IE did not add support until version 8. If you plan on supporting this for IE 7 or lower, you'll be forced to use a more complicated approach, like @Hristo's.

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