π Flex Items: The Star Performers of Your Layout Stage
Imagine a theater stage. The stage itself is the flex containerβbut the real magic? Thatβs the actors (flex items). Each actor can grow, shrink, take center stage, or step back. Today, youβll learn how to direct these performers like a pro director!
π The Big Picture
When you put items inside a flex container, they become flex items. Each item has special powers:
| Power | What It Does |
|---|---|
| flex-basis | Starting size before growing/shrinking |
| flex shorthand | All sizing powers in one line |
| order | Change position without moving HTML |
| align-self | Override alignment for just one item |
π¦ Flex Item Sizing: How Items Share Space
Think of a pizza π. When you have 3 friends, how do you divide it?
- Equal slices? Everyone gets the same
- Hungry friend gets more? Some slices are bigger
- Someone not hungry? They get less
Flex items work the same way! They can:
- Grow to fill empty space
- Shrink when space is tight
- Have a starting size before anything happens
.item {
flex-grow: 1; /* Can grow */
flex-shrink: 1; /* Can shrink */
flex-basis: 0; /* Start from 0 */
}
π flex-basis: The Starting Point
What is it? The initial size of an item BEFORE grow/shrink happens.
Simple Example:
.item {
flex-basis: 200px;
}
This means: βStart at 200px, then grow or shrink from there.β
flex-basis vs width
| Property | When to Use |
|---|---|
width |
Fixed size, ignore flex rules |
flex-basis |
Starting size, respects flex |
Real-World Analogy:
Think of flex-basis as your backpack size. You start with a backpack, then:
- You can stuff more in (grow)
- Remove things if too heavy (shrink)
- But you START with that backpack size!
Common Values
/* Auto = use content size */
flex-basis: auto;
/* Specific size */
flex-basis: 100px;
flex-basis: 25%;
/* Zero = ignore content size */
flex-basis: 0;
β‘ flex Shorthand: The Power Combo
Typing three properties is boring. The flex shorthand does it all!
The Magic Formula
flex: grow shrink basis;
/* Example */
flex: 1 1 200px;
/* grow=1, shrink=1, basis=200px */
Most Common Patterns
| Shorthand | Meaning | Use Case |
|---|---|---|
flex: 1 |
Grow equally, start from 0 | Equal columns |
flex: auto |
Grow/shrink, use content size | Natural sizing |
flex: none |
Donβt grow or shrink | Fixed buttons |
flex: 0 0 100px |
Fixed 100px | Sidebar |
Example: Three Equal Boxes
<div class="container">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
</div>
.container {
display: flex;
}
.box {
flex: 1;
/* All three share space equally! */
}
What Each Number Does
flex: 2 1 100px;
/* β β β
| | ββ basis: start at 100px
| ββββ shrink: can shrink (1 = yes)
ββββββ grow: grow 2x faster than flex:1
*/
π’ order: Rearrange Without Moving HTML
The Problem: Your HTML says A, B, C. But you want C, A, B on screen!
The Solution:
The order property changes visual order without touching HTML.
How It Works
/* Default order is 0 */
.item-a { order: 2; } /* Shows third */
.item-b { order: 3; } /* Shows last */
.item-c { order: 1; } /* Shows first */
Visual Result:
HTML: A B C
Screen: C A B
Real-World Example
<nav class="menu">
<a class="logo">π </a>
<a class="search">π</a>
<a class="cart">π</a>
</nav>
.menu {
display: flex;
}
/* On mobile: cart first! */
@media (max-width: 600px) {
.cart { order: -1; }
}
Quick Reference
| Order Value | Position |
|---|---|
| Negative (-1) | Moves to front |
| 0 (default) | Normal position |
| Positive (1, 2β¦) | Moves to back |
β οΈ Remember: Screen readers still follow HTML order. Use wisely!
π― align-self: The Rebel Item
The Scenario: Your container says: βEveryone align to the top!β But ONE item wants to be at the bottom.
The Solution:
align-self lets ONE item break the rules.
How It Works
.container {
display: flex;
align-items: flex-start; /* All at top */
}
.special-item {
align-self: flex-end; /* Just me at bottom */
}
All align-self Values
graph TD A["align-self values"] --> B["flex-start<br/>Top of container"] A --> C["flex-end<br/>Bottom of container"] A --> D["center<br/>Middle of container"] A --> E["stretch<br/>Fill full height"] A --> F["baseline<br/>Text baseline"]
Visual Example
βββββββββββββββββββββββββββ
β βββββ βββββ β flex-start
β β A β β C β β
β βββββ βββββ β
β β
β βββββ β center (B)
β β B β β
β βββββ β
β β
β βββββ β flex-end (D)
β β D β β
β βββββ β
βββββββββββββββββββββββββββ
align-self: flex-start (A, C)
align-self: center (B)
align-self: flex-end (D)
Code Example
.container {
display: flex;
align-items: stretch;
height: 200px;
}
.item-1 { align-self: flex-start; }
.item-2 { align-self: center; }
.item-3 { align-self: flex-end; }
.item-4 { align-self: stretch; }
π¬ Putting It All Together
Hereβs a real navbar using everything we learned:
<nav class="navbar">
<div class="logo">Brand</div>
<div class="spacer"></div>
<div class="menu">Menu</div>
<div class="cart">π</div>
</nav>
.navbar {
display: flex;
align-items: center;
height: 60px;
}
.logo {
flex: none; /* Don't grow/shrink */
order: 1;
}
.spacer {
flex: 1; /* Eat all space */
order: 2;
}
.menu {
flex: none;
order: 3;
}
.cart {
flex: 0 0 50px; /* Fixed 50px */
order: 4;
align-self: stretch; /* Full height */
}
π§ Quick Memory Tricks
| Property | Remember As |
|---|---|
flex-basis |
βStarting backpack sizeβ |
flex |
βGrow Shrink Startβ |
order |
βVisual shuffleβ |
align-self |
βThe rebel itemβ |
β¨ Key Takeaways
- flex-basis = Starting size before flex magic
- flex shorthand =
flex: grow shrink basis - order = Change visual order, keep HTML same
- align-self = One item breaks alignment rules
Youβre now ready to direct your flex items like a true layout maestro! πβ¨
