Flexbox – eindimensionale Layouts – HTML & CSS
Zeilen und Spalten mit flexiblen Elementen, Ausrichtung und Abständen.
Mit display: flex wird ein Element zum Flex-Container, seine Kinder zu Flex-Items. Die Hauptachse legt flex-direction fest (row Standard, column). Auf ihr verteilt justify-content den Platz; auf der Querachse richtet align-items aus. gap setzt Abstände zwischen den Items – ohne Margin-Hacks.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.leiste { display: flex; gap: 10px; justify-content: space-between; align-items: center;
background: #eef; padding: 10px; height: 80px; }
.leiste div { background: royalblue; color: #fff; padding: 10px 14px; border-radius: 6px; }
.leiste .gross { padding: 24px 14px; }
</style>
<div class="leiste">
<div>Eins</div><div class="gross">Zwei (höher)</div><div>Drei</div>
</div>Die Größenverteilung steuert die Kurzform flex: <grow> <shrink> <basis>. flex: 1 (= 1 1 0%) lässt Items gleichmäßig wachsen; flex: 0 0 200px erzwingt eine feste Breite. Mit flex-wrap: wrap brechen Items in neue Zeilen um; margin-left: auto schiebt ein Item ans Ende.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.nav { display: flex; align-items: center; gap: 14px; background: #222; color: #fff; padding: 10px 16px; }
.nav .logo { font-weight: 800; }
.nav .push { margin-left: auto; } /* schiebt alles Folgende nach rechts */
.reihe { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.reihe span { flex: 1 1 120px; background: #cde; padding: 10px; text-align: center; }
</style>
<div class="nav"><span class="logo">LOGO</span><a href="#a" style="color:#fff">Start</a>
<a href="#b" style="color:#fff">Preise</a><button class="push">Login</button></div>
<div class="reihe"><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span></div>Perfekte Zentrierung in zwei Zeilen: display: flex; justify-content: center; align-items: center; – horizontal und vertikal. Für Spalten-Layouts (z. B. Footer nach unten drücken) nimmst du flex-direction: column und lässt den Hauptbereich mit flex: 1 wachsen.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.seite { display: flex; flex-direction: column; min-height: 220px; border: 2px solid #999; }
.seite header, .seite footer { background: #333; color: #fff; padding: 8px; }
.seite main { flex: 1; display: flex; justify-content: center; align-items: center; background: #f5f5f5; }
</style>
<div class="seite">
<header>Kopf</header>
<main>Zentriert in der Mitte</main>
<footer>Fuß bleibt unten (sticky footer)</footer>
</div>flex-shrink: 1), aber nicht unter ihre Mindestinhaltsbreite: Lange Wörter oder Bilder sprengen dann das Layout. Abhilfe: min-width: 0 am Item und overflow-wrap: anywhere. Außerdem: align-items wirkt auf die Querachse, justify-content auf die Hauptachse – bei flex-direction: column sind die Rollen vertauscht.