search

Home  >  Q&A  >  body text

How can I make the bottom Flex column maintain item order using CSS?

I have a horizontally centered Flex item column, sorted from 1 to 5, aligned from the top of the container, like this:

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>

I want it to be aligned with the bottom of the container. I managed to do this using flex-direction: column-reverse; like in the next snippet:

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>

But, as you can see, these projects are malfunctioning! Is there a way to have a Flex column at the bottom without reversing the order of items using CSS? I've tried every Flex property I know of so far with no success.

P粉277464743P粉277464743237 days ago360

reply all(2)I'll reply

  • P粉420958692

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

    You need to use the justify-content attribute to align the content along the main axis (in your case vertical alignment). You are using align-items which defines how items should be aligned along the cross axis.

    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

    reply
    0
  • P粉269530053

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

    You can use 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

    reply
    0
  • Cancelreply