Flex Items

Back

Loading concept...

🎭 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&lt;br/&gt;Top of container"] A --> C["flex-end&lt;br/&gt;Bottom of container"] A --> D["center&lt;br/&gt;Middle of container"] A --> E["stretch&lt;br/&gt;Fill full height"] A --> F["baseline&lt;br/&gt;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

  1. flex-basis = Starting size before flex magic
  2. flex shorthand = flex: grow shrink basis
  3. order = Change visual order, keep HTML same
  4. align-self = One item breaks alignment rules

You’re now ready to direct your flex items like a true layout maestro! 🎭✨

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.