Align each block vertically in table-cell

佐手、 提交于 2019-12-11 10:41:37

问题


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



来源:https://stackoverflow.com/questions/35221068/align-each-block-vertically-in-table-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!