CSS Grid – Two-Dimensional Layouts – HTML & CSS
Columns, rows, areas and automatically responsive grids.
With display: grid you define columns (grid-template-columns) and rows (grid-template-rows). The unit fr distributes free space proportionally; repeat(3, 1fr) creates three equal columns. Items arrange themselves one after another automatically; gap sets the spacing.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.raster { display: grid; grid-template-columns: 100px 1fr 2fr; gap: 8px; }
.raster div { background: #cde; padding: 14px; text-align: center; }
.breit { grid-column: span 2; background: #fc9 !important; }
</style>
<div class="raster">
<div>100px</div><div>1fr</div><div>2fr</div>
<div class="breit">spans 2 columns</div><div>Rest</div>
</div>With named areas you draw the layout almost like a sketch: grid-template-areas and grid-area per item. This is perfect for classic page layouts and is easy to rearrange in media queries.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.layout { display: grid; gap: 8px; min-height: 240px;
grid-template-columns: 140px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"kopf kopf"
"menue inhalt"
"fuss fuss"; }
.layout > * { padding: 10px; color: #fff; }
.kopf { grid-area: kopf; background: #333; } .menue { grid-area: menue; background: #06c; }
.inhalt { grid-area: inhalt; background: #2a7; } .fuss { grid-area: fuss; background: #555; }
</style>
<div class="layout"><div class="kopf">Header</div><div class="menue">Menu</div>
<div class="inhalt">Content</div><div class="fuss">Footer</div></div>The recipe for responsive cards without a media query: grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)). The browser packs in as many columns (at least 160px) as fit and distributes the rest. With place-items: center you center content within its cells.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.karten { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.karten article { background: #f2f4ff; border: 1px solid #ccd; border-radius: 10px; padding: 14px;
display: grid; place-items: center; min-height: 70px; }
</style>
<div class="karten">
<article>1</article><article>2</article><article>3</article>
<article>4</article><article>5</article><article>6</article>
</div>
<p>Drag the preview window narrower/wider: the number of columns adapts.</p>auto-fit and auto-fill differ when there are few items: auto-fit stretches the items to the full width (empty columns collapse), auto-fill keeps empty columns. Also: a 1fr grid item can become too wide because of long content – then use minmax(0, 1fr).