CSS Grid and Flexbox are the two layout tools every modern website is built on. They overlap a little, which is why people often ask which one is "better". Neither is. They solve different problems, and most good layouts use both.
This guide explains the difference in plain terms, shows the handful of properties you will use most, and ends with a simple way to decide which one to reach for.
The one-sentence difference
Flexbox arranges items along one line. Grid arranges items in rows and columns at the same time.
If you are lining things up in a row or a column, such as a navigation bar or a row of buttons, that is a job for Flexbox. If you are placing things on a two-dimensional layout, such as a page with a sidebar or a gallery of cards, that is a job for Grid.
Flexbox basics
You turn an element into a flex container with display: flex. Its direct children become flex items and line up along the main axis, which is a row by default.
.nav {
display: flex;
justify-content: space-between; /* spread along the row */
align-items: center; /* centre vertically */
gap: 1rem; /* space between items */
}The properties you will use most:
flex-direction–roworcolumn.justify-content– how items are spaced along the main axis.align-items– how items line up across the other axis.gap– space between items, without margins.flex-wrap: wrap– lets items move to a new line when there is no room.flex: 1– on an item, tells it to grow and fill the free space.
Flexbox is content-driven. Each item starts from the size of its content and then grows or shrinks. That is ideal for menus, toolbars, form rows, and for centring one thing inside another.
Grid basics
Grid works the other way round. You describe the layout first, and the items fill it.
.page {
display: grid;
grid-template-columns: 250px 1fr; /* sidebar + flexible content */
gap: 2rem;
}The fr unit means "a fraction of the free space". 1fr 1fr 1fr gives three equal columns, and repeat(3, 1fr) is a shorter way to write the same thing.
One line that is worth remembering builds a responsive card grid without any media queries:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 1.5rem;
}It reads as: fit as many columns as you can, each at least 260px wide, and share any leftover space equally. On a phone that gives one column, on a tablet two, and on a desktop three or four.
You can also place items exactly. grid-column: span 2 makes a card twice as wide, and grid-template-areas lets you name the regions of a page such as header, sidebar, main and footer.
Using them together
In practice you nest one inside the other. A common pattern is Grid for the page and for groups of cards, and Flexbox inside each component.
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 1.5rem; }
.card { display: flex; flex-direction: column; }
.card .actions { margin-top: auto; } /* pushes the button to the bottom */Grid keeps every card the same width and height. Flexbox, inside the card, stacks the image, title and text, and margin-top: auto pins the button to the bottom so all the buttons line up across the row.
Common mistakes
- Setting fixed widths on flex items. Let the content and
flexvalues do the work, or the layout will overflow on small screens. - Long words breaking the layout. Flex and grid items will not shrink below their content by default. Adding
min-width: 0to the item usually fixes it. - Using margins for spacing.
gapworks in both Flexbox and Grid and avoids the extra margin on the last item. - Writing a media query for every screen size. Try
auto-fitwithminmax(), orflex-wrap, first. You will need far fewer breakpoints. - Changing the visual order. Properties like
ordermove things on screen but not for keyboard and screen-reader users. Keep the HTML in a sensible reading order.
A quick way to choose
- Is it a single row or column of items? Use Flexbox.
- Do rows and columns both need to line up? Use Grid.
- Do you want the content to decide the sizes? Flexbox.
- Do you want the layout to decide the sizes? Grid.
- Not sure? Start with Grid for the outer layout and Flexbox for what goes inside each box.
Frequently asked questions
Should I learn Flexbox or Grid first?
Flexbox is a little easier to start with because it only works in one direction. Learn it first, then Grid. Together they cover almost every layout you will build.
Do all browsers support them?
Yes. Both have been supported by all current browsers for years, including the gap property in Flexbox.
Do I still need a CSS framework?
Not for layout. Frameworks such as Tailwind or Bootstrap use Flexbox and Grid underneath, so understanding the two makes any framework easier to use, and often lets you do without one.
Can I use Grid inside Flexbox, or the other way round?
Yes, in any combination. A flex item can be a grid container and a grid item can be a flex container.