Align 2 DIV per line, both the with the same height

前端 未结 4 1774
别那么骄傲
别那么骄傲 2021-01-26 20:40

We have a problem where we need to have a list of divs with dynamic content. There will always be 2 divs per row. Both of those elements should have the same height.

Cur

相关标签:
4条回答
  • 2021-01-26 21:02

    Have a look at this fiddle You can use display:table-cell;

    CSS:

    .row {
        display: table;
        width: 100%;
    }
    .left {
        width:50%;
        background: blue;
        display:table-cell;
    }
    .right {
        width:50%;
        background: red;
        display:table-cell;
    }
    

    HTML

    <div class='row'>
        <div class='left'>
    
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    
        </div>
        <div class='right'>
    
    "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos
    
        </div>    
    </div>
    
    0 讨论(0)
  • 2021-01-26 21:09

    You can use a flex model for this:

    .boxes .box 
    {
        margin-left: 2%;
        margin-bottom: 2%;
        width: 50%;
        padding: 4%;
        border: 1px solid #b6b6b6;
        border: 0.0625rem solid #b6b6b6;
        box-sizing: border-box;
    }
    
    .box-wrapper
    {
        width: 100%;
        display: -webkit-box;
        display: -moz-box;
        display: -webkit-flexbox;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
    }
    

    jsFIddle

    This way every row will be the height of the highest child element. However the support for this is limited.

    So if you rather not use this method you can transform you structure in a table structure. This way every row will be the height of the highest child element.

    .boxes .box 
    {
        display: table-cell;
        margin-left: 2%;
        margin-bottom: 2%;
        width: 50%;
        padding: 4%;
        border: 1px solid #b6b6b6;
        border: 0.0625rem solid #b6b6b6;
        box-sizing: border-box;
    
    }
    
    .box-wrapper
    {
        display: table-row;
    }
    
    .boxes
    {
        display: table;
        border-collapse: separate;
        border-spacing: 5px;
    }
    

    Because margin doesn't work between table-cells i used border-spacing to define the seperation between the cells.

    jsFiddle

    0 讨论(0)
  • 2021-01-26 21:09

    I use flexbox, it's magic ^^ :

    HTML

    <div class="boxes">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
    

    CSS

    .boxes {
      display: flex;
      flex-wrap: wrap;
    }
    .box {
       margin: 0 1% 1% 0;
       width: 48%;
    }
    

    CODEPEN DEMO

    Complete guide to flexbox on css-trick

    0 讨论(0)
  • 2021-01-26 21:12

    Try this

    .boxes .box {
        float: left;
        margin-left: 2%;
        margin-bottom: 2%;
        width: 38%;
        padding: 4%;
        border: 1px solid #b6b6b6;
        border: 0.0625rem solid #b6b6b6;
        box-sizing: border-box;
        display:inline-block
    }
    .box-wrapper,.boxes{
            display:inline-block;
            width:100%;
    }
    

    Fiddle

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