首页  >  问答  >  正文

如何使用 CSS 让底部的 Flex 列保持项目顺序?

我有一个水平居中的 Flex 项目列,从 1 到 5 排序,从容器顶部对齐,如下所示:

body, html {
  height: 100%;
  position: relative;
  margin: 0;
  padding: 0;
}
.container {
  display: inline-flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-items: flex-end;
  align-content: center;
  width: 100%;
  height: 100%;
  background: pink;
}
.item {
  margin: 1px;
  width: 30px;
  height: 30px;
  background: green;
}
<div class=container><div class=item>1</div><div class=item>2</div><div class=item>3</div><div class=item>4</div><div class=item>5</div></div>

我想让它与容器底部对齐。我设法使用 flex-direction: column-reverse; 来做到这一点,就像下一个片段中一样:

body, html {
  height: 100%;
  position: relative;
  margin: 0;
  padding: 0;
}
.container {
  display: inline-flex;
  flex-wrap: wrap;
  flex-direction: column-reverse;
  align-items: flex-end;
  align-content: center;
  width: 100%;
  height: 100%;
  background: pink;
}
.item {
  margin: 1px;
  width: 30px;
  height: 30px;
  background: green;
}
<div class=container><div class=item>1</div><div class=item>2</div><div class=item>3</div><div class=item>4</div><div class=item>5</div></div>

但是,正如您所看到的,这些项目出现了故障! 有没有办法让 Flex 列位于底部,而无需使用 CSS 反转项目顺序?我尝试了迄今为止我所知道的每个 Flex 属性,但没有成功。

P粉277464743P粉277464743194 天前303

全部回复(2)我来回复

  • P粉420958692

    P粉4209586922024-03-31 10:59:39

    您需要使用 justify-content 属性沿主轴对齐内容(在您的情况下是垂直对齐)。您正在使用 align-items ,它定义了项目应如何沿交叉轴对齐。

    body, html {
      height: 100%;
      position: relative;
      margin: 0;
      padding: 0;
    }
    .container {
      display: inline-flex;
      flex-wrap: wrap;
      flex-direction: column;
      justify-content: flex-end;
      align-content: center;
      width: 100%;
      height: 100%;
      background: pink;
    }
    .item {
      margin: 1px;
      width: 30px;
      height: 30px;
      background: green;
    }
    1
    2
    3
    4
    5

    回复
    0
  • P粉269530053

    P粉2695300532024-03-31 10:25:56

    您可以使用 justify-content: end;

    .container {
      width: 150px;
      height: 150px;
      border: 1px solid black;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: end;
    }
    
    .content {
      width: 25px;
      height: 25px;
      border: 1px solid black;
    }
    1
    2
    3
    4
    5

    回复
    0
  • 取消回复