</>HTML & CSS

HTML & CSS: Layout – code examples

Contents

Centering with Flexbox

Layout#flexbox#centering#center
Use case: Align content horizontally and vertically in the center – e.g. hero sections, login boxes or error pages.
<!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

Layout#grid#areas#layout
Use case: Classic layout with header, sidebar, content and footer – single-column on the smartphone.
<!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

Layout#grid#auto-fit#minmax#responsive
Use case: Product cards, galleries or blog overviews that adapt automatically to any width.
<!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.

Container Query Card

Layout#container query#responsive#component
Use case: Components that adapt to the space available to them – no matter where they are used.
<!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.