⏱️3 min read · 569 words
CSS Flexbox ist auch im Jahr 2026 immer noch das wichtigste Layout-Tool für UI-Komponenten. Während CSS Grid zweidimensionale Layouts verarbeitet, zeichnet sich Flexbox durch eindimensionale Ausrichtung, responsive Navigationsleisten, Kartenlayouts und Zentrierelemente aus. Dieser vollständige Leitfaden deckt jede Immobilie mit visuellen Beispielen ab.
📋 Table of Contents
Flex-Container-Eigenschaften
.container {
display: flex; /* or inline-flex */
flex-direction: row; /* row | row-reverse | column | column-reverse */
flex-wrap: nowrap; /* nowrap | wrap | wrap-reverse */
flex-flow: row nowrap; /* shorthand: flex-direction + flex-wrap */
/* Main axis alignment (row = horizontal, column = vertical) */
justify-content: flex-start; /* flex-start | flex-end | center | space-between | space-around | space-evenly */
/* Cross axis alignment */
align-items: stretch; /* stretch | flex-start | flex-end | center | baseline */
/* Multi-line cross axis alignment (only when flex-wrap: wrap) */
align-content: normal; /* normal | flex-start | flex-end | center | space-between | space-around | space-evenly | stretch */
/* Gap between items */
gap: 16px; /* gap: row-gap col-gap */
row-gap: 16px;
column-gap: 24px;
}
Flex-Artikeleigenschaften
.item {
/* flex-grow: how much to grow (0 = don't grow) */
flex-grow: 1; /* 0 = don't grow, 1 = grow proportionally */
/* flex-shrink: how much to shrink (1 = shrink proportionally) */
flex-shrink: 0; /* 0 = don't shrink */
/* flex-basis: initial size before growing/shrinking */
flex-basis: 200px; /* auto | 0 | 200px | 25% */
/* Shorthand: grow shrink basis */
flex: 1 1 auto; /* flex: 1 (common) | flex: 0 0 200px | flex: auto */
/* Self alignment (overrides align-items for this item) */
align-self: center; /* auto | stretch | flex-start | flex-end | center | baseline */
/* Order: visual order (default: 0) */
order: -1; /* appears before order:0 items */
}
Gängige Flexbox-Muster
Perfekte Zentrierung
/* Center anything horizontally and vertically */
.center-container {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
/* Or use margin: auto on the child (works when parent is flex) */
.centered-item {
margin: auto;
}
Navigationsleiste
<nav class="navbar">
<a href="/" class="logo">TechPulse</a>
<ul class="nav-links">
<li><a href="/blog">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
<a href="/contact" class="cta">Get Started</a>
</nav>
.navbar {
display: flex;
align-items: center;
gap: 16px;
padding: 0 24px;
height: 64px;
}
.logo { font-weight: bold; }
.nav-links {
display: flex;
gap: 24px;
list-style: none;
margin: 0;
padding: 0;
flex: 1; /* push CTA to right */
justify-content: center;
}
.cta { margin-left: auto; } /* or use flex: 1 on nav-links
Kartenlayout mit gleichen Höhen
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.card {
display: flex;
flex-direction: column;
flex: 1 1 300px; /* grow, shrink, min-basis 300px */
max-width: 400px;
background: white;
border-radius: 12px;
padding: 24px;
}
.card-content {
flex: 1; /* grows to fill space, pushes button to bottom */
}
.card-button {
margin-top: 16px; /* always at bottom regardless of content height */
}
Klebrige Fußzeile
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1; /* grows to fill remaining space, pushing footer down */
}
footer {
/* Always at bottom */
}
Heiliger Gral-Layout
.layout {
display: flex;
min-height: 100vh;
flex-direction: column;
}
.layout-header,
.layout-footer { flex: none; }
.layout-body {
display: flex;
flex: 1;
}
.layout-sidebar { width: 250px; flex: none; }
.layout-main { flex: 1; } /* fills remaining space */
.layout-aside { width: 200px; flex: none; }
Responsives Layout mit Flex-Wrap
/* Items stack vertically on mobile, go horizontal on desktop */
.responsive-row {
display: flex;
flex-wrap: wrap;
gap: 16px;
}
.responsive-item {
flex: 1 1 250px; /* min-width 250px, grows to fill */
}
/* Force break: half-width items */
.half-width {
flex: 1 1 calc(50% - 16px);
max-width: calc(50% - 16px);
}
/* Full-width override */
.full-width {
flex-basis: 100%;
}
Flexbox vs. Grid: Wann jeweils zu verwenden ist
| Szenario | Verwenden Sie Flexbox | Verwenden Sie Raster |
|---|---|---|
| Navigationsleiste | ✅ | ❌ |
| Kartenliste in einer Reihe | ✅ | ✅ |
| Komplexes 2D-Layout | ❌ | ✅ |
| Zentrieren Sie ein Element | ✅ | ✅ |
| Formularlayout | ✅ (einfach) | ✅ (komplex) |
| Seitenlayout (Kopfzeile/Hauptzeile/Fußzeile) | ✅ | ✅ |
| Automatische Platzierung des Artikels | ❌ | ✅ |
Die Beherrschung von Flexbox ist für jeden Webentwickler unerlässlich. Die Kombination ausflex: 1für wachsende Gegenstände,gapfür Abstand,align-itemszur achsübergreifenden Ausrichtung undflex-wrap: wrapfür responsive Layouts deckt 90 % der realen Anwendungsfälle ab.
🔗 Share this article
✍️ Leave a Comment