Align each block vertically in table-cell

2019-08-14 22:07发布

问题:

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/

回答1:

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>



回答2:

You may use margin-top: 164px; for ch2