2. Modern CSS Layout & Design Systems
Flexbox Layouts: Aligning Rows & Columns Easily
Stop fighting CSS alignment: master 1D Flexbox layout with flex-direction, justify-content, align-items, flex-wrap, gap, and flex: 1.
- Understand parent container vs child item rules in CSS Flexbox
- Align and distribute space along rows or columns using justify-content, align-items, and gap
- Build wrapping responsive navbars and equal-height button bars cleanly
Before Flexbox arrived in CSS, placing a logo on the left and links on the right—or vertically centering an icon inside a button—took fragile layout hacks. Flexbox turns one-dimensional alignment (arranging items in a single row or column) into clean, predictable rules.
Parent Container vs Child Items
Flexbox separates responsibilities cleanly:
- You put
display: flexon the parent container to tell children how to line up, wrap, and space out. - You optionally put rules like
flex: 1on specific child items when you want an item to grow and fill leftover room.
.navbar {
display: flex;
justify-content: space-between; /* push left edge and right edge apart */
align-items: center; /* center items vertically */
gap: 16px; /* uniform space between items */
flex-wrap: wrap; /* wrap onto a second line on small phones */
}The moment .navbar gets display: flex, all its direct children stop stacking vertically and line up side by side in a horizontal row automatically.
Mastering the Two Axes (justify-content vs align-items)
With flex-direction: row, justify-content aligns left-to-right (Main Axis) and align-items aligns top-to-bottom (Cross Axis). Switching to column swaps both axes!
Every Flexbox container works along two perpendicular axes:
- Main Axis (the direction items flow—horizontal left-to-right by default): controlled by
justify-content. - Cross Axis (perpendicular to the flow—vertical top-to-bottom by default): controlled by
align-items.
| Property | Common Values | Visual Effect (Default Row) |
|---|---|---|
justify-content | flex-start / center / flex-end | Packs items left, dead-center, or right |
justify-content | space-between | First item pins left, last pins right, equal space inside |
align-items | stretch (default) / center / flex-start | Stretches heights equally, or aligns vertically to center/top |
flex-direction | row (default) / column | Switches between horizontal row and vertical stack |
Watch carefully if you switch to flex-direction: column: the main axis rotates to vertical! That means in column mode, justify-content spaces items top-to-bottom, and align-items aligns them left-to-right.
flex-direction: row | justify-content: flex-start | align-items: stretch
Step 1: display: flex (defaults)
By default, flex-direction is row, justify-content is flex-start, and items stretch vertically across the container.
Filling Leftover Space (flex: 1) & Clean Spacing (gap)
Suppose you are building a search bar with a text <input> and a "Search" button side by side, or a card whose "Enroll Free" button always sits neatly at the bottom even when descriptions vary in length:
.search-bar {
display: flex;
gap: 8px;
}
.search-bar input {
flex: 1; /* Stretch input to absorb all unused horizontal space */
}Putting gap: 12px; on the flex container spaces items evenly without leaving an awkward extra margin sticking off the last element or breaking wrapped rows!
By default, Flexbox squeezes children aggressively onto one rigid line (flex-wrap: nowrap). On a 360px phone, a navbar with 5 links overflows right off the screen unless you add flex-wrap: wrap;!
Try It Live: Responsive Flexbox Navbar & Search Bar
Experiment with changing justify-content: space-between to center, toggle flex-wrap, and watch how the flex: 1 search bar adapts automatically:
Practice Challenge: Centering a Hero Box Dead-Center
How do you center a single <div> card both horizontally AND vertically inside a 300px-tall banner using Flexbox?
Quick Checkpoints
In a horizontal Flexbox row (flex-direction: row), which property centers items vertically?
Which Flexbox property allows items inside a tight row to flow cleanly onto a second line instead of shrinking awkwardly or overflowing?