柔化物品均匀间隔但第一项对齐左侧

dra*_*035 10 css css3 flexbox

Flexbox justify-content: space-around使我的列表项水平均匀分布.有没有办法让完全相同的东西,唯一的区别是左边的第一个项目左边没有空格?(即,列表从容器的左边缘"开始")

Mic*_*l_B 12

而不是使用justify-content: space-around,使用auto项目的边距.

通过给出每个弹性项目margin-right: auto,容器空间将在项目之间均匀分布(例如justify-content),但第一个项目将保留在左边界边缘.

flex-container[one] {
  display: flex;
  justify-content: space-around;
  border: 1px dashed green;
}

flex-container[one]>flex-item {
  background-color: lightgreen;
}

flex-container[two] {
  display: flex;
  border: 1px dashed red;
}

flex-container[two]>flex-item {
  margin-right: auto;
  background-color: orangered;
}

flex-item {
  width: 50px;
  height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
<code>justify-content: space-around</code>
<flex-container one>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
</flex-container>

<br>

<code>margin-right: auto</code>
<flex-container two>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
</flex-container>
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

auto在这里了解有关flex margin的更多信息:在CSS Flexbox中,为什么没有"justify-items"和"justify-self"属性?

  • 太棒了,非常感谢!这对我有用. (3认同)
  • 天才,简直就是天才! (2认同)
  • 好东西。我将相同的原理应用于垂直布局,使用“justify-content: space equally”和“margin-top: auto”,让项目粘在底部并保持响应式、均匀的间距。间隙属性并不适用于所有屏幕尺寸,而且它比必须编写一个带有幻数的特殊规则以及 5 个媒体查询(Flexbox 应该可以解决这个问题!)要好。 (2认同)

Shi*_*127 8

您可以使用justify-content: space-between,但是最后一个内容在右侧也将没有空格。

一个好的文档