๐Ÿ”’ Members-only lesson โ€” thanks for supporting devnotes
← back to course
progress is saved in this browser โ€” no account needed
devnotes / HTML & CSS Fundamentals / Lesson 4
lesson 4 of 6 ยท premium

Flexbox: Layouts That Don't Break

20 min ยท HTML & CSS Fundamentals

Before Flexbox, arranging boxes side by side reliably was genuinely painful โ€” old tricks like floats broke constantly. Flexbox fixed this properly. It's how navigation bars, button rows, and centered content are built on nearly every modern site.

Container and items: the core idea

Flexbox works on two levels: the container (the parent you turn into a flex layout) and the items (its direct children, which flex arranges automatically).

.container { display: flex; /* this one line turns on Flexbox for its children */ }

That's it to get started โ€” every direct child of .container now lines up in a row automatically, instead of stacking vertically like normal block elements do.

flex-direction: row or column

.container { display: flex; flex-direction: row; /* default โ€” side by side */ /* flex-direction: column; */ /* stacked vertically, but still flex */ }

justify-content: spacing along the main axis

This controls how items are spread out horizontally (assuming the default row direction):

justify-content: center;
A
B
C
justify-content: space-between;
A
B
C
justify-content: space-around;
A
B
C
ValueEffect
flex-startItems bunched at the start (default)
centerItems bunched in the middle
space-betweenFirst and last touch the edges, even spacing between
space-aroundEven spacing around every item, including the edges

align-items: positioning on the cross axis

While justify-content handles horizontal spacing, align-items handles vertical alignment within the row (perfect for that classic "how do I vertically center this" problem):

.container { display: flex; align-items: center; /* vertically centers items, no matter their height */ }
quick tip
justify-content: center; align-items: center; together is the single most common way to perfectly center anything โ€” a modal, a loading spinner, a hero section โ€” in modern CSS. It replaces a dozen old hacky centering tricks.
AD SLOT โ€” 728ร—90 or responsive (mid-lesson)

flex-wrap: what happens when items don't fit

.container { display: flex; flex-wrap: wrap; /* items drop to a new line instead of shrinking forever */ }

Without flex-wrap: wrap, Flexbox tries to cram everything into one line, shrinking items until they're uncomfortably small. Adding it lets items flow onto new rows once they run out of horizontal space โ€” essential for anything that needs to work on mobile.

gap: spacing between items, the easy way

.container { display: flex; gap: 16px; /* space between every item, no margin hacks needed */ }

Before gap existed, spacing flex items apart meant adding margin to every child and subtracting it from the container โ€” messy. gap replaced all of that with one clean line, and it's supported everywhere modern.

flex-grow and flex-shrink: controlling individual items

.sidebar { flex-grow: 0; } /* stays its natural size */ .main-content { flex-grow: 1; } /* expands to fill remaining space */

flex-grow: 1 on an item tells it "take up whatever extra space is left" โ€” the classic pattern for a fixed-width sidebar next to a content area that fills the rest of the screen.

Try it yourself

exercise
Build a simple navigation bar: a <div class="nav"> containing a logo and three links. Give .nav display: flex, justify-content: space-between, and align-items: center. Watch the logo and links line up cleanly on one row, evenly spaced.

Level up: a 3-column card row

exercise 2
Take three .card elements from Lesson 2 and wrap them in a container with display: flex, gap: 20px, and flex-wrap: wrap. Give each card flex: 1 so they share space equally. Resize your browser and watch them wrap onto a new row once the window gets narrow.
what's next
Flexbox is perfect for one row or column at a time. Lesson 5 covers CSS Grid โ€” for when you need to control a full two-dimensional layout (rows and columns) at once.
bottom line
display: flex turns on Flexbox. justify-content and align-items handle alignment, flex-wrap keeps things responsive, and gap handles spacing cleanly. Together, they solve the layout problems that used to need hacks.
AD SLOT โ€” 300ร—250 or responsive (in-lesson)
next upLesson 5: CSS Grid
Continue โ†’