html - 垂直均匀分布列表项

标签 html css

我正在尝试使用 CSS 找到一种在垂直方向上均匀分布列表项的方法。

我希望每个列表项都有一个固定的高度,不会改变。我希望每个列表项之间的边距自动拉伸(stretch),因此它具有相同的空间量但 margin:auto;不工作。

这是片段:

.container {
  border: 1px solid black;
  height: 500px;
  width: 400px;
}

.spaced {
  padding: 0;
  list-style: none;
}
.spaced li {
  border: 1px solid blue;
  height: 60px;
  margin: 15px;
}
<div class="container">
  <ul class="spaced">
    <li>item </li>
    <li>item </li>
    <li>item </li>
    <li>item </li>
  </ul>
</div>

所以对于这个片段,我需要它,这样蓝色框将保持相同的高度并且在垂直方向上均匀分布。如果黑框的高度发生变化,那么蓝框仍将均匀分布。

我该怎么做?

最佳答案

您可以使用 Flexboxjustify-content: space-around; 来做到这一点

Flex items are evenly distributed so that the space between two adjacent items is the same. The empty space before the first and after the last items equals half of the space between two adjacent items.

.container {
  border: 1px solid black;
  height: 500px;
  width: 400px;
  padding: 10px;
  box-sizing: border-box;
}

.spaced {
  height: 100%;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  margin: 0;
}
.spaced li {
  border: 1px solid blue;
  height: 60px;
}
<div class="container">
  <ul class="spaced">
    <li>item </li>
    <li>item </li>
    <li>item </li>
    <li>item </li>
  </ul>
</div>

关于html - 垂直均匀分布列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34841488/

相关文章:

html - 多列列表可以有单独的 flex 列吗?

javascript - 实现 jQuery 下拉效果

jquery - 如何优化 jquery 数据表以有效加载大数据(10k-50k 行)?

javascript - 变量中的横向 DOM

javascript - 隐藏没有类别的元素

javascript - 选项卡 - 需要在页面加载时隐藏内容并在单击时显示内容

javascript - 加载动态包含的静态资源?

javascript - 显示/隐藏 div 取决于 AngularJS 中默认值的下拉选择

html 2 div容器定位

html - 使用宽度使两列相等