CSS layout has come a long way. For years, developers wrestled with floats, clearfix hacks, and tables-for-everything. Then Flexbox arrived and changed how we think about one-dimensional layouts. Then Grid arrived and changed how we think about layouts, period.
Today both are universally supported and production-ready. The question is no longer whether to use them — it's when to reach for which one. This guide covers every major property for both systems, shows you real patterns you'll use in production, and gives you quick-reference tables you'll actually want to bookmark.
Flexbox vs Grid: The Core Distinction
Before diving into properties, get this mental model right:
- Flexbox is one-dimensional. It lays items out in a single row or a single column. You can wrap items onto new lines, but each line is independent of the others.
- Grid is two-dimensional. It controls rows and columns simultaneously. Items can span across both axes in a coordinated way.
A simple rule of thumb:
Use Flexbox when you're arranging items along a line — navigation links, a row of buttons, a list of tags.
Use Grid when you're placing items into a structure — page layouts, card grids, dashboards.
They also compose well together: a Grid handles the macro page layout, and Flexbox handles the micro alignment inside each grid cell.
Part 1: CSS Flexbox — The Complete Reference
Enabling Flexbox
.container {
display: flex; /* or inline-flex for inline-level containers */
}
All direct children become flex items. The container itself becomes the flex container.
flex-direction — Which Way Does the Line Run?
Controls the main axis — the direction items flow.
.container {
flex-direction: row; /* default: left to right */
flex-direction: row-reverse; /* right to left */
flex-direction: column; /* top to bottom */
flex-direction: column-reverse; /* bottom to top */
}
Visually: row gives you a horizontal strip of items. column stacks them vertically like a list. row-reverse and column-reverse flip that order while keeping items at the start of the container visually.
justify-content — Distributing Space Along the Main Axis
Controls how items are spaced along the main axis (horizontal in row, vertical in column).
.container {
justify-content: flex-start; /* items packed at the start */
justify-content: flex-end; /* items packed at the end */
justify-content: center; /* items centered */
justify-content: space-between; /* equal gaps between items, no edge gaps */
justify-content: space-around; /* equal gaps around items (edges get half) */
justify-content: space-evenly; /* equal gaps everywhere, including edges */
}
space-between is the most-used value for navbars and toolbars. center is the fastest way to horizontally center a group of items.
align-items — Aligning Across the Cross Axis
Controls how items are aligned on the cross axis (perpendicular to the main axis).
.container {
align-items: stretch; /* default: items stretch to fill container height */
align-items: flex-start; /* items align to the top (in row direction) */
align-items: flex-end; /* items align to the bottom */
align-items: center; /* items centered vertically */
align-items: baseline; /* items aligned by their text baseline */
}
align-items: center combined with justify-content: center is the classic one-liner perfect centering solution:
.center-everything {
display: flex;
justify-content: center;
align-items: center;
}
flex-wrap — Single Line or Multiple Lines?
By default, flex items squeeze onto one line. flex-wrap allows them to spill onto additional lines.
.container {
flex-wrap: nowrap; /* default: single line, items may overflow */
flex-wrap: wrap; /* items wrap to new lines */
flex-wrap: wrap-reverse; /* wraps, but new lines appear above */
}
Once items wrap, align-content controls how the lines themselves are distributed along the cross axis (it works exactly like justify-content but for the bundle of lines).
gap — Space Between Items
The modern way to add gutters. Works on both flex and grid containers.
.container {
gap: 1rem; /* equal gap in both directions */
gap: 1rem 2rem; /* row-gap column-gap */
row-gap: 1rem;
column-gap: 2rem;
}
Prefer gap over margins on children — it only applies between items, never on the outer edges.
Flex Item Properties
These go on the children, not the container.
flex-grow — Can This Item Expand?
.item {
flex-grow: 0; /* default: item does not grow */
flex-grow: 1; /* item claims all available extra space */
}
When multiple items have flex-grow: 1, they split the extra space equally. Give one item flex-grow: 2 and it claims twice as much as siblings with flex-grow: 1.
flex-shrink — Can This Item Shrink?
.item {
flex-shrink: 1; /* default: item shrinks proportionally when space is tight */
flex-shrink: 0; /* item refuses to shrink (useful for fixed-width sidebars) */
}
flex-basis — What's the Item's Starting Size?
Sets the ideal size before growing or shrinking kicks in.
.item {
flex-basis: auto; /* default: use the item's content size */
flex-basis: 200px; /* start at 200px, then grow/shrink from there */
flex-basis: 0; /* start at 0; combined with flex-grow, distributes all space */
}
The flex Shorthand
Use this instead of setting flex-grow, flex-shrink, and flex-basis separately.
.item { flex: 0 1 auto; } /* default */
.item { flex: 1; } /* shorthand for flex: 1 1 0 — grow, shrink, start at 0 */
.item { flex: none; } /* shorthand for flex: 0 0 auto — rigid, no grow/shrink */
order — Reorder Without Touching the HTML
.item-first { order: -1; } /* moves visually before items with order: 0 (default) */
.item-last { order: 1; } /* moves visually after */
Use with caution — it creates a disconnect between DOM order (which screen readers follow) and visual order.
align-self — Override the Container's align-items for One Item
.special-item {
align-self: flex-end; /* this item aligns to the bottom, others use container setting */
}
Flexbox Quick Reference Table
| Property | Applies To | Key Values |
|---|---|---|
display |
Container | flex, inline-flex |
flex-direction |
Container | row, column, row-reverse, column-reverse |
justify-content |
Container | flex-start, flex-end, center, space-between, space-around, space-evenly |
align-items |
Container | stretch, flex-start, flex-end, center, baseline |
align-content |
Container | Same values as justify-content (multi-line only) |
flex-wrap |
Container | nowrap, wrap, wrap-reverse |
gap |
Container | Any length value |
flex |
Item | <grow> <shrink> <basis> |
flex-grow |
Item | Number (0 = no grow) |
flex-shrink |
Item | Number (0 = no shrink) |
flex-basis |
Item | Length, auto, 0 |
order |
Item | Integer |
align-self |
Item | Same as align-items |
Part 2: CSS Grid — The Complete Reference
Enabling Grid
.container {
display: grid; /* or inline-grid */
}
Grid gives you explicit control over both rows and columns.
grid-template-columns and grid-template-rows
Define the track sizes for the grid.
.container {
grid-template-columns: 200px 1fr 1fr; /* fixed 200px sidebar, two equal columns */
grid-template-rows: 80px auto 60px; /* fixed header, flexible content, fixed footer */
}
The fr Unit
fr stands for fraction. It distributes leftover space after fixed sizes are subtracted.
grid-template-columns: 1fr 2fr 1fr;
/* Three columns: the middle gets twice as much space as each side */
repeat()
Avoids repetition when defining multiple equal tracks.
grid-template-columns: repeat(4, 1fr); /* four equal columns */
grid-template-columns: repeat(3, 200px); /* three fixed columns */
grid-template-columns: repeat(2, 1fr 2fr); /* repeating pattern: 1fr 2fr 1fr 2fr */
minmax()
Sets a minimum and maximum size for a track. Especially powerful for responsive layouts.
grid-template-columns: repeat(3, minmax(200px, 1fr));
/* Three columns, each at least 200px wide, growing equally above that */
auto-fill vs auto-fit
These work with repeat() to create responsive grids without media queries.
/* auto-fill: create as many tracks as fit, even if empty */
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* auto-fit: same, but collapse empty tracks so filled items stretch */
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
Visually: with three items in a wide container, auto-fill might leave empty ghost columns on the right while auto-fit stretches the three items to fill the full width. For card grids, auto-fill is usually the right choice.
gap in Grid
Works identically to Flexbox.
.container {
gap: 1.5rem; /* equal row and column gap */
gap: 2rem 1rem; /* row-gap 2rem, column-gap 1rem */
}
grid-template-areas — Named Layout Regions
One of Grid's most powerful features. Draw your layout in ASCII art, then assign elements to named regions.
.container {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: 60px 1fr 50px;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }
This is the holy grail layout — header across the top, sidebar and content in the middle, footer across the bottom — in about 15 lines of CSS. No floats, no hacks.
Placing Items Explicitly
You can also place items using line numbers.
.item {
grid-column: 1 / 3; /* span from column line 1 to column line 3 (2 columns wide) */
grid-row: 2 / 4; /* span from row line 2 to row line 4 (2 rows tall) */
}
/* Shorthand */
.item {
grid-area: 2 / 1 / 4 / 3; /* row-start / col-start / row-end / col-end */
}
/* Using span keyword */
.item {
grid-column: 1 / span 2; /* start at line 1, span 2 columns */
grid-row: span 3; /* span 3 rows from wherever it naturally lands */
}
Implicit Grid — What Happens to Extra Items
When items overflow your explicitly defined tracks, Grid creates implicit tracks automatically.
.container {
grid-auto-rows: 200px; /* all implicit rows will be 200px */
grid-auto-columns: 1fr; /* all implicit columns will be 1fr */
grid-auto-flow: row; /* default: fill rows, create new rows as needed */
grid-auto-flow: column; /* fill columns, create new columns as needed */
grid-auto-flow: row dense; /* backfill gaps when items vary in size */
}
grid-auto-flow: row dense is useful for masonry-style layouts where items have different heights and you don't want gaps left by larger items.
Alignment in Grid
Grid has the full alignment toolkit:
/* Aligns items within their grid areas */
.container {
justify-items: start | end | center | stretch; /* horizontal */
align-items: start | end | center | stretch; /* vertical */
place-items: center; /* shorthand: align-items justify-items */
}
/* Aligns the entire grid within the container (when tracks don't fill it) */
.container {
justify-content: start | end | center | stretch | space-between | space-around | space-evenly;
align-content: start | end | center | stretch | space-between | space-around | space-evenly;
place-content: center;
}
/* Override for individual items */
.item {
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
place-self: center;
}
CSS Grid Quick Reference Table
| Property | Applies To | Key Values |
|---|---|---|
display |
Container | grid, inline-grid |
grid-template-columns |
Container | Length, fr, auto, repeat(), minmax() |
grid-template-rows |
Container | Same as columns |
grid-template-areas |
Container | Named string areas |
gap |
Container | Any length |
grid-auto-rows |
Container | Length, minmax() |
grid-auto-columns |
Container | Length, minmax() |
grid-auto-flow |
Container | row, column, dense |
justify-items |
Container | start, end, center, stretch |
align-items |
Container | start, end, center, stretch |
grid-column |
Item | <start> / <end>, span <n> |
grid-row |
Item | <start> / <end>, span <n> |
grid-area |
Item | Named area or row-start / col-start / row-end / col-end |
justify-self |
Item | start, end, center, stretch |
align-self |
Item | start, end, center, stretch |
Part 3: Real-World Layout Patterns
Pattern 1: Responsive Navbar
A logo on the left, navigation links in the middle or right. Flexbox is perfect here.
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 2rem;
height: 64px;
}
.navbar__logo {
font-size: 1.5rem;
font-weight: 700;
flex-shrink: 0; /* prevent logo from shrinking */
}
.navbar__links {
display: flex;
gap: 2rem;
list-style: none;
}
/* Collapse to hamburger on mobile */
@media (max-width: 768px) {
.navbar__links {
display: none; /* swap for your hamburger menu logic */
}
}
Pattern 2: Holy Grail Layout
Classic three-column layout with full-width header and footer. Grid shines.
.page {
display: grid;
grid-template-columns: 220px 1fr 180px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"sidebar content aside"
"footer footer footer";
min-height: 100vh;
gap: 0;
}
.page__header { grid-area: header; }
.page__sidebar { grid-area: sidebar; }
.page__content { grid-area: content; }
.page__aside { grid-area: aside; }
.page__footer { grid-area: footer; }
@media (max-width: 900px) {
.page {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"content"
"sidebar"
"aside"
"footer";
}
}
Pattern 3: Responsive Card Grid
Cards that automatically reflow based on available space — no media queries needed.
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
.card {
display: flex;
flex-direction: column;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.card__body {
flex: 1; /* pushes the card footer to the bottom */
padding: 1.25rem;
}
.card__footer {
padding: 1rem 1.25rem;
border-top: 1px solid #eee;
}
The outer Grid handles columns. The inner Flexbox on each card ensures footers align at the bottom regardless of card body height — a common requirement.
Pattern 4: Perfect Centering
Horizontally and vertically center anything inside its container.
/* Flexbox approach */
.centered {
display: flex;
justify-content: center;
align-items: center;
}
/* Grid approach — often cleaner for single items */
.centered {
display: grid;
place-items: center;
}
Both work. The Grid approach with place-items: center is the most concise.
Pattern 5: Sidebar Layout with Fixed + Flexible Columns
.layout {
display: grid;
grid-template-columns: 280px 1fr;
gap: 2rem;
min-height: 100vh;
}
.sidebar {
/* sticky sidebar */
position: sticky;
top: 0;
height: 100vh;
overflow-y: auto;
}
.main-content {
padding: 2rem;
}
The 1fr column claims all remaining space after the sidebar's fixed 280px.
Pattern 6: Feature Grid with Featured Item
A grid where the first item spans two columns, like a magazine layout.
.feature-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.feature-grid__item--featured {
grid-column: span 2; /* takes up two of three columns */
}
Part 4: Common Mistakes and How to Fix Them
Mistake 1: Using Flexbox for a Two-Dimensional Layout
You have a 3x4 card grid where all rows need to align with each other. You use flex-wrap: wrap with flex-basis: 33%.
The problem: Flex lines are independent. If a card in row one is taller, it does not make cards in row two start lower — row two just starts wherever row one ended, creating ragged alignment.
The fix: Use Grid. grid-template-columns: repeat(3, 1fr) creates columns that enforce alignment across all rows.
Mistake 2: Forgetting min-width: 0 on Flex/Grid Items
Text overflow or wide content (like a pre block or a wide image) can break out of a flex or grid item because the default minimum size of a flex/grid item is its content size.
/* Symptom: text or content overflows its cell */
/* Fix: */
.flex-item,
.grid-item {
min-width: 0; /* allows the item to shrink below its content size */
overflow: hidden;
}
Mistake 3: Using margin: auto When gap Works
Old pattern:
.nav a + a {
margin-left: 1rem;
}
Modern pattern:
.nav {
display: flex;
gap: 1rem;
}
The gap approach is cleaner, more declarative, and doesn't add margin to the first item.
Mistake 4: Not Using auto-fill/auto-fit for Responsive Grids
Writing media queries for every breakpoint:
/* Verbose and brittle */
@media (min-width: 600px) { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 900px) { grid-template-columns: repeat(3, 1fr); }
@media (min-width: 1200px) { grid-template-columns: repeat(4, 1fr); }
Just use:
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
The grid handles every breakpoint automatically.
Mistake 5: Overusing order and Harming Accessibility
Visual order should match DOM order whenever possible. Screen readers, keyboard navigation, and focus order all follow the DOM, not CSS visual order. Use order only when you have a genuine reason and test the result with a keyboard.
Part 5: When to Use Each — Decision Guide
| Scenario | Use |
|---|---|
| Navigation bar with links | Flexbox |
| Row of buttons | Flexbox |
| Tags on a blog post | Flexbox |
| Vertically centering content in a div | Flexbox or Grid (place-items: center) |
| Page-level layout (header, sidebar, content, footer) | Grid |
| Card grid that reflows responsively | Grid |
| Image gallery | Grid |
| Dashboard with widgets at specific positions | Grid |
| Form fields in a row | Flexbox |
| Complex form with labeled fields in columns | Grid |
| Items in a list that should push last item to bottom | Flexbox with flex: 1 on content |
When in doubt: if you're thinking about one axis, use Flexbox. If you're thinking about rows and columns, use Grid.
Closing Thoughts
Flexbox and Grid are not competing tools — they're complementary. Modern CSS layouts almost always use both: Grid for the macro structure, Flexbox for the micro arrangement inside each grid cell. Learning both deeply, and knowing intuitively when to reach for each, is one of the highest-leverage CSS skills you can develop.
Bookmark this page. The next time you're staring at a layout problem wondering which tool to use, come back to the decision table and the pattern examples. Most layout problems are variations on the patterns covered here.
Imperial Products by The Emperor offers a curated collection of developer tools and productivity resources built for engineers who care about doing things right. If you found this guide useful, explore our frontend resource bundles — structured references and cheat sheets designed to stay open in your second monitor.