SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/grid-layouts

7 CSS Grid Layouts

7 Pure CSSPublished 2026-10-05Free · no sign-up

Seven demos of CSS Grid features: auto-fill and auto-fit side by side, a page described with named areas, cards aligned with subgrid, items spanning rows and columns, gaps filled with dense packing, a caption layered over an image on one grid and the robust min() column formula.

01. auto-fill vs auto-fit

GridRepeat

Two grids with the same three items: auto-fill keeps empty column tracks when there is spare room, so items stay their minimum size; auto-fit collapses the empty tracks, so the items stretch to fill the row. The difference only shows when there are fewer items than fit.

View code
<div class="gdl1"><p>auto-fill</p><div class="g fill"><i></i><i></i><i></i></div><p>auto-fit</p><div class="g fit"><i></i><i></i><i></i></div></div>
.gdl1 { width: 520px; max-width: 100%; font: 600 12px "JetBrains Mono", ui-monospace, monospace; color: #475467; }
.gdl1 p { margin: 10px 0 4px; }
.gdl1 .g { display: grid; gap: 8px; padding: 6px; border: 1px dashed #d0d5dd; border-radius: 10px; }
.gdl1 .fill { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }
.gdl1 .fit { grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); }
.gdl1 i { height: 44px; border-radius: 8px; background: #7f56d9; }

02. Page Layout with Named Areas

GridAreas

A full page skeleton described with grid-template-areas: header, nav, main, aside and footer. The narrow version is a different map where everything stacks, defined in one container query, and the HTML order stays the logical reading order.

View code
<div class="gdl2"><div class="in"><header>header</header><nav>nav</nav><main>main</main><aside>aside</aside><footer>footer</footer></div></div>
.gdl2 { width: 560px; max-width: 100%; container-type: inline-size; font: 700 12px "JetBrains Mono", ui-monospace, monospace; }
.gdl2 .in { display: grid; gap: 6px; grid-template-areas: "header" "nav" "main" "aside" "footer"; }
.gdl2 .in > * { display: grid; place-items: center; min-height: 36px; border-radius: 8px; }
.gdl2 header { grid-area: header; background: #312e81; color: #c7d2fe; } .gdl2 nav { grid-area: nav; background: #134e4a; color: #99f6e4; }
.gdl2 main { grid-area: main; min-height: 90px; background: #1e293b; color: #e2e8f0; } .gdl2 aside { grid-area: aside; background: #713f12; color: #fde68a; }
.gdl2 footer { grid-area: footer; background: #3f3f46; color: #e4e4e7; }
@container (min-width: 440px) { .gdl2 .in { grid-template-columns: 90px 1fr 90px; grid-template-areas: "header header header" "nav main aside" "footer footer footer"; } .gdl2 main { min-height: 140px; } }

03. Aligned Card Parts with subgrid

GridSubgrid

Three cards whose titles, text and buttons line up across the row even though the text lengths differ. Each card spans three rows of the parent grid and uses grid-template-rows: subgrid, so all cards share the same row heights.

View code
<div class="gdl3"><article><h3>Basic</h3><p>Good for one person.</p><a href="#b">Choose</a></article><article><h3>Team plan with extras</h3><p>Shared libraries, roles and an audit log for everyone.</p><a href="#t">Choose</a></article><article><h3>Pro</h3><p>Everything in Team.</p><a href="#p">Choose</a></article></div>
.gdl3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 0 10px; width: 520px; max-width: 100%; font: 13px/1.45 Inter, system-ui, sans-serif; }
.gdl3 article { display: grid; grid-row: span 3; grid-template-rows: subgrid; gap: 8px; margin-bottom: 10px; padding: 14px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; }
.gdl3 h3 { margin: 0; color: #101828; font-size: 15px; } .gdl3 p { margin: 0; color: #475467; }
.gdl3 a { align-self: end; padding: 8px; border-radius: 8px; background: #101828; color: #fff; font-weight: 700; text-align: center; text-decoration: none; }
.gdl3 a:focus-visible { outline: 3px solid #84adff; outline-offset: 2px; }

04. Items Spanning Rows and Columns

GridSpan

A six-column grid where some items span two columns or two rows, set with grid-column: span 2 and grid-row: span 2. Spans are relative to where the item lands, so the layout stays tidy as items are added.

View code
<ul class="gdl4"><li class="w">span 2 cols</li><li class="t">span 2 rows</li><li>1</li><li>1</li><li class="w">span 2 cols</li><li>1</li><li>1</li></ul>
.gdl4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); grid-auto-rows: 60px; gap: 6px; width: 440px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 600 11px "JetBrains Mono", ui-monospace, monospace; }
.gdl4 li { display: grid; place-items: center; border-radius: 8px; background: #1e293b; color: #cbd5e1; text-align: center; }
.gdl4 .w { grid-column: span 2; background: #4338ca; color: #e0e7ff; }
.gdl4 .t { grid-row: span 2; background: #be185d; color: #fce7f3; }

05. Filling Gaps with dense

GridAuto-flow

Wide items can leave holes when they do not fit at the end of a row. grid-auto-flow: dense lets later, smaller items move up to fill the holes. It changes the visual order from the source order, so only use it where order does not matter, like a photo wall.

View code
<ul class="gdl5"><li>1</li><li class="w">2 wide</li><li class="w">3 wide</li><li>4</li><li>5</li><li class="w">6 wide</li><li>7</li></ul>
.gdl5 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-flow: dense; grid-auto-rows: 54px; gap: 6px; width: 380px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 700 13px Inter, system-ui, sans-serif; }
.gdl5 li { display: grid; place-items: center; border-radius: 8px; background: #fef0c7; color: #93370d; }
.gdl5 .w { grid-column: span 2; background: #d1fadf; color: #054f31; }

06. Overlapping Items on One Grid

GridLayering

Grid items can share cells, which is a clean way to layer a caption over an image without absolute positioning. Both the image and the caption are placed on the same grid area, and the caption aligns to the bottom of it.

View code
<figure class="gdl6"><div class="im" role="img" aria-label="Mountain lake"></div><figcaption>Lake Bled, early morning</figcaption></figure>
.gdl6 { display: grid; width: 340px; max-width: 100%; margin: 0; border-radius: 14px; overflow: hidden; font: 600 14px Inter, system-ui, sans-serif; }
.gdl6 > * { grid-area: 1 / 1; }
.gdl6 .im { aspect-ratio: 4 / 3; background: linear-gradient(#93c5fd 0 40%, #1e3a8a 40% 45%, #0e7490 45% 75%, #134e4a 75%); }
.gdl6 figcaption { align-self: end; padding: 30px 14px 12px; background: linear-gradient(transparent, rgba(0,0,0,.75)); color: #fff; }

07. Responsive Columns with min()

GridResponsive

repeat(auto-fill, minmax(min(160px, 100%), 1fr)) is the robust card grid: the min() stops a single column overflowing when the container is narrower than 160px, which a plain minmax(160px, 1fr) would do.

View code
<ul class="gdl7"><li>Card</li><li>Card</li><li>Card</li><li>Card</li><li>Card</li></ul>
.gdl7 { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(160px, 100%), 1fr)); gap: 10px; width: 540px; max-width: 100%; margin: 0; padding: 0; list-style: none; font: 700 14px Inter, system-ui, sans-serif; }
.gdl7 li { display: grid; place-items: center; height: 70px; border: 1px solid #e4e7ec; border-radius: 12px; background: #fff; color: #344054; }

About these CSS Grid Layouts

Named areas read like a map

grid-template-areas lets you draw the layout as strings. Redefine the strings in a container or media query and every area moves, without touching the HTML.

auto-fill or auto-fit

Both create as many columns as fit. auto-fill keeps empty tracks, so items stay at their minimum size; auto-fit collapses them, so items stretch to fill the row.

subgrid aligns nested parts

A card that spans several rows with grid-template-rows: subgrid uses the parent's rows, so titles, text and buttons line up across every card in the row.

dense changes visual order

grid-auto-flow: dense fills holes by moving later items earlier. Screen readers and the Tab key still follow the source order, so keep it for content where order does not matter.

Frequently asked questions

Is subgrid supported?

Yes, in all current major browsers.

Why min(160px, 100%) inside minmax?

It stops a single column overflowing when the container is narrower than 160px.

Grid or flexbox?

Grid for two-dimensional layouts; flexbox for rows or columns of items that size themselves.