π Introduction
The order property in Flexbox allows you to control the **visual order** of flex items, independent of their order in the HTML source. This is extremely useful for responsive design, rearranging layouts, and prioritizing important UI elements without modifying markup. Think of it as assigning numbers to decide which element should come first, second, third, etc. π¨β¨
π― Why Use order?
- Rearrange UI without changing HTML
- Change layout per breakpoint (mobile-first)
- Highlight or deprioritize elements
- Useful in dashboards, cards, and responsive grids
β¨ Syntax
Syntax
selector {
order: <number>;
}Note
π§© How order Works
Flex items are sorted visually based on their order value:
- Lowest order = appears first
- Higher order = appears later
- Items with the same order follow HTML order
π₯ Practical Examples
1. Simple Reordering
Basic Reorder
.item1 { order: 3; }
.item2 { order: 1; }
.item3 { order: 2; }Visual order now becomes: Item 2 β Item 3 β Item 1
2. Highlight an Item by Putting It First
Highlight
.important {
order: -1; /* Moves to the front */
}3. Responsive Reordering (Mobile First)
Responsive Example
.sidebar { order: 2; }
.main { order: 1; }
@media (min-width: 768px) {
.sidebar { order: 1; }
.main { order: 2; }
}Mobile: main β sidebar Desktop: sidebar β main
4. Using order in a Navigation Bar
Navbar Example
.logo { order: 1; }
.menu { order: 2; }
.profile { order: 3; }
@media (max-width: 600px) {
.profile { order: -1; } /* Move profile to the front */
}5. Ordering Cards in a Dashboard
Dashboard Cards
.card-alert { order: -1; }
.card-stats { order: 1; }
.card-recent { order: 2; }π order β Behavior Summary
| order Value | Meaning |
|---|---|
| -1 | Appears before all default items |
| 0 | Default order (follows HTML structure) |
| 1, 2, 3β¦ | Appears after default items in numerical order |
π§ Tips & Best Practices
- Donβt overuse order β excessive reordering may confuse users and screen readers.
- Use semantic HTML order for accessibility; use order only for layout changes.
- Use negative numbers carefullyβthey override everything visually.
- Perfect for responsive layouts where order changes by screen size.
π Conclusion
The order property is a powerful Flexbox feature that allows you to rearrange elements visually without changing the structure of your HTML. It's essential for responsive layouts, mobile-first design, and dynamic interfaces. Learn it well β and you'll unlock far more flexibility in your designs. π