Fixed-price website design from £2,400View Prices

CSS Grid vs Flexbox: Which Should You Use?

The two layout systems behind every modern website, what each is good at and a simple rule for choosing between them.

Layout and Structure guide. 6 minute read, with a live example. Updated .

In short

  • Flexbox lays items out along one line, a row or a column. Grid lays them out in rows and columns at the same time.
  • Use flexbox when the content should decide the sizes, such as a menu, a row of buttons or a toolbar.
  • Use grid when the layout should decide the sizes, such as a page structure, a card gallery or a form.
  • They are not rivals. Most pages use grid for the overall structure and flexbox inside the components.
  • Both are supported in every browser in use today, so there is no reason to fall back on floats or tables.

Every web page is a set of boxes that has to be arranged somehow. For most of the web's history that was done with tables and then with floats, both of which were made for something else. CSS now has two layout systems designed for the job: flexbox and grid. Between them they have turned layouts that once took hours of workarounds into a few lines of code.

The question that follows is which one to use. The honest answer is both, for different things, and the difference is easy to learn.

The Short Answer

Flexbox works in one dimension. It takes a set of items and distributes them along a single line, either a row or a column, deciding how they share the space and how they are aligned. Grid works in two dimensions. You define rows and columns, and items are placed into the cells they create.

QuestionFlexboxGrid
DimensionsOne: a row or a columnTwo: rows and columns together
Who is in chargeThe content. Items take the space they need and share out the rest.The layout. You define the tracks and the items fit into them.
Best forMenus, button groups, toolbars, centring and small componentsPage structure, card galleries, forms and anything that must line up in both directions
WrappingItems wrap onto new lines, but each line is laid out separatelyItems stay aligned to the same columns on every row
Overlapping itemsNot designed for itStraightforward: place two items in the same cells
Live example
Flexboxdisplay: flex; flex-wrap: wrap;
HomeOur servicesPricesPortfolio and case studiesAboutBlogContact us
Gridgrid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
HomeOur servicesPricesPortfolio and case studiesAboutBlogContact us
The same seven labels, laid out twice. Flexbox gives each one the width of its text and starts a new line wherever it runs out of room. Grid places them in equal columns that line up on every row. Narrow the window and both rearrange without a media query.

Tip: If you are arranging things in a line, reach for flexbox. If you find yourself wanting things to line up in both directions, you want grid.

How Flexbox Works

You make an element a flex container, and its children become flex items laid out along the main axis. A handful of properties control the rest: justify-content distributes items along the line, align-items positions them across it, gap sets the space between them and flex on an item says how it grows or shrinks.

.site-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 2rem;
}

.site-header nav {
  display: flex;
  gap: 1.5rem;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
A header with the logo at one end and the menu at the other, and a row of buttons that wraps when it runs out of room.

Flexbox is at its best when you do not know, or do not want to dictate, how wide things are. A menu with five links of different lengths, a list of tags, a card whose button should sit at the bottom however much text is above it: these are flexbox jobs.

How Grid Works

With grid you describe the structure first. grid-template-columns and grid-template-rows define the tracks, and the children fall into the cells in order unless you place them yourself. The fr unit shares out the free space in fractions, and minmax() sets limits.

.page {
  display: grid;
  grid-template-columns: 1fr min(65rem, 100% - 3rem) 1fr;
}

.page > * {
  grid-column: 2;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1.5rem;
}
A centred page column with a sensible maximum width, and a gallery that fits as many columns as the screen allows without a single media query.

Grid can also name the areas of a layout, which makes a page structure readable at a glance, and it lets items span several rows or columns, the basis of the bento grid layouts that have become so common.

.layout {
  display: grid;
  grid-template-columns: 16rem 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.layout > header { grid-area: header; }
.layout > aside  { grid-area: sidebar; }
.layout > main   { grid-area: main; }
.layout > footer { grid-area: footer; }
Named grid areas describe the page as a picture of itself.

When to Use Flexbox

  • Navigation bars and menus
  • A row of buttons or links that should wrap naturally
  • Centring one thing inside another, horizontally and vertically
  • Pushing one item to the far end of a row, such as a price at the right of a line
  • A card whose contents stack in a column with the button pinned to the bottom
  • Any small group of items whose sizes should depend on what is in them

When to Use Grid

  • The overall structure of a page: header, main content, sidebar and footer
  • Galleries of cards, products or photographs that must align in rows and columns
  • Forms where labels and fields line up across several rows
  • Layouts with items of different sizes, such as a feature panel beside smaller ones
  • Overlapping elements, such as text laid over part of an image
  • Anything you would once have reached for a twelve-column framework to build

A related feature, subgrid, lets a nested element line its own contents up with the tracks of the grid it sits in. It solves a long-standing problem: making the headings, text and buttons of neighbouring cards align with one another even when their content differs in length. It is supported in all current browsers.

Using Grid and Flexbox Together

The two systems are designed to be combined, and on a real page they nearly always are. Grid sets out the regions of the page and the gallery of cards. Inside each card, flexbox stacks the image, heading, text and button and pushes the button to the bottom. Inside the header, flexbox spaces out the logo and the menu.

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
  gap: 2rem;
}

.card {
  display: flex;
  flex-direction: column;
}

.card .button {
  margin-top: auto;
}
Grid arranges the cards. Flexbox arranges what is inside each one and keeps every button at the bottom.

Both systems are central to responsive web design, because each can adapt to the space available without a media query for every screen width. Clean layout code is also one of the practical differences between a hand-built website and a page builder, which our guide to custom websites and website builders compares.

Common Layout Mistakes

  • Building a grid out of flexbox. Percentage widths and negative margins on flex items were a workaround from before grid existed. They are harder to maintain, and the rows do not align.
  • Using grid for a simple row. A line of three links does not need tracks defined for it.
  • Margins instead of gap. The gap property works in both systems and puts space only between items, never around the outside.
  • Fixed pixel tracks. Columns set in pixels do not adapt. Use fr, minmax() and auto-fit.
  • Changing the visual order. Both systems can reorder items on screen, but keyboard and screen reader users still meet them in the order of the HTML. Keep the two the same.
  • Reaching for a framework first. A layout framework adds weight and a layer of class names to do what a few lines of grid now do natively.

Putting It Into Practice

Reading about a technique is the easy part. Applying it to a real business, with real content and a launch date, takes judgement about what to use and what to leave out. DigitalPod is a UK-based design and web development studio that designs every site from a blank page and builds it by hand, so a technique is used because it helps the business and not because a template came with it. If you would like a website designer to handle it for you, startup website design is what we specialise in, websites start from £2,400 and our website redesign service brings an older site up to date.

Frequently Asked Questions

Is CSS grid better than flexbox?

Neither is better. They solve different problems. Grid is better for two-dimensional layouts where rows and columns must align, and flexbox is better for distributing items along a single line. Most websites use both.

Can I use grid and flexbox together?

Yes, and you should. A common pattern is grid for the page structure and for galleries of cards, with flexbox arranging the contents of each card, the header and the button rows. A grid item can be a flex container, and the other way round.

Should I learn flexbox or grid first?

Flexbox is the gentler start, because it has fewer concepts and you will use it in almost every component. Learn grid straight afterwards. Together they cover nearly every layout you will need.

Is flexbox outdated now that grid exists?

No. Grid did not replace flexbox. Flexbox remains the right tool for one-dimensional layouts, and it is used inside most components on a modern page. What grid replaced was the practice of forcing flexbox and floats to behave like a grid.

Do all browsers support grid and flexbox?

Yes. Both have been supported by every major browser for years, including gap in flexbox and newer additions such as subgrid in current versions. There is no longer any need for fallback layouts built with floats or tables.

Want a Website Built This Way?

DigitalPod designs and builds websites for startups and growing UK businesses, with everything handled in-house from the first sketch to launch. Startup website design starts from £2,400, or you can read what clients say in our Trustpilot reviews.

Get a quote

Related Web Design Techniques

More from our web design techniques guides. For costs, briefs and choosing a designer, see the startup blog.