Search code examples
htmlcssblockvertical-alignmenttablecell

Align each block vertically in table-cell


I have 1 parent table-cell .t and 2 child blocks .ch1 and .ch2:

HTML:

<div class="t">
    <div class="ch1">1</div>
    <div class="ch2">2</div>
</div>

CSS:

.t {
    display: table-cell;
    background-color:green;
    height:200px;
    width:200px;
}

.ch1 {
    background-color:blue;
    display: block;
}

.ch2 {
    background-color:red;
    display: block;
}

Is it possible to push .ch2 to bottom, but left .ch1 on top (if use vertical-align: bottom; of .t it will push both blocks to bottom)

JSFiddle: https://jsfiddle.net/hvz4cn69/


Solution

  • Flexbox can do that:

    .t {
      display: flex;
      flex-direction: column;
      background-color: green;
      height: 200px;
      width: 200px;
      justify-content: space-between;
    }
    .ch1 {
      background-color: blue;
      display: block;
    }
    .ch2 {
      background-color: red;
      display: block;
    }
    <div class="t">
      <div class="ch1">1</div>
      <div class="ch2">2</div>
    </div>

    Or

    .t {
      display: flex;
      flex-direction: column;
      background-color: green;
      height: 200px;
      width: 200px;
    }
    .ch1 {
      background-color: blue;
      display: block;
    }
    .ch2 {
      background-color: red;
      display: block;
      margin-top: auto;
    }
    <div class="t">
      <div class="ch1">1</div>
      <div class="ch2">2</div>
    </div>