How can I make the bottom Flex column maintain item order using CSS?
P粉277464743
P粉277464743 2024-03-30 11:59:06
0
2
358

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粉277464743
P粉277464743

reply all(2)
P粉420958692

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
P粉269530053

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template