在 Flex 中,当 flex 方向为 row-reverse 时将项目对齐到左侧

21

我想水平反向显示项目,并左对齐。

这是我的实现示例,但项目被右对齐了。

#main {
    width: 400px;
    height: 400px;
    border: 1px solid #c3c3c3;
    display: flex;
    flex-direction: row-reverse;
    align-items: flex-start;
}

#main div {
    width: 50px;
    height: 50px;
}
<div id="main">
  <div style="background-color:coral;">A</div>
  <div style="background-color:lightblue;">B</div>
  <div style="background-color:khaki;">C</div>
  <div style="background-color:pink;">D</div>
  <div style="background-color:lightgrey;">E</div>
  <div style="background-color:lightgreen;">F</div>
</div>

有人可以帮我吗,先谢谢了。


3
你需要的是 #main { justify-content: flex-end; } - misorude
1个回答

31
你需要指定justify-content: flex-end;

#main {
  width: 400px;
  height: 400px;
  border: 1px solid #c3c3c3;
  display: flex;
  flex-direction: row-reverse;
  align-items: flex-start;
  justify-content: flex-end;
}

#main div {
  width: 50px;
  height: 50px;
}
<div id="main">
  <div style="background-color:coral;">A</div>
  <div style="background-color:lightblue;">B</div>
  <div style="background-color:khaki;">C</div>
  <div style="background-color:pink;">D</div>
  <div style="background-color:lightgrey;">E</div>
  <div style="background-color:lightgreen;">F</div>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接