← Back to Overview Chapter 02: Flexbox

1. Interactive Flex Container Playground

Click the control buttons below to dynamically adjust justify-content, align-items, and flex-direction in real-time!

justify-content (Main Axis):
align-items (Cross Axis):
flex-direction:
Ninja #1 Ninja
Robot #2 Robot
Hero #3 Hero
Mage #4 Mage
.container {
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
    flex-direction: row;
    gap: 12px;
}

2. Flex Items: flex-grow, flex-shrink & flex-basis

flex-grow dictates what proportion of remaining available space each item claims.

flex-grow: 0
Fixed width: 140px
flex-grow: 1
Takes 1 share of free space
flex-grow: 2
Takes 2 shares (2x wider!)
/* Container distributes remaining width according to grow ratios */
.item-fixed  { flex-grow: 0; width: 140px; }
.item-share1 { flex-grow: 1; }
.item-share2 { flex-grow: 2; } /* 2x growth factor */

3. Practical Flexbox Recipes

Recipe A: The 2-Line Centering Trick

Center anything horizontally and vertically with two flex properties:

Gem
Perfect Center
justify-content: center; align-items: center;
.center-container {
    display: flex;
    justify-content: center;
    align-items: center;
}

Recipe B: Navigation Bar with Auto Margins

Using margin-left: auto; on a flex child automatically pushes it (and following siblings) to the far right end!

.nav-links {
    margin-left: auto; /* Pushes the menu to the right! */
    display: flex;
    gap: 20px;
}

Recipe C: The Media Object

An avatar image next to descriptive content. flex-shrink: 0; prevents the avatar from squishing when text grows large.

Robot Avatar
Robo Assistant #42

Flexbox simplifies modern UI components. By setting flex-shrink: 0 on the media avatar, it maintains its geometric proportions even in constrained screen viewports.

.media-object { display: flex; align-items: flex-start; gap: 16px; }
.media-avatar { flex-shrink: 0; width: 64px; height: 64px; }
.media-content { flex-grow: 1; }
Fixed 🏠