SiteTidy
HomeGuidesMastering CSS Grid Layouts (Part 1): The Mental Model That Makes Grid Click
Design

Mastering CSS Grid Layouts (Part 1): The Mental Model That Makes Grid Click

Learn CSS Grid from the layout model up: tracks, lines, explicit and implicit grids, fr units, minmax(), intrinsic sizing, auto-placement and the patterns that make responsive layouts predictable.

Published on August 15, 2026Updated August 31, 2026
Mastering CSS Grid Layouts (Part 1): The Mental Model That Makes Grid Click

CSS Grid becomes much easier when you stop thinking of it as “Flexbox with more columns”.

Flexbox is primarily a one-dimensional layout system: you are normally arranging items along a row or a column and letting the cross-axis follow. Grid is two-dimensional. You define a system of rows and columns, then place content into that system — explicitly, automatically, or with a mixture of both.

That difference sounds small until you build something real.

A card list, admin dashboard, pricing table, image gallery or documentation shell can all be written with a few Grid declarations. The hard part is not memorising display: grid. The hard part is understanding what the browser is sizing, why a track refuses to shrink, why 1fr sometimes still overflows, why grid-column: 1 / -1 works in one place and not another, and when Grid should be allowed to place items for you.

This first part builds that mental model properly.

Start with the right vocabulary: container, items, tracks and lines

Given this markup:

<div class="layout">
  <aside>Navigation</aside>
  <main>Content</main>
</div>

and this CSS:

.layout {
  display: grid;
  grid-template-columns: 16rem 1fr;
}

.layout is the grid container.

Its direct children — aside and main — become grid items.

The declaration creates two column tracks:

|------ 16rem ------|-------------- 1fr --------------|

Those tracks are separated and bounded by grid lines:

line 1        line 2                              line 3
  |-------------|-----------------------------------|

This matters because most Grid placement is really line placement.

When you write:

main {
  grid-column: 2 / 3;
}

you are saying:

Start this item at column line 2 and end it at column line 3.

Once you see the layout as tracks bounded by numbered lines, many Grid properties stop feeling arbitrary.

Rows and columns create the explicit grid

These properties define tracks directly:

.container {
  display: grid;
  grid-template-columns: 18rem 1fr 20rem;
  grid-template-rows: auto 1fr auto;
}

That creates an explicit grid of three columns and three rows.

                columns
         18rem    1fr      20rem
       ┌────────┬────────┬────────┐
 auto  │        │        │        │
       ├────────┼────────┼────────┤
 1fr   │        │        │        │
       ├────────┼────────┼────────┤
 auto  │        │        │        │
       └────────┴────────┴────────┘

The explicit grid is important because some placement behaviour — notably negative line numbers such as -1 — refers to the explicit grid’s lines.

Grid can also create tracks you never declared

Suppose you define only columns:

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

but have twelve cards.

You did not define any rows. Grid creates them for you as needed.

Those automatically created rows form the implicit grid.

By default, implicit tracks are auto-sized. You can control them with:

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(8rem, auto);
}

Likewise, implicit columns can be sized with grid-auto-columns.

This distinction explains a classic Grid surprise.

You might expect:

.item {
  grid-row: 1 / -1;
}

to span every row that eventually exists.

But -1 means the final line of the explicit grid, not “whatever the final implicit row turns out to be after auto-placement”.

That is a subtle rule worth understanding before debugging it for an hour.

fr means a share of leftover space, not a percentage

The fr unit is one of Grid’s best features.

.layout {
  display: grid;
  grid-template-columns: 15rem 1fr 2fr;
}

After Grid accounts for fixed sizing and other track constraints, the remaining distributable space is split into fractional shares.

The second track receives one share. The third receives two.

If 900px of distributable space remains:

1fr = 300px
2fr = 600px

But this is where an important misconception appears:

1fr does not necessarily mean “this track may shrink all the way to zero”.

Grid items have automatic minimum sizing behaviour, and their content can force a track wider than you expect.

Why 1fr sometimes overflows

Consider:

.layout {
  display: grid;
  grid-template-columns: 240px 1fr;
}

Inside the second column is an element containing an extremely long unbreakable string, wide table, code block or other intrinsically wide content.

You may see horizontal overflow even though the column says 1fr.

One reliable fix is:

.layout {
  grid-template-columns: 240px minmax(0, 1fr);
}

Why does this work?

Because a bare 1fr track participates in Grid’s automatic minimum sizing behaviour. minmax(0, 1fr) explicitly says:

This track may shrink as far as zero, then take one fractional share of available space.

You will see this pattern constantly in robust application layouts:

grid-template-columns: minmax(0, 1fr) 20rem;

or:

grid-template-columns: 16rem minmax(0, 1fr);

There is another useful fix at the item level:

main {
  min-width: 0;
}

The right choice depends on whether you want the constraint expressed at the track or item level.

minmax() is one of the most useful functions in responsive CSS

minmax(min, max) defines a track whose size may vary between a lower and upper bound.

For example:

.grid {
  grid-template-columns: repeat(3, minmax(12rem, 1fr));
}

Each column:

  • should not become narrower than 12rem;
  • may expand to consume an equal fraction of the available space.

This becomes especially useful with automatic repetition.

The responsive card-grid pattern worth memorising

For many card layouts, this one declaration removes the need for several breakpoint rules:

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

Read it as:

Create as many columns as fit. Each column wants to be at least 16rem wide, and if extra room remains, distribute it between the columns.

At wide sizes you may get four columns. Then three. Then two. Eventually one.

The layout responds to the available container width, not to a collection of device names.

That is often more resilient than:

@media (max-width: 1200px) { ... }
@media (max-width: 900px) { ... }
@media (max-width: 640px) { ... }

Breakpoints still have a place, but do not introduce them merely because you have forgotten what Grid can already express.

auto-fit and auto-fill are similar until there is spare room

These two are frequently explained badly.

Compare:

repeat(auto-fit, minmax(14rem, 1fr))

with:

repeat(auto-fill, minmax(14rem, 1fr))

Both calculate how many tracks can fit.

The difference becomes visible when there are fewer items than the number of tracks the container could hold.

auto-fill keeps the empty tracks in the grid.

auto-fit collapses empty tracks, allowing occupied tracks to stretch into that space.

For typical responsive card lists, auto-fit often gives the behaviour people expect:

3 cards in a wide container

AUTO-FILL:
| card | card | card | empty |

AUTO-FIT:
|    card    |    card    |    card    |

But neither is universally “better”. If empty track structure matters, auto-fill may be exactly what you want.

Intrinsic sizing: min-content and max-content

Grid becomes much more expressive when you understand content-based sizing.

min-content

min-content represents roughly the smallest width the content can take without avoidable overflow — often governed by the longest unbreakable piece of content.

max-content

max-content represents the width the content would prefer if it were laid out without wrapping.

Consider a toolbar:

.toolbar {
  display: grid;
  grid-template-columns: max-content 1fr max-content;
  gap: 1rem;
}

The left and right controls take the width their content naturally needs. The centre consumes the remainder.

That can be much cleaner than guessing fixed widths for buttons or labels.

fit-content() lets content grow — up to a point

You can constrain intrinsic growth with fit-content():

.sidebar {
  display: grid;
  grid-template-columns: fit-content(22rem) 1fr;
}

The first track can size itself according to its content but will not simply keep expanding beyond the supplied limit in the same way an unconstrained max-content track might.

It is particularly useful for labels, navigation columns and content-driven sidebars.

repeat() is more than syntax sugar

Instead of:

grid-template-columns: 1fr 1fr 1fr 1fr;

write:

grid-template-columns: repeat(4, 1fr);

But repeat() becomes more powerful when combined with named lines and responsive repetition:

grid-template-columns: repeat(12, [col-start] minmax(0, 1fr));

or:

grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));

The function is not just about shortening repetitive declarations. It is part of Grid’s track-generation model.

Grid auto-placement is useful — let it do some work

You do not need to manually place every grid item.

Given:

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

children with no explicit position are automatically placed into successive cells.

Grid’s default auto-flow is by row.

1 2 3 4
5 6 7 8

You can change it:

.gallery {
  grid-auto-flow: column;
}

although column flow generally requires you to think carefully about row definitions or you may create a long horizontal grid.

For ordinary galleries and card collections, default auto-placement is usually exactly what you want.

Explicitly place only the exceptional items

A nice Grid pattern is to let most items auto-place while giving special items explicit spans.

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

.featured {
  grid-column: span 2;
  grid-row: span 2;
}

Now ordinary cards flow naturally, while .featured occupies a larger region.

You do not need to assign coordinates to every sibling.

This tends to be more maintainable when content is dynamic.

span is often easier to maintain than hard-coded end lines

These two declarations can produce equivalent placement in the right context:

grid-column: 2 / 4;

and:

grid-column: 2 / span 2;

The second says “start at line 2 and occupy two tracks”.

That is often easier to reason about when you care about the item’s width in tracks rather than the exact numerical end line.

For auto-placed items, you can simply say:

.featured {
  grid-column: span 2;
}

and let Grid find the starting position.

gap belongs to the layout, not the children

Older CSS layouts often created spacing using margins on each child:

.card {
  margin-right: 1rem;
  margin-bottom: 1rem;
}

Grid gives the container ownership of spacing:

.cards {
  display: grid;
  gap: 1rem;
}

You can separate row and column spacing:

.cards {
  row-gap: 2rem;
  column-gap: 1rem;
}

This is cleaner because spacing describes the relationship between tracks rather than being attached arbitrarily to item edges.

Alignment: tracks versus content inside cells

Grid has several alignment properties, and confusion usually comes from forgetting what is being aligned.

Align items inside their grid areas

Use:

.container {
  justify-items: start;
  align-items: center;
}

These affect the items within their grid areas.

Align the whole grid inside its container

If the grid itself does not consume all available space, use:

.container {
  justify-content: center;
  align-content: start;
}

These act on the track collection as a whole.

A useful shorthand distinction is:

*-items   -> items inside grid areas
*-content -> grid tracks inside the container

That is not the entire alignment specification, but it is a good debugging mental model.

A real application shell

Here is a robust two-column application layout:

.app-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
}

.app-header {
  grid-column: 1 / -1;
}

.sidebar {
  grid-column: 1;
  grid-row: 2;
  overflow: auto;
}

.content {
  grid-column: 2;
  grid-row: 2;
  min-width: 0;
  overflow: auto;
}

Why the minmax(0, 1fr) choices?

Because application shells frequently contain tables, code editors, charts or long text. Allowing flexible tracks to shrink below their automatic content minimum prevents the entire page from being pushed wider or taller than intended.

This is the kind of detail that separates a demo layout from one that survives real content.

Responsive layout without forcing every component through media queries

A typical dashboard section can be written like this:

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  gap: 1rem;
}

The extra min(100%, 16rem) helps avoid a minimum track width being wider than the container on extremely narrow viewports.

Then a larger page shell can still use a semantic breakpoint when its layout mode genuinely changes:

.dashboard {
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
}

@media (max-width: 48rem) {
  .dashboard {
    grid-template-columns: minmax(0, 1fr);
  }
}

This is a better reason for a breakpoint: the sidebar/content relationship changes, not merely because a popular phone width was copied from a framework.

Building a layout is only half the job

Once your responsive layout is live, SiteTidy can help inspect the surrounding performance, HTML, metadata and security quality of the deployed page.

Run Free Audit →

Common Grid mistakes

Using Grid for a simple one-dimensional row by default

If you have five buttons that simply need spacing along one axis, Flexbox may express the layout more directly.

Use Grid because the rows/columns relationship matters, not because Grid is newer.

Hard-coding every item coordinate

Dynamic content becomes painful when item 7 is always expected to occupy row 3 column 2.

Let auto-placement do ordinary work and explicitly place only the exceptional items.

Assuming 1fr always prevents overflow

Wide intrinsic content can still affect track sizing. Remember minmax(0, 1fr) and min-width: 0.

Creating breakpoints that Grid could avoid

Before adding a media query to change four columns to three, try:

repeat(auto-fit, minmax(...))

Reordering visual content without thinking about source order

Grid can visually place content in ways that differ from DOM order.

That does not automatically change the meaningful reading or focus order used by assistive technologies and keyboard navigation.

Do not create a beautiful visual sequence that becomes nonsensical when read in source order.

Debug Grid with browser DevTools

Modern browser developer tools have excellent Grid overlays.

Use them.

Turn on the grid overlay and inspect:

  • numbered grid lines;
  • track boundaries;
  • gaps;
  • named lines where present;
  • implicit versus explicit tracks;
  • item areas.

When a Grid layout surprises you, visually inspecting the tracks is usually faster than randomly changing CSS values.

Ask:

  1. Which tracks actually exist?
  2. Which are explicit?
  3. Which were created implicitly?
  4. What is the minimum sizing behaviour of the problematic track?
  5. Is the item explicitly placed or auto-placed?
  6. Is intrinsic content forcing a larger minimum size?

Grid becomes very predictable when you debug the model rather than the screenshot.

The CSS worth taking away from Part 1

These patterns cover a surprising amount of real UI work:

/* Equal columns */
grid-template-columns: repeat(3, minmax(0, 1fr));

/* Sidebar + flexible content */
grid-template-columns: 16rem minmax(0, 1fr);

/* Responsive cards */
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));

/* Full-width item */
grid-column: 1 / -1;

/* Item occupying two tracks */
grid-column: span 2;

/* Controlled automatic rows */
grid-auto-rows: minmax(8rem, auto);

/* Track-based spacing */
gap: 1rem;

Do not just memorise the declarations. Know why they work.

Grid is a sizing system before it is a placement system

Developers often approach CSS Grid by asking, “How do I put this box in column three?”

That is useful, but the deeper skill is understanding how the browser determines the sizes of the columns in the first place.

Tracks can be fixed, flexible, intrinsic, constrained or automatically generated. Items can have minimum content requirements. The explicit grid can coexist with an implicit one. Auto-placement can do most of the repetitive work while selected items span or occupy named regions.

Once that model is clear, Grid stops feeling magical.

In Part 2, we move from track sizing into deliberate placement: line numbers, named lines, grid areas, overlapping content and maintainable page layouts that do not turn into a pile of unexplained coordinates.

Ready to audit your site?

Put this guide into practice immediately using our free tools.

Browse 180+ Free Tools