纵向证明内容

Roc*_*160 4 html css sass css3 flexbox

希望这不是一个尚未解决的任务,但我正在尝试垂直证明容器内div数量未知(ish)。

每个div彼此之间的距离应相等,并且距边缘的距离应相同。(假设最后一部分可以使用之前和之后的ghost元素完成)

每个div都将填充容器的宽度,并且容器为设定的高度,但是容器内的元素数未知。

我假设可以在一定程度上使用Flexbox完成此操作,但到目前为止,我的尝试均未成功。

小智 8

是的,flexbox是最简单的方法。

在容器元素上:

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
Run Code Online (Sandbox Code Playgroud)

在子元素上:

.container div {
  flex: 1;
  width: 100%
}
Run Code Online (Sandbox Code Playgroud)

对于元素之间的间距,只需在容器中添加填充,并在子级中添加底边距。

样式如下所示:

.container {
  /* Same as above, and */
  padding: 20px;
}

.container div {
  /* Same as above, and */
  margin-bottom: 20px;
}

.container div:last-of-type{
  margin-bottom: 0;
  /* So that spacing is even at bottom and top of container */
}
Run Code Online (Sandbox Code Playgroud)

(当您发布答案时,我正在输入此字,因此无论如何我都提出了它)

小提琴


Roc*_*160 -1

像往常一样,无论我搜索多久,我在提出问题后都会立即找到答案。:D

对于那些好奇的人,或者为了我自己的未来参考:Flexbox 的 justify 确实有效,您只需要更多选项:

HTML:

<div id="outer-container">
  <div class="inner-element"></div>
  <div class="inner-element"></div>
  <div class="inner-element"></div>
  <div class="inner-element"></div>
  <div class="inner-element"></div>
  <div class="inner-element"></div>
  <div class="inner-element"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#outer-container {
  height: 250px;
  width: 200px;
  background: red;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
}

.inner-element {
  width: 200px;
  height: 10px;
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)

https://css-tricks.com/almanac/properties/j/justify-content/

https://jsfiddle.net/WW3bh/