331

This question concerns a browser with full css3 support including flexbox.

I have a flex container with some items in it. They are all justified to flex-start but I want the last .end item to be justified to flex-end. Is there a good way to do this without modifying the HTML and without resorting to absolute positioning?

.container {
  display: flex;
  flex-direction: column;
  outline: 1px solid green;
  min-height: 400px;
  width: 100px;
  justify-content: flex-start;
}
p {
  height: 50px;
  background-color: blue;
  margin: 5px;
}
<div class="container">
  <p></p>
  <p></p>
  <p></p>
  <p class="end"></p>
</div>

2

2 Answers 2

676

Flexible Box Layout Module - 8.1. Aligning with auto margins

Auto margins on flex items have an effect very similar to auto margins in block flow:

  • During calculations of flex bases and flexible lengths, auto margins are treated as 0.

  • Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.

Therefore you could use margin-top: auto to distribute the space between the other elements and the last element.

This will position the last element at the bottom.

p:last-of-type {
  margin-top: auto;
}

.container {
  display: flex;
  flex-direction: column;
  border: 1px solid #000;
  min-height: 200px;
  width: 100px;
}
p {
  height: 30px;
  background-color: blue;
  margin: 5px;
}
p:last-of-type {
  margin-top: auto;
}
<div class="container">
  <p></p>
  <p></p>
  <p></p>
</div>

vertical example


Likewise, you can also use margin-left: auto or margin-right: auto for the same alignment horizontally.

p:last-of-type {
  margin-left: auto;
}

.container {
  display: flex;
  width: 100%;
  border: 1px solid #000;
}
p {
  height: 50px;
  width: 50px;
  background-color: blue;
  margin: 5px;
}
p:last-of-type {
  margin-left: auto;
}
<div class="container">
  <p></p>
  <p></p>
  <p></p>
  <p></p>
</div>

horizontal example

Sign up to request clarification or add additional context in comments.

12 Comments

ah perfect, can't believe I missed that! Especially embarrassing since I totally know about the using-margin-auto-to-center-vertically trick and use it all the time
@j08691 I don't have a formal explanation as to why it works, but it's similar to how you can use margin: 0 auto for horizontal centering. Flexbox items respect the margin, and I believe that margin: auto will essentially position the item with as much space between the sibling as possible. See: w3.org/TR/css-flexbox-1/#auto-margins
@jo8691 if I'm not mistaken, margin: auto on a flex item tells it to create as much space as possible in that direction.
The spec says Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.
I'm trying to learn more about flexbox so it would be great to see a reference that describes this behavior. Particularly that it's also intended and not a quirk or side effect.
You can check out these two MDN articles for an "official" documentation of the behavior. developer.mozilla.org/en-US/docs/Web/CSS/CSS_box_alignment/… developer.mozilla.org/en-US/docs/Web/CSS/Layout_cookbook/…
|
111

This flexbox principle also works horizontally

During calculations of flex bases and flexible lengths, auto margins are treated as 0.
Prior to alignment via justify-content and align-self, any positive free space is distributed to auto margins in that dimension.

Setting an automatic left margin for the Last Item will do the work.

.last-item {
  margin-left: auto;
}

Code Example:

.container {
  display: flex;
  width: 400px;
  outline: 1px solid black;
}

p {
  height: 50px;
  width: 50px;
  margin: 5px;
  background-color: blue;
}

.last-item {
  margin-left: auto;
}
<div class="container">
  <p></p>
  <p></p>
  <p></p>
  <p class="last-item"></p>
</div>

Codepen Snippet

This can be very useful for Desktop Footers.

As Envato did here with the company logo.

Codepen Snippet

3 Comments

Unsure why this actually works, but it helped me on a project.
What if I need that all items will be in center and the last one at the right side?
There's a variation of this but with centered items: stackoverflow.com/a/59405567/2394994

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.