The main difference between CSS Flexbox and CSS Grid is direction. Flexbox lays items out in one dimension, either a row or a column, and lets the content decide how much space each item takes. Grid lays items out in two dimensions, rows and columns together, on a structure you define first. Use Flexbox for things like menus and button groups, and Grid for page layouts and card grids.
This guide explains how each one works, shows short examples of both, and gives a simple way to decide which to use for menus, cards, columns and whole pages.
How Flexbox works
You turn an element into a flex container with display: flex;. Its direct children then line up in a row (the default) or a column.
.nav { display: flex; gap: 16px; align-items: center; }
The properties you will use most:
- flex-direction:
roworcolumn, the direction items flow. - justify-content: spacing along the main direction, such as
space-betweenorcenter. - align-items: alignment across the other direction, such as
centerfor vertical centering in a row. - flex-wrap:
wraplets items move onto a new line when they run out of room. - gap: space between items, without margin tricks.
- flex: on a child, controls how it grows and shrinks.
flex: 1;lets an item take up the remaining space.
Flexbox is content-first. Items start at their natural size and then grow or shrink to fill the line. That makes it ideal when you do not know exactly how wide each item will be, such as menu links of different lengths.
How Grid works
You turn an element into a grid container with display: grid;, then describe the columns (and optionally rows). Children are placed into the cells.
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 24px; }
The key ideas:
- grid-template-columns and grid-template-rows define the tracks. The
frunit means a fraction of the free space, so1fr 2frmakes the second column twice as wide as the first. - gap sets the space between rows and columns.
- grid-column and grid-row on a child let it span several tracks, for example
grid-column: span 2;. - grid-template-areas lets you name regions such as header, sidebar and main, and place items by name.
Grid is layout-first. You draw the structure, and items fit into it. Rows and columns stay aligned with each other, which Flexbox cannot guarantee across separate lines.
Flexbox vs Grid side by side
| Flexbox | Grid | |
|---|---|---|
| Dimensions | One: a row or a column | Two: rows and columns together |
| Approach | Content decides the size | Layout decides the size |
| Best for | Navigation bars, toolbars, button rows, centering | Page layouts, card galleries, forms with aligned labels |
| Wrapping | Wrapped lines are sized independently | Every row lines up with the same columns |
| Overlapping items | Awkward | Easy, by placing items in the same cell |
| Browser support | All current major browsers | All current major browsers |
When to use Flexbox
Reach for Flexbox when items sit in a single line and you want them to share space sensibly.
Navigation bars
A logo on the left and links on the right is a classic Flexbox job: header { display: flex; justify-content: space-between; align-items: center; }
Centering something
To center a box both ways inside its parent, give the parent display: flex; justify-content: center; align-items: center;.
Inside a card
To push a card's button to the bottom, however long the text above it is, make the card a column (display: flex; flex-direction: column;) and give the button margin-top: auto;.
Rows of tags or buttons
Items of different widths that should wrap naturally onto new lines work well with flex-wrap: wrap; and a gap.
When to use Grid
Reach for Grid when you care about rows and columns at the same time, or when you want to define the layout before the content.
Card galleries that adapt to the screen
This one line creates as many columns as fit, each at least 250 pixels wide, with no media queries:
.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; }
Unlike a wrapping Flexbox row, the last row of cards stays lined up with the columns above.
Whole page layouts
A header, sidebar, main area and footer map naturally to named grid areas. For example, grid-template-columns: 240px 1fr; gives a fixed sidebar and a flexible content column.
Forms
A two-column grid with labels in one column and inputs in the other keeps every field aligned, whatever the label lengths.
Using them together
You do not have to choose one for the whole site. The most common pattern is:
- Use Grid for the overall page structure and for grids of cards.
- Use Flexbox inside components, such as the navigation bar, the contents of each card, and button groups.
A grid item can itself be a flex container, and a flex item can be a grid container. Mixing them like this is normal and keeps each piece of CSS simple.
A quick test: if you are thinking "these items should sit next to each other", start with Flexbox. If you are thinking "this should look like a grid with columns", start with Grid.
Both layouts also make responsive design much easier. A grid can change from three columns to one with a single media query, and a flex row can switch to a column on phones. See how to make a website responsive for the full approach.
The easier way: Flexbox and Grid with visual controls
Remembering which property does what takes practice. Apollo Editor, a Windows desktop HTML editor, lets you build these layouts visually and see the CSS it creates.
- Drag-and-drop components include columns, flexbox containers and CSS grid layouts, all editable visually.
- The CSS Style Designer has visual controls for display, Flexbox and Grid, along with margin, padding and size.
- Responsive breakpoints and a Preview mode with desktop, tablet and mobile sizes show how the layout adapts.
- Split mode keeps the page and its code side by side, so you learn the properties as you go.
The output is standard CSS in your own files. If you are new to stylesheets, start with CSS basics for beginners, or see the features.
Frequently asked questions
Is CSS Grid better than Flexbox?
Neither is better. Flexbox suits one-dimensional rows or columns, and Grid suits two-dimensional layouts with rows and columns together.
Can I use Flexbox and Grid together?
Yes. A common pattern is a Grid for the overall page and Flexbox inside components such as navigation bars and cards.
Do all browsers support Flexbox and Grid?
All current major browsers support both, so they are safe to use for modern websites.
