如何在垂直空间上均匀填充多个元素

时间:2015-07-02 16:34:44

标签: html css

如果我有以下html / css,我怎样才能使内容元素间隔相等,以便它们占据容器的整个区域 - 包括红色区域。

每个元素之间的填充应该相等。

仅限CSS解决方案。



#container {
  position: absolute;
  width: 100px;
  height: 400px;
  background-color: #f00;
}
.a,
.c {
  background-color: #ccc;
}
.b,
.d {
  background-color: #999;
}

<div id="container">
  <div class="content a">something</div>
  <div class="content b">something else</div>
  <div class="content c">something longer. something longer. something longer. something longer.</div>
  <div class="content d">not a lot</div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

在您的情况下使用display:tabledisplay:table-row会有效吗?

#container {
  position: absolute;
  width: 100px;
  height: 400px;
  background-color: #f00;
  display:table;
}
.content {display:table-row;}
.a,
.c {
  background-color: #ccc;
}
.b,
.d {
  background-color: #999;
}
<div id="container">
  <div class="content a">something</div>
  <div class="content b">something else</div>
  <div class="content c">something longer. something longer. something longer. something longer.</div>
  <div class="content d">not a lot</div>
</div>

答案 1 :(得分:1)

使用flexbox

#container {
  position: absolute;
  width: 100px;
  height: 400px;
  background-color: #f00;
  display: flex;
  flex-direction: column
}
.content {
  flex-grow: 1;
}
.a,
.c {
  background-color: #ccc;
}
.b,
.d {
  background-color: #999;
}
<div id="container">
  <div class="content a">something</div>
  <div class="content b">something else</div>
  <div class="content c">something longer. something longer. something longer. something longer.</div>
  <div class="content d">not a lot</div>
</div>