Align Self and Order – Controlling Individual Flex Items
Quick Answer: Use align-self on a single flex item to override the container's align-items for that item on the cross axis. Its values are auto (the default, which follows the container), stretch, flex-start, flex-end, center and baseline. Use order to change where an item appears. Items are shown from the lowest order value to the highest, the default is 0, and items with equal values keep their HTML order. order changes the visual order only.
S1. Why Do We Need Item-Level Control?
Imagine the navigation bar from the previous article. It is 80px tall, and align-items: center keeps the logo, the menu and the login button in the vertical middle.
Now the design changes. The login button should sit at the bottom edge of the bar, while the other two stay centered. On small screens, the login button should also appear first.
Would you take the button out of the bar and position it by hand? That works for one screen, but it breaks as soon as the layout changes.
CSS Flexbox solves this with properties on the item itself. Let's first see what happens when we only have container rules.
S2. The Problem: One Rule for Every Item
Item-level control = a rule that applies to one flex item, instead of to the whole container.
Imagine the 360px wide and 80px tall bar with three items, each 120px wide and 40px tall. The container uses align-items: center, and the HTML order is Logo, Menu, Login.
Example 1: A bar where every item follows the container
Assumption: In the output diagrams, one character is about 20px wide and one row is about 20px tall. A, B and C stand for Logo, Menu and Login.
<!-- HTML -->
<nav class="bar">
<div class="bar-item">Logo</div>
<div class="bar-item">Menu</div>
<div class="bar-item">Login</div>
</nav>
/* CSS */
.bar {
display: flex;
align-items: center;
width: 360px;
height: 80px;
}
.bar-item {
width: 120px;
height: 40px;
background: #E2E8F0;
}
Expected output
| | |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | | All three items are centered, in the HTML order A, B, C. The container rule gives Login no way to be different.
The page renders, but it is not the bar we wanted. Container rules treat every item in the same way.
What goes wrong?
- No exceptions: A container rule such as
align-itemsapplies to every item, so one item cannot differ. - Margin hacks: Developers add a large top margin to one item to push it down, using numbers that depend on the bar height.
- Extra containers: The odd item is moved into a separate wrapper, which adds markup.
- Edited HTML order: To show an item first, developers rewrite the HTML or duplicate content for each screen size.
- Poor clarity: The CSS does not say that one item is special.
Now think about many toolbars, page layouts and card rows on one website. Repeating hacks for each special item becomes harder to maintain.
Remember: The container is not broken. It gives the same rule to every item. The real problem is that nobody told one item to behave differently.
Animation 1: How one rule for all items creates workarounds
Three items, one container rule
The red highlights show every item forced to follow the same rule.
The animation highlights the container rule that every item must follow.
S3. The Solution: Override One Item with align-self and order
Flexbox gives us a simpler design.
align-self = a property on a flex item that overrides the container's align-items for that item.
order = a property on a flex item that sets its position in the visual sequence of items.
Instead of working around the container, add rules to the one item that needs to differ.
align-self: flex-endputs this item at the end of the cross axis, and the others keep the container rule.order: -1moves this item before every item whose order is 0 or higher.- The other items, and the HTML, stay exactly as they were.
Now Login sits at the bottom edge and comes first, while Logo and Menu stay centered.
Example 2: One item with its own rules
Assumption: This example uses standard CSS supported by all modern browsers, and assumes a row direction in a left-to-right language.
<!-- HTML (only the third item changes) -->
<div class="bar-item login">Login</div>
/* CSS */
.bar {
display: flex;
align-items: center;
width: 360px;
height: 80px;
}
.bar-item {
width: 120px;
height: 40px;
background: #E2E8F0;
}
.login {
align-self: flex-end;
order: -1;
}
Expected output
| | | AAAAAABBBBBB| |CCCCCCAAAAAABBBBBB| |CCCCCC | Login (C) is drawn first and sits at the bottom edge. Logo (A) and Menu (B) stay centered. The HTML order is still Logo, Menu, Login.
Notice the difference. The container rule stays simple, and one class on one item makes the exception.
Animation 2: The item-level design
One container rule, one exception
Most items follow the container, and one item has rules of its own.
The animation highlights the container rule and the item that overrides it.
Compare both approaches
| Container rules only | With align-self and order |
|---|---|
| Every item follows the same alignment. | One item can override the alignment. |
| Exceptions need margin hacks. | Exceptions are one declaration. |
| Reordering means editing the HTML. | Reordering is done in CSS. |
| Different screen sizes need different markup. | A media query can change the order. |
| Layout intent is hidden. | Layout intent is clear. |
The idea to remember
- align-self = set on a flex item. It overrides the container's
align-itemsfor that item. - auto = the default for
align-self. The item follows the container. - order = set on a flex item. Items are shown from the lowest value to the highest.
- Default order =
0. Items with equal values keep their HTML order. - Visual only =
orderchanges what you see, not the HTML order.
S4. Step-by-Step Code Breakdown
1. Start with a flex item
Both properties are set on a flex item, which is a direct child of a flex container. The parent needs display: flex or display: inline-flex. On an element whose parent is not a flex container, they have no effect.
2. Know the align-self values
align-self accepts the same positions as align-items, plus auto. The table assumes a row direction.
| Value | Where this item goes | Item size | Typical use |
|---|---|---|---|
auto |
Wherever the container's align-items puts it |
Follows the container | The default for every item |
flex-start |
At the top of the line | Keeps its own size | An item that should not move down |
flex-end |
At the bottom of the line | Keeps its own size | A button pinned to the bottom |
center |
In the middle of the line | Keeps its own size | One centered item |
stretch |
Fills the line | Grows if the cross size is auto | One item that fills the line |
baseline |
Text baseline lines up with other baseline items | Keeps its own size | Mixed font sizes |
3. Compare the main values
Each item below sets its own align-self. The container has no align-items rule, so each item is placed by its own value.
Example 3: flex-start, center and flex-end on separate items
<!-- HTML -->
<div class="demo">
<span class="a">A</span>
<span class="b">B</span>
<span class="c">C</span>
</div>
/* CSS */
.demo {
display: flex;
width: 360px;
height: 80px;
}
.demo span {
width: 120px;
height: 40px;
}
.a { align-self: flex-start; }
.b { align-self: center; }
.c { align-self: flex-end; }
Expected output
|AAAAAA | |AAAAAABBBBBB | | BBBBBBCCCCCC| | CCCCCC| A is at the top, B is in the middle, and C is at the bottom. Each item follows its own align-self value.
4. Override the container with stretch
An item can also take a different align-self from the rest. Here the container centers its items, but the Menu item stretches to fill the whole line.
Example 4: One stretched item among centered items
<!-- HTML -->
<div class="bar">
<div class="bar-item">Logo</div>
<div class="bar-item menu">Menu</div>
<div class="bar-item">Login</div>
</div>
/* CSS */
.bar {
display: flex;
align-items: center;
width: 360px;
height: 80px;
}
.bar-item {
width: 120px;
height: 40px;
}
.menu {
align-self: stretch;
height: auto;
}
Expected output
| BBBBBB | |AAAAAABBBBBBCCCCCC| |AAAAAABBBBBBCCCCCC| | BBBBBB | Menu (B) fills the full 80px line. Logo (A) and Login (C) stay centered.
Notice the height: auto on the Menu item. Stretching only works when the cross size is automatic. A fixed height would stop it.
5. Understand how order works
Every flex item has an order value, and the default is 0. The browser sorts the items from the lowest value to the highest. Items with the same value stay in their HTML order. Negative values are allowed.
Example 5: Reordering four items
<!-- HTML -->
<div class="row">
<span class="a">A</span>
<span class="b">B</span>
<span class="c">C</span>
<span class="d">D</span>
</div>
/* CSS */
.row { display: flex; gap: 8px; }
.b { order: 3; }
.d { order: -1; }
Expected output
HTML order: A B C D order values: A:0 B:3 C:0 D:-1 Visual order: D A C B A and C share the value 0, so they keep their HTML order. B has the highest value, so it comes last. D has the lowest value, so it comes first.
6. Use order in responsive layouts
Because order lives in CSS, a media query can change it. A sidebar that comes first in the HTML can move below the main content on small screens.
Example 6: Moving the main content first on small screens
<!-- HTML -->
<div class="layout">
<aside class="sidebar">Sidebar</aside>
<main class="main">Main content</main>
</div>
/* CSS */
.layout { display: flex; gap: 16px; }
.sidebar { width: 120px; }
.main { width: 220px; }
@media (max-width: 600px) {
.layout { flex-direction: column; }
.main { order: -1; }
}
Expected output
Viewport wider than 600px: [ Sidebar ] [ Main content ] Viewport 600px or narrower: [ Main content ] [ Sidebar ] The HTML order is still Sidebar, then Main content. Keyboard focus and screen readers follow that HTML order.
Remember that the reordering is visual only. Keyboard focus and screen readers still follow the HTML, so a reordered layout can feel out of sync for those users.
S5. Architecture and Real-World Trade-Offs
Item-level rules are set per item, so each part of a page can make a single exception without changing the container. This is useful in toolbars, page layouts and card rows.
How the design fits a website
Imagine a website with a toolbar, a page layout and a row of cards. Each one has a simple container rule and one item that differs.
A focused exception flow
Each website area keeps a simple container rule and lets one item differ.
The animation traces each website area to the item that overrides its container.
Benefits for larger websites
- Maintainability: The container rule stays simple, and exceptions are small and easy to find.
- No extra markup: You do not need wrapper elements to treat one item differently.
- Responsive layouts: A media query can change
orderwithout touching the HTML. - Clear intent: A rule on one item shows that this item is special.
- Safe defaults: Items that set nothing keep following the container.
Important trade-offs
orderchanges the visual order only. Keyboard focus and screen readers still follow the HTML order.- Many different
ordervalues spread across a stylesheet become hard to follow. align-selfworks on the cross axis only. To push one item along the main axis, auto margins can be used instead.- Auto margins on the cross axis take the free space first, so
align-selfhas no effect there. - Both properties only work on direct children of a flex container.
Important: align-self and order change one item at a time. The other items keep following the container, and the HTML is not changed. If the reading order matters for the content, change the HTML order instead of using order.
S6. Common Mistakes and Best Practices
Mistake 1: Putting align-self on the container
Wrong: Add align-self: center to the flex container and expect its children to be centered.
Why it fails: align-self positions the element it is set on, inside its own parent. It does not change how the children are placed.
Better: Use align-items: center on the container.
Rule: align-items is for the container. align-self is for one item.
Mistake 2: Expecting align-self to move an item along the main axis
Wrong: Use align-self: flex-end to push one item to the right edge of a row.
Why it fails: align-self works on the cross axis, which is vertical in a row.
Better: Use justify-content for all items, or an auto margin on the item for one item.
Rule: Know which axis a property works on.
Mistake 3: Using align-self: stretch on a fixed-height item
Wrong: Set align-self: stretch on an item that also has a fixed height.
Why it fails: Stretching only applies when the cross size is auto, so the fixed height wins.
Better: Remove the fixed height, or use height: auto.
Rule: A fixed size beats stretch.
Mistake 4: Using order to reorder meaningful content
Wrong: Use order to put the steps of a tutorial or the items of a form in a new reading order.
Why it fails: The HTML order does not change. Keyboard users and screen readers still move through the original order, which no longer matches what they see.
Better: Change the order of the elements in the HTML when the reading order matters.
Rule: Use order for visual design, not for reordering content.
Mistake 5: Forgetting that every item starts at order 0
Wrong: Set order: 1 on one item and expect it to become the second item.
Why it fails: Every other item still has the default value 0. An item with 1 comes after all of them.
Better: Use order: -1 to move an item first, or give every item an explicit value.
Rule: An order value only means something compared with the other items.
S7. How This Comes Up in Interviews
Question 1: What is the difference between align-items and align-self?
What the interviewer wants: They want to know whether you understand container rules and item rules.
Strong answer:
"align-items is set on the flex container and positions all its items on the cross axis. align-self is set on one item and overrides the container for that item. Its default is auto, which means the item follows the container."
Question 2: What does the order property do?
Strong answer:
"It sets the visual position of a flex item. Items are sorted from the lowest order value to the highest. The default is 0, negative values are allowed, and items with equal values keep their HTML order."
Question 3: Does order change the tab order or the reading order?
Strong answer:
"No. It changes only the visual order. Keyboard focus and screen readers follow the HTML order, so I use order for visual design, and I change the HTML when the reading order matters."
Question 4: How do you place one item at the bottom of a row when the others are centered?
Strong answer:
"I keep align-items: center on the container and set align-self: flex-end on that one item. The container needs a height that is larger than the item, so there is free cross-axis space."
What should different experience levels explain?
- Junior developer: Explain
align-selfand show one item that sits differently from the others. - Senior developer: Explain the default
auto, sorting byorderwith ties, and the accessibility cost of reordering. - Technical architect: Explain how exception rules and order conventions keep components consistent and accessible across a design system.
S8. Hands-On Practice
Beginner: Make one exception
Create an 80px tall flex container with align-items: center and three 40px tall items. Set align-self: flex-end on the third item and observe the result.
Expected result: The first two items stay centered, and the third item sits at the bottom.
Intermediate: Predict the order
Create four items A, B, C and D. Give B order: 3 and D order: -1. Write down the visual order you expect, then open the page and compare.
Hint: A and C both have the default value 0.
Advanced: Build a responsive layout and test it
Build a sidebar and main content layout. On screens 600px or narrower, switch to a column and move the main content first with order. Then press the Tab key through any links inside it.
Expected result: The main content appears first on small screens. Tab order still follows the HTML. Decide whether the HTML order should be changed instead.
S9. Next Steps and Community
Previous article: Align Items and Align Content – Cross Axis Alignment.
Next article: Flex Grow – Controlling How Items Expand.
Continue the series by learning how flex-grow lets items expand to use the free space on the main axis, and how that space is shared between the items.
Try the examples in your own HTML and CSS project. Change which item has its own rule, try different order values, test your media queries, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Set the rule for the group. Then adjust one item.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.