CSS Flexbox Versus Grid: When to Use Each
Flexbox and Grid are the two modern layout systems in CSS, and beginners often ask which one to learn first or which to use for a given page. The honest answer is that they solve related but different problems, and most real sites use both.
This guide explains how each one thinks about space, shows small examples, and gives you a practical rule of thumb for choosing.
The Core Difference
Flexbox is one dimensional. It lays items out along a single line, either a row or a column, and lets you control how they share the space on that line.
Grid is two dimensional. It lets you define rows and columns together and place items into the resulting cells.
A helpful summary: Flexbox works from the content outward, where items decide how much room they need. Grid works from the layout inward, where you define the structure first and then place content into it.
How Flexbox Works
You turn a container into a flex container, and its direct children become flex items.
.toolbar {
display: flex;
gap: 1rem;
align-items: center;
justify-content: space-between;
}
The key ideas are:
- Main axis and cross axis. With
flex-direction: row, the main axis runs horizontally and the cross axis vertically. - **
justify-content** distributes items along the main axis. - **
align-items** aligns them on the cross axis. - **
flex-grow,flex-shrinkandflex-basis** control how items grow, shrink and start out. - **
flex-wrap** allows items to flow onto a new line when space runs out, though each line is laid out on its own.
Flexbox shines for small components: navigation bars, button groups, form rows, card footers and anything where items sit in a line and should adapt to the space left over.
How Grid Works
You turn a container into a grid container and describe its tracks.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
The key ideas are:
- Tracks are the rows and columns you define with
grid-template-columnsandgrid-template-rows. - **The
frunit** divides available space into fractions. - **
gap** sets spacing between rows and columns. - Placement lets items span several tracks, using properties like
grid-columnandgrid-row. - Named areas let you draw the layout in text with
grid-template-areas.
Grid shines for page level structure and any layout where items need to line up in both directions at once, such as a header, sidebar, main area and footer, or a gallery of tiles.
A Responsive Grid With No Media Queries
One powerful Grid pattern creates as many columns as fit:
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 1.5rem;
}
The browser fits as many columns of at least 220 pixels as the container allows, and the columns share leftover space. Resizing the window changes the column count without any extra rules.
Content First or Layout First
Ask yourself which comes first in your thinking.
- If you already know the content and want it to flow naturally, such as a row of tags of varying length, use Flexbox.
- If you have a layout in mind and want content to fit into it, such as a three column dashboard, use Grid.
Another test: if you find yourself fighting wrapping lines in Flexbox to make items line up in columns, Grid is probably what you want. If you are creating a grid just to center one thing or space a handful of items, Flexbox is lighter.
Using Them Together
Combining the two is normal and encouraged. A common structure is:
- Grid for the overall page: header, sidebar, content and footer.
- Flexbox inside the header for the logo and navigation.
- Grid for a card list.
- Flexbox inside each card to push a button to the bottom.
Nesting is fine. A grid item can itself be a flex container, and the reverse.
Alignment in Both
Both systems share alignment properties from the same specification, which is helpful.
justify-contentandalign-itemswork in both.gapworks in both, and it replaced the older margin hacks.- Grid adds
justify-items,align-contentandplace-itemsfor finer control.
Centering something both ways is now short:
.center {
display: grid;
place-items: center;
}
Common Mistakes
- Using Flexbox for a full page layout and then fighting alignment between rows.
- Forgetting that only direct children become flex or grid items.
- Hard coding pixel widths instead of using flexible units such as
fr, percentages andminmax(). - Overusing nested containers when a single grid would do.
Browser Support and Tooling
Both are supported across current major browsers, so you rarely need fallbacks. Browser developer tools include layout inspectors that draw the tracks and flex lines, which makes debugging much easier. Open the inspector and toggle the overlay to see what is actually happening.
Frequently Asked Questions
Should I learn Flexbox or Grid first?
Many people start with Flexbox because small components are the most common need, then learn Grid for page structure. Either order works. The concepts, especially alignment and gap, carry over from one to the other.
Can Grid replace Flexbox completely?
Technically it could handle many jobs, but it would often be more work. Flexbox remains the better fit for one dimensional flows where content size drives the layout. Both tools coexist for good reason.
Is Grid bad for performance?
No. Both layout systems are efficient in current browsers for ordinary pages. Choose based on what makes the layout clearer and easier to maintain, not on speculation about speed.