loading bytebasex.com

CSS Grid Explained With Real Layout Examples

Master CSS Grid with clear examples covering fr units, responsive card grids without media queries, named grid areas, and when to choose Grid.

You're building a page layout: a header, a sidebar, a main content area, and a footer. With flexbox, you end up nesting wrapper divs inside wrapper divs, fighting to get two dimensions (rows and columns) to line up at the same time, because flexbox was only ever built to handle one dimension well.

This is exactly the gap CSS Grid was designed to fill. Where flexbox thinks in a single line, Grid thinks in a full two-dimensional layout, rows and columns together, from the start.

CSS Grid Explained With Real Layout Examples

Welcome to BytebaseX. I'm Suptojit Modak, and in this guide, I'll walk through CSS Grid from the ground up: the properties that actually matter day to day, one trick that builds fully responsive layouts without a single media query, and when you should genuinely reach for Grid instead of flexbox.

The One-Sentence Version

Flexbox arranges items along one line, either a row or a column, and lets that line wrap. Grid arranges items into an actual grid of rows and columns simultaneously, where every item can be placed by its position in both dimensions at once.

Think of it like this: flexbox is a single shelf. You can push books together, spread them out, or let them wrap onto a new shelf below. Grid is the whole bookcase, with defined rows and columns, where you can tell a specific book "go on shelf 2, column 3" directly.

Turning On Grid

Grid works the same way flexbox does at the starting line: apply display: grid to a parent element, and its direct children automatically become grid items.

.container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  gap: 16px;
}

That's three fixed-width columns, each 200 pixels wide, with 16 pixels of space between every item, both horizontally and vertically. Any direct child of .container will automatically flow into this grid, filling the first row's three columns, then wrapping to a new row for the next three items, without you writing a single extra line of CSS.

The fr Unit: Grid's Best Trick

Fixed pixel columns work, but they're rigid. Grid introduces a unit called fr (a "fraction" of the remaining available space), which is where Grid starts feeling genuinely different from anything flexbox offers directly:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
}

This creates three columns of exactly equal width, and critically, that width automatically recalculates as the container resizes. No percentages to calculate, no calc() needed.

fr units don't have to be equal either:

.container {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 16px;
}

This gives you a two-column layout where the first column is exactly twice as wide as the second, a common pattern for a main content area next to a narrower sidebar, and it stays proportional at every screen size automatically.

A Live Demo: Fixed vs fr Columns

Numbers on a screen are one thing, watching a layout actually respond is another. Try the buttons below and drag your browser window afterward to see the difference.

grid-template-columns: 100px 100px 100px;

1
2
3

Try resizing your browser window after clicking "Equal fr columns" or the "2fr / 1fr split" button.

Named Grid Areas: Layout That Reads Like a Diagram

This is the feature that has no real flexbox equivalent, and it's often the single biggest reason people switch to Grid for page-level layouts. Instead of positioning items with row and column numbers, you can name regions of your grid and place items into them by name:

.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar main"
    "sidebar footer";
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

Read that grid-template-areas block literally: it's an actual ASCII picture of your layout. The sidebar spans all three rows because its name repeats in every row. The header, main content, and footer stack in the second column. Anyone reading this CSS for the first time can understand the page structure just by looking at the shape of the text, without mentally simulating how a dozen flexbox rules interact.

Success: Named areas also make responsive rearranging trivial. Inside a media query, just redefine grid-template-areas with a different layout, like stacking everything into a single column on mobile, and every element automatically moves to its new spot since it's still referencing the same area names.

Responsive Grids Without a Single Media Query

Here's the trick that genuinely surprises people the first time they see it. Combine repeat(), auto-fit, and minmax(), and you get a fully responsive card grid with zero media queries:

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

Read this as: "fit as many columns as possible, each at least 200 pixels wide, and let them grow evenly with 1fr to fill any leftover space." On a wide desktop screen, that might mean five columns. On a phone, it collapses down to a single column automatically, no breakpoints written anywhere, because the browser recalculates how many 200-pixel-minimum columns actually fit at every width.

auto-fit vs auto-fill: The Difference That Actually Matters

These two look nearly identical and behave identically until your grid has fewer items than could fit in a row, which is exactly when the difference shows up:

  • auto-fit collapses empty tracks down to zero width, letting the items that do exist stretch to fill the remaining space.
  • auto-fill keeps those empty tracks at their minimum width, leaving visible gaps rather than letting existing items grow into that space.

For most card-grid layouts, where you want existing cards to stretch and fill the row nicely when there aren't enough to complete it, auto-fit is almost always what you actually want. Reach for auto-fill only in the rarer case where you specifically want those empty slots preserved, like a layout being fed dynamically where new items might slot into those gaps later.

Grid vs Flexbox: When to Actually Use Which

These two aren't competitors, they solve different shapes of problem, and most real projects end up using both together: Grid for the overall page skeleton, flexbox for arranging items inside individual components.

SituationBetter fit
Overall page layout (header, sidebar, main, footer)Grid
A row of nav links or buttonsFlexbox
A responsive card galleryGrid
Centering one or two itemsFlexbox (or Grid, both work fine)
A layout with items that need exact row and column alignmentGrid
Content that only needs to flow in one direction and wrapFlexbox

If you're still working through when and why to center things with flexbox specifically, that's covered in more depth in Why Centering Div Confuses Everyone. The short version for Grid: centering a single item is just as easy here too, using place-items: center on the container as a one-line alternative to flexbox's justify-content and align-items pair.

Common Mistakes With Grid

Forgetting That Grid Items Aren't Automatically Flex-Wrapped Content

If you put a long paragraph of text inside a narrow grid column, it wraps normally, since Grid controls the container's structure, not the text flow inside each item. This trips people coming from flexbox who expect identical wrapping behavior everywhere.

Overusing Explicit Line Numbers

You can place items by grid line number, like grid-column: 2 / 4, and it works, but it's brittle. Add or remove a column later, and every hardcoded line number throughout your CSS needs updating. Named areas, covered above, age far better for anything beyond a quick one-off layout.

Not Using gap

Before gap was well-supported, people used margins on individual grid items to create spacing, which gets messy fast, especially at the edges of the grid. Modern gap handles spacing between grid items cleanly, without adding unwanted space around the grid's outer edge.

Quick Reference

PropertyWhat it does
display: grid;Turns an element into a grid container
grid-template-columnsDefines the number and width of columns
grid-template-rowsDefines the number and height of rows
gapSpace between grid items, rows and columns
frA flexible unit representing a share of remaining space
repeat(auto-fit, minmax(200px, 1fr))A responsive column pattern with no media queries needed
grid-template-areasNames regions of the grid for readable, easy-to-rearrange layouts
place-items: center;Centers grid items both horizontally and vertically in one line

Frequently Asked Questions

Can I use Grid and Flexbox together in the same project?

Yes, and this is genuinely the normal way most real projects use both. A common pattern is Grid for the overall page structure, and flexbox inside individual components, like a navigation bar or a card's internal content, where a single-direction layout is all that's needed.

Do I need to specify both rows and columns for Grid to work?

No. If you only define grid-template-columns, Grid will automatically create rows as needed to fit your content, using a default row height based on the content itself. You only need grid-template-rows when you want explicit control over row sizing.

Is CSS Grid supported in all modern browsers?

Yes, CSS Grid has full support across all current major browsers, including Chrome, Firefox, Safari, and Edge, and has for several years now. There's no practical compatibility concern for a modern website in 2026.

What's the difference between auto-fit and auto-fill?

Both fit as many columns as possible based on a minimum width. The difference shows up when there are fewer items than could fill a row: auto-fit collapses the empty columns and lets existing items stretch to fill the space, while auto-fill keeps those empty column tracks reserved, leaving visible gaps instead.

Can grid items overlap each other?

Yes, intentionally. If two items are placed on overlapping grid lines or the same named area, they'll stack on top of each other, and you can control which one appears on top using z-index. This is occasionally used deliberately for layered design effects, like text overlapping a background image.

Conclusion

Grid isn't a replacement for flexbox, it's the tool for a different shape of problem: real two-dimensional layouts where rows and columns both matter at once. Learn grid-template-columns with fr units, the auto-fit and minmax() combination for responsive grids, and named grid areas for anything page-level, and you'll cover the vast majority of layouts you'll actually build.

The fastest way to make any of this stick is the same advice that applies to most of CSS: open a blank HTML file, paste in a few colored boxes, and try changing one property at a time until you can predict what each one does before you hit save.

For how Grid fits alongside the box model, positioning, Flexbox, and centering as one connected system, see The Complete Guide to CSS Layout.

About the author

Suptojit Modak
I'm Suptojit Modak, a web developer and the person behind BytebaseX — a blog with tutorials, guides, and free resources for developers and bloggers, built to be simple and easy to follow.

Instagram · GitHub · Facebook

Post a Comment