Table of contents
CSS layouts used to mean floats, clearfixes, and endless hacks. Today, Flexbox and Grid give developers two powerful, purpose-built layout systems. The challenge is knowing which one to reach for and when.
This guide breaks down the core differences, shows you exactly when each tool is the right choice, and provides a quick-reference cheat sheet you can bookmark.
The core difference in one sentence
Flexbox handles one dimension at a time — either a row or a column. Grid handles two dimensions simultaneously — rows and columns at once.
That single distinction drives almost every decision about which to use.
What is CSS Flexbox?
Flexbox (Flexible Box Layout) is a layout model that arranges items in a single axis: either horizontally (row) or vertically (column). You define a flex container, and its direct children become flex items that can grow, shrink, and align along that axis.
Flexbox is ideal when you have a group of items that need to be distributed or aligned along one direction — navigation links, a row of cards, a button group, or a centered element.
.nav {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
Key Flexbox properties:
flex-direction— row, column, row-reverse, column-reversejustify-content— alignment along the main axis (start, end, center, space-between, space-around, space-evenly)align-items— alignment along the cross axis (start, end, center, stretch, baseline)flex-wrap— whether items wrap to a new linegap— space between flex itemsflex-grow,flex-shrink,flex-basis— how individual items resize
What is CSS Grid?
CSS Grid is a two-dimensional layout system. You define rows and columns on the container, then place items into cells, areas, or let the browser auto-place them.
Grid is ideal when you need to control both rows and columns at once — full-page layouts, image galleries, dashboards, complex form layouts.
.layout {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: auto 1fr auto;
gap: 1.5rem;
}
Key Grid properties:
grid-template-columns— defines columns (e.g.,repeat(3, 1fr),250px 1fr)grid-template-rows— defines rowsgrid-template-areas— names areas for semantic placementgap(orrow-gap/column-gap) — space between grid cellsgrid-column,grid-row— how many cells an item spansplace-items— shorthand foralign-items+justify-itemsauto-fit/auto-fill— responsive columns without media queries
Flexbox vs Grid: when to use each
Use Flexbox when:
1. You have a single row or column of items Navigation bars, tab lists, button groups, icon rows — anything that is fundamentally a line of items.
2. Content size should drive layout Flexbox lets items grow or shrink based on their content and available space. If you want items to be as wide as they need to be, Flexbox is the natural fit.
3. You need simple alignment
Centering an element both horizontally and vertically with display: flex; justify-content: center; align-items: center on the parent is still the most readable centering solution in CSS.
4. You are working inside a component A card's internal layout (image, title, description, button stacked vertically) is a single column — Flexbox handles it cleanly.
Use Grid when:
1. You are building a full-page layout Header, sidebar, main content, and footer — this is a two-dimensional problem. Grid lets you define the structure once and position everything explicitly.
2. You need rows and columns to align across items Unlike Flexbox, Grid tracks rows across the entire grid. If you want a gallery where every image thumbnail aligns in rows and columns (not just in its own row), you need Grid.
3. You want responsive columns without media queries
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)) creates as many 250px+ columns as fit the container and wraps to fewer columns as the viewport narrows. This is impossible to replicate cleanly with Flexbox.
4. You need to place items precisely Grid lets you put an item in a specific row and column, or span it across multiple rows and columns. Named grid areas make complex layouts readable.
Flexbox cheat sheet
| Property | Values | Effect |
|---|---|---|
display |
flex |
Creates a flex container |
flex-direction |
row | column | row-reverse | column-reverse |
Sets main axis direction |
justify-content |
flex-start | flex-end | center | space-between | space-around | space-evenly |
Aligns items on main axis |
align-items |
stretch | flex-start | flex-end | center | baseline |
Aligns items on cross axis |
flex-wrap |
nowrap | wrap | wrap-reverse |
Allows items to wrap |
gap |
length | Space between items |
flex |
grow shrink basis |
Item sizing shorthand |
flex-grow |
number | How much item grows |
flex-shrink |
number | How much item shrinks |
flex-basis |
length | auto |
Item's base size |
order |
integer | Item order within container |
align-self |
same as align-items | Overrides align-items for one item |
Grid cheat sheet
| Property | Example | Effect |
|---|---|---|
display |
grid |
Creates a grid container |
grid-template-columns |
repeat(3, 1fr) |
Defines 3 equal columns |
grid-template-rows |
auto 1fr auto |
Defines row sizes |
grid-template-areas |
"header header" "sidebar main" |
Named layout areas |
gap |
1rem 2rem |
Row gap, column gap |
grid-column |
1 / 3 |
Item spans columns 1–2 |
grid-row |
2 / 4 |
Item spans rows 2–3 |
grid-area |
header |
Places item in named area |
place-items |
center |
Centers items in cells |
auto-fit |
repeat(auto-fit, minmax(200px, 1fr)) |
Responsive columns |
auto-fill |
repeat(auto-fill, minmax(200px, 1fr)) |
Fills with empty tracks |
fr unit |
1fr 2fr |
Fractional unit of available space |
Common layout patterns
Centering a single element
/* Flexbox */
.container {
display: flex;
justify-content: center;
align-items: center;
}
/* Grid */
.container {
display: grid;
place-items: center;
}
Both work. Grid's place-items: center is shorter. Flexbox is more familiar to most developers.
Responsive card grid
/* Grid — no media queries needed */
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.5rem;
}
This creates as many columns as fit, each at least 280px wide. As the viewport shrinks, columns automatically wrap to fewer per row.
Page layout with sidebar
.page {
display: grid;
grid-template-columns: 260px 1fr;
grid-template-rows: 60px 1fr 60px;
grid-template-areas:
'header header'
'sidebar main'
'footer footer';
min-height: 100vh;
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
.footer {
grid-area: footer;
}
Flexbox navigation bar
.nav {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 1.5rem;
height: 60px;
}
.nav-links {
display: flex;
gap: 1.5rem;
list-style: none;
}
The myth that you must choose one
You do not have to pick Flexbox or Grid for your entire project. The most common real-world pattern is to use them together:
- Grid for the macro layout: page structure, section divisions, dashboard panels
- Flexbox for the micro layout: components inside those sections — navbars, card interiors, button groups, form rows
A sidebar navigation built with Grid at the page level might use Flexbox inside each menu item to align an icon and label side-by-side. This is the intended use. They solve different problems and work together naturally.
Generate CSS layouts visually
Instead of memorizing every property, use the FixTools CSS Flexbox & Grid Builder to visually create layouts and copy the generated code directly. Adjust properties with a visual editor, see the layout update in real time, and copy clean CSS when you are done.
For production, run your CSS through the FixTools CSS Formatter to organize it, or through the CSS Minifier to reduce file size before deployment.
Try it free — right in your browser
No sign-up, no uploads. Your data stays private on your device.
Frequently asked questions
6 questions answered
QShould I use Flexbox or Grid for navigation bars?
Flexbox is almost always the right choice for navigation bars. Navigation items are a single row of links, and Flexbox handles one-dimensional alignment and spacing perfectly with properties like justify-content and gap. Grid adds unnecessary complexity for this use case.
QCan I use Flexbox and Grid together?
Yes, and you often should. A common pattern is to use Grid for the overall page layout (header, sidebar, main content, footer) and Flexbox inside individual components (a navigation bar, a card's content area, a button group). They are complementary, not competing.
QIs CSS Grid harder to learn than Flexbox?
Grid has more properties and concepts to learn initially because it controls two axes at once. Flexbox is easier to start with for simple one-dimensional layouts. Most developers learn Flexbox first, then add Grid for full-page or complex two-dimensional layouts.
QWhich has better browser support, Flexbox or Grid?
Both have excellent browser support in 2024. Flexbox has been supported since IE 11 (with prefixes) and is universally supported in modern browsers. CSS Grid is supported in all modern browsers since 2017. Neither requires any workarounds for current browser versions.
QWhat is the difference between grid-template-columns and flex-direction?
These control different things. grid-template-columns defines how many columns a grid has and their sizes (e.g., repeat(3, 1fr) creates three equal columns). flex-direction controls whether flex items run in a row or a column — it changes the main axis of the flex container.
QHow do I center something with Flexbox and with Grid?
With Flexbox on the parent: display: flex; justify-content: center; align-items: center. With Grid on the parent: display: grid; place-items: center. Both center the child element both horizontally and vertically. The Grid approach is shorter but both work reliably.
O. Kimani
Software Developer & Founder, FixTools
Building FixTools — a single destination for free, browser-based productivity tools. Every tool runs client-side: your files never leave your device.
About the authorRelated articles
Best Free JSON Formatter and Validator Online (2026)
Format messy JSON instantly and catch syntax errors before they break your app. Free browser-based JSON formatter and validator. No signup, no install.
Read articleDeveloper & WebHow to Minify HTML, CSS, and JavaScript Safely
Shrink your code for faster pages without breaking your site. What minification actually does, what to watch out for, and free tools to do it right.
Read article