HTML & CSS: Layout – code examples
Centering with Flexbox
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.buehne { display: flex; justify-content: center; align-items: center; height: 180px; background: #eef; border: 2px dashed #99c; }
.mitte { background: #2b59c3; color: #fff; padding: 16px 24px; border-radius: 10px; }
</style>
<div class="buehne"><div class="mitte">Perfectly centered</div></div>justify-content distributes along the main axis (horizontal here), align-items along the cross axis (vertical). Alternative with Grid: display: grid; place-items: center;.
Page Layout with Grid Areas
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
body { margin: 0; }
.seite { display: grid; gap: 8px; min-height: 260px;
grid-template-areas: "kopf" "menue" "inhalt" "fuss"; }
@media (min-width: 520px) {
.seite { grid-template-columns: 150px 1fr; grid-template-rows: auto 1fr auto;
grid-template-areas: "kopf kopf" "menue inhalt" "fuss fuss"; }
}
.seite > * { padding: 12px; color: #fff; }
.kopf { grid-area: kopf; background: #374151; } .menue { grid-area: menue; background: #2563eb; }
.inhalt { grid-area: inhalt; background: #059669; } .fuss { grid-area: fuss; background: #6b7280; }
</style>
<div class="seite"><header class="kopf">Header</header><nav class="menue">Menu</nav>
<main class="inhalt">Content</main><footer class="fuss">Footer</footer></div>grid-template-areas describes the layout like an ASCII sketch. Mobile first: the base is one column, from 520 px on there is a sidebar. Change the window width of the preview.
Responsive Card Grid Without a Media Query
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.raster { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.karte { border: 1px solid #d5d9e0; border-radius: 12px; overflow: hidden; background: #fff; }
.bild { height: 70px; }
.karte h3 { margin: 10px 12px 2px; font-size: 1rem; } .karte p { margin: 0 12px 12px; color: #667; font-size: .9rem; }
</style>
<div class="raster">
<article class="karte"><div class="bild" style="background:#f87171"></div><h3>Red</h3><p>19,90 €</p></article>
<article class="karte"><div class="bild" style="background:#60a5fa"></div><h3>Blue</h3><p>24,90 €</p></article>
<article class="karte"><div class="bild" style="background:#34d399"></div><h3>Green</h3><p>14,90 €</p></article>
<article class="karte"><div class="bild" style="background:#fbbf24"></div><h3>Yellow</h3><p>9,90 €</p></article>
</div>repeat(auto-fit, minmax(140px, 1fr)) creates as many columns as fit at a minimum of 140 px. No breakpoint needed. Change the width of the preview.
Sticky Header Row
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
body { margin: 0; }
.scroll { height: 200px; overflow: auto; }
.kopf { position: sticky; top: 0; background: #1f2937; color: #fff; padding: 10px 14px; font-weight: 700; z-index: 1; }
.scroll p { margin: 0; padding: 14px; border-bottom: 1px solid #e5e7eb; }
</style>
<div class="scroll">
<div class="kopf">Stays stuck at the top</div>
<p>Line 1</p><p>Line 2</p><p>Line 3</p><p>Line 4</p><p>Line 5</p><p>Line 6</p><p>Line 7</p><p>Line 8</p>
</div>position: sticky with top: 0 stays in the normal flow but sticks to the top edge of the scroll container while scrolling. It does not work if an ancestor has overflow: hidden.
Container Query Card
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.slot { container-type: inline-size; border: 2px dashed #94a3b8; padding: 8px; margin-bottom: 12px; }
.karte { display: grid; gap: 8px; background: #f1f5f9; padding: 10px; border-radius: 10px; }
.bild { height: 60px; background: linear-gradient(135deg, #38bdf8, #6366f1); border-radius: 8px; }
@container (min-width: 360px) { .karte { grid-template-columns: 110px 1fr; align-items: center; } .bild { height: 70px; } }
</style>
<div class="slot" style="width:220px"><div class="karte"><div class="bild"></div><div><strong>Narrow</strong><br>Image on top</div></div></div>
<div class="slot"><div class="karte"><div class="bild"></div><div><strong>Wide</strong><br>Image on the left</div></div></div>container-type: inline-size turns the parent element into a container; @container reacts to its width instead of the window. The same card behaves differently depending on where it is placed.