2026-02-2811 min readStyleWars Team

CSS Flexbox vs Grid: When to Use Which (With Code Examples)

Not sure whether to use CSS Flexbox or Grid? This practical guide breaks down key differences, real-world use cases, and when to combine both.

cssflexboxgridlayouttutorial
Side-by-side comparison of CSS Flexbox and CSS Grid layouts

CSS Flexbox vs Grid: When to Use Which

The CSS flexbox vs grid debate is one of the most common questions developers face when building layouts. Both are powerful CSS layout systems, both have excellent browser support, and both can handle a surprising range of use cases. So which one should you actually reach for?

The short answer: it depends on what you are building. The longer answer is the rest of this article, where we will break down real differences, walk through practical code comparisons, and give you a decision framework you can use every day.

If you are still building your CSS fundamentals, our guide on whether learning CSS is worth it makes the case for why mastering layout is one of the highest-leverage skills in front-end development.

A Quick Overview of Flexbox

Flexbox (Flexible Box Layout) landed in browsers around 2012-2013 and was the first CSS module purpose-built for laying out items in a container. Its core idea is simple: you define a flex container, and its children become flex items that can grow, shrink, and distribute space along a single axis.

.container {
  display: flex;
  gap: 1rem;
  align-items: center;
}

Key characteristics of Flexbox:

  • One-dimensional -- it works along a single axis (row or column) at a time.
  • Items can flex to fill available space or shrink to prevent overflow.
  • Alignment along both axes is straightforward with justify-content and align-items.
  • Content-driven -- the size of items can influence the layout.

Flexbox excels when you need items to flow in a line and respond to the content inside them.

A Quick Overview of CSS Grid

CSS Grid Layout arrived a few years later (broad browser support from 2017) and introduced a fundamentally different model. Instead of working along one axis, Grid lets you define rows and columns simultaneously, creating a two-dimensional layout system.

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

Key characteristics of CSS Grid:

  • Two-dimensional -- it controls both rows and columns at the same time.
  • You define the layout structure explicitly on the container.
  • Items can span multiple rows and columns.
  • Layout-driven -- the grid defines where items go, regardless of content size.

Grid excels when you need precise control over placement in both dimensions.

The Core Difference: 1D vs 2D

The most important distinction when choosing between flexbox or grid comes down to dimensionality.

Flexbox is one-dimensional. Even when flex items wrap to a new line, each line is an independent flex container. The items in row two have no relationship to the items in row one -- they cannot align into columns.

Grid is two-dimensional. Rows and columns are defined together, so items in different rows inherently align to the same column tracks. The layout is cohesive across both axes.

Here is a side-by-side comparison that makes this concrete.

Flexbox: Items Wrap Independently

.flex-container {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.flex-container > .item {
  flex: 1 1 200px;
}

When items wrap, the last row might have fewer items that stretch to fill the space unevenly. Each row acts on its own.

Grid: Items Align to Tracks

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

Items always snap into defined column tracks. Even if the last row has fewer items, they occupy the same column widths as every other row. The result is a clean, predictable layout.

When to Use Flexbox

Reach for Flexbox when your layout problem is fundamentally about distributing items along a single line. Here are the strongest use cases.

Navigation Bars

A horizontal nav is the textbook Flexbox use case. Items flow in a row, you might want one group pushed to the left and another to the right, and the number of items may vary.

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  list-style: none;
}

Centering Content

If you need to center a single element both horizontally and vertically, Flexbox does it in three lines:

.center-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

Inline Form Controls

A search bar with an input and a button sitting side by side, where the input should grow to fill available space:

.search-bar {
  display: flex;
  gap: 0.5rem;
}

.search-bar input {
  flex: 1;
}

.search-bar button {
  flex-shrink: 0;
}

Card Footers and Toolbars

Any row of buttons, tags, or metadata that needs to distribute space evenly or push items to opposite ends is a natural fit for Flexbox.

If you enjoy writing tight, efficient CSS for challenges like these, you will appreciate our collection of CSS tips and tricks for writing less code.

When to Use Grid

Reach for Grid when you need to control placement in two dimensions or when the layout structure should be defined by the container rather than the content.

Page-Level Layouts

The classic header/sidebar/main/footer page structure is where Grid truly shines:

.page {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  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; }

Try doing that cleanly with Flexbox. You will end up nesting multiple flex containers and fighting with the markup order.

Card Grids and Galleries

When you want cards to form a uniform grid where every card aligns to both row and column tracks:

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
}

This single declaration handles responsiveness, consistent sizing, and proper alignment -- no media queries needed.

Dashboard Layouts

Dashboards often have widgets of varying sizes that need to span multiple columns or rows:

.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}

.widget-large {
  grid-column: span 2;
  grid-row: span 2;
}

.widget-wide {
  grid-column: span 2;
}

Overlapping Elements

Grid is the only CSS layout system that lets you overlap items without resorting to position: absolute:

.stack {
  display: grid;
}

.stack > * {
  grid-area: 1 / 1;
}

Every child occupies the same cell, stacking naturally. This technique is invaluable for image overlays, text on top of backgrounds, and creative effects -- exactly the kind of thing you will encounter in CSS battles on StyleWars.

When to Use Both Together

The best layouts often combine Grid and Flexbox. This is not an either/or decision. Use Grid for the macro layout and Flexbox for the micro layout within each grid cell.

Example: A Blog Post Layout

/* Grid for the overall page structure */
.blog-layout {
  display: grid;
  grid-template-columns: 1fr min(65ch, 100%) 1fr;
}

.blog-layout > * {
  grid-column: 2;
}

/* Flexbox for the post metadata row */
.post-meta {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

/* Flexbox for tag pills */
.tag-list {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

Example: E-Commerce Product Grid

/* Grid for the product listing */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 2rem;
}

/* Flexbox inside each product card */
.product-card {
  display: flex;
  flex-direction: column;
}

.product-card .card-body {
  flex: 1; /* Pushes footer to bottom */
}

.product-card .card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Grid handles the overall placement of cards. Flexbox handles the internal structure of each card, ensuring the footer stays pinned to the bottom regardless of how much content the card body contains.

Side-by-Side Comparison Table

| Feature | Flexbox | Grid | |---|---|---| | Dimension | One (row or column) | Two (rows and columns) | | Layout direction | Content-out | Layout-in | | Item sizing | Items can grow/shrink | Defined by track sizes | | Alignment | Single axis + cross axis | Both axes simultaneously | | Item placement | Sequential flow | Explicit coordinates | | Overlapping items | Requires positioning hacks | Native support | | Wrapping behavior | Independent lines | Cohesive tracks | | Best for | Components, inline layouts | Page layouts, grids | | Learning curve | Lower | Moderate | | Browser support | Excellent | Excellent |

Decision Flowchart: Flexbox or Grid?

Use this checklist when you are staring at a layout and wondering which tool to pick.

Start here: What are you building?

  1. Is the layout one-dimensional (a single row or column of items)?

    • Yes -- Use Flexbox.
  2. Do you need items to align across both rows and columns?

    • Yes -- Use Grid.
  3. Should the content determine the layout size (e.g., buttons that grow to fit their text)?

    • Yes -- Use Flexbox.
  4. Should the layout determine where content goes (e.g., fixed sidebar widths, named regions)?

    • Yes -- Use Grid.
  5. Do items need to overlap without absolute positioning?

    • Yes -- Use Grid.
  6. Are you building a small component inside a larger layout?

    • Probably Flexbox for the component, Grid for the outer layout.
  7. Still not sure?

    • Try Grid first. You can always fall back to Flexbox for the parts where it feels like overkill.

The key principle: Flexbox works from the content out; Grid works from the layout in. If you are arranging items and letting them dictate the space, that is Flexbox territory. If you are defining a structure and placing items into it, that is Grid territory.

Common Mistakes to Avoid

Using Flexbox for everything. Many developers learned Flexbox first and use it out of habit, even for two-dimensional layouts. If you find yourself nesting three or four flex containers to achieve a grid-like result, stop and switch to Grid.

Using Grid for a simple row of buttons. Grid works for this, but it is heavier than necessary. Flexbox handles inline component layouts more naturally and with less code.

Forgetting about gap. Both Flexbox and Grid support the gap property. Stop using margins on children to create spacing -- gap is cleaner, more predictable, and avoids the "extra margin on the last item" problem.

Avoiding Grid because it seems complex. The basics of Grid are not much harder than Flexbox. You can get very far with just grid-template-columns, gap, and the occasional span. You do not need to learn grid-template-areas or subgrid on day one.

Practical Advice for CSS Battle Players

If you compete in CSS battles on StyleWars or similar platforms, knowing when to use flexbox vs grid is a serious advantage. Here is how the choice plays out in a competitive context:

  • Centering a shape inside the viewport? Flexbox with justify-content: center and align-items: center -- three lines, done.
  • Recreating a layout with multiple elements in a grid pattern? CSS Grid with explicit row and column placement will get you pixel-perfect results faster.
  • Stacking elements on top of each other? Grid's implicit stacking (grid-area: 1/1 on all children) saves you from managing position: absolute and coordinates.
  • Building a symmetrical pattern? Grid's repeat() and fractional units keep things precise with minimal code.

For a deeper dive into competitive CSS techniques, check out our ultimate guide to CSS battles. And if you want to test your layout skills head-to-head against another developer, try 1v1 mode on StyleWars.

Wrapping Up

The CSS flexbox vs grid question does not have a single right answer, but it does have clear guidelines. Flexbox is your tool for one-dimensional, content-driven layouts -- nav bars, button rows, centering, and component internals. Grid is your tool for two-dimensional, structure-driven layouts -- page layouts, card grids, dashboards, and anything that needs items to align across both axes.

The best developers do not pick one and ignore the other. They use both, often in the same component, letting each tool handle what it does best. Start with the decision flowchart above, build a few layouts with each approach, and the right choice will quickly become instinct.

Now go build something. Whether it is a production app or a CSS battle on StyleWars, the only way to internalize these layout tools is to use them.

Sharpen Your CSS Skills

Put what you learned into practice. Try a CSS battle and see how you compare against other developers on the leaderboard.

More from the Blog