CSS Grid Layout Guide
CSS Grid: Das mächtige Layout-System
CSS Grid ermöglicht komplexe zweidimensionale Layouts. Definieren Sie Zeilen und Spalten, platzieren Sie Elemente präzise – alles mit reinem CSS.
Grid aktivieren
.container {
display: grid;
/* oder inline-grid */
}
Spalten und Zeilen definieren
grid-template-columns / grid-template-rows
.container {
display: grid;
/* Feste Größen */
grid-template-columns: 200px 200px 200px;
/* Flexible Größen */
grid-template-columns: 1fr 1fr 1fr;
/* Gemischt */
grid-template-columns: 200px 1fr 2fr;
/* Mit repeat() */
grid-template-columns: repeat(3, 1fr);
/* Auto-fill für responsive Grids */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
/* Zeilen */
grid-template-rows: 100px auto 100px;
}
Die fr-Einheit
/* fr = fraction (Bruchteil des verfügbaren Platzes) */ grid-template-columns: 1fr 2fr 1fr; /* 1. Spalte: 25%, 2. Spalte: 50%, 3. Spalte: 25% */
Abstände (gap)
.container {
gap: 20px; /* Alle Richtungen */
gap: 20px 10px; /* row-gap column-gap */
row-gap: 20px;
column-gap: 10px;
}
Items platzieren
grid-column / grid-row
.item {
/* Spalte: von Linie 1 bis Linie 3 */
grid-column: 1 / 3;
/* Kurzform: Start / Ende */
grid-column: 1 / span 2; /* Ab 1, 2 Spalten breit */
/* Zeile */
grid-row: 1 / 2;
/* Beides kombiniert */
grid-column: 1 / 3;
grid-row: 1 / 2;
}
grid-area (Shorthand)
.item {
/* row-start / column-start / row-end / column-end */
grid-area: 1 / 1 / 2 / 3;
}
Benannte Grid-Bereiche
.container {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
min-height: 100vh;
}
header { grid-area: header; }
nav { grid-area: nav; }
main { grid-area: main; }
aside { grid-area: aside; }
footer { grid-area: footer; }
Ausrichtung
Container-Eigenschaften
.container {
/* Items horizontal ausrichten */
justify-items: start | end | center | stretch;
/* Items vertikal ausrichten */
align-items: start | end | center | stretch;
/* Gesamtes Grid horizontal */
justify-content: start | end | center | space-between | space-around;
/* Gesamtes Grid vertikal */
align-content: start | end | center | space-between | space-around;
}
Item-Eigenschaften
.item {
/* Einzelnes Item überschreiben */
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
/* Shorthand */
place-self: center center;
}
Implizites Grid
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
/* Automatisch erstellte Zeilen */
grid-auto-rows: 200px;
/* Oder minmax für flexible Höhe */
grid-auto-rows: minmax(100px, auto);
/* Automatisch erstellte Spalten */
grid-auto-columns: 100px;
/* Füll-Richtung */
grid-auto-flow: row; /* Standard */
grid-auto-flow: column;
grid-auto-flow: dense; /* Lücken füllen */
}
Praktische Beispiele
Responsive Card Grid
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 1.5rem;
}
.card {
/* Karten füllen ihre Zelle */
}
Dashboard Layout
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr;
grid-template-areas:
"sidebar header"
"sidebar main";
min-height: 100vh;
}
.sidebar { grid-area: sidebar; }
.header { grid-area: header; }
.main { grid-area: main; }
Magazine Layout
.magazine {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 200px);
gap: 1rem;
}
.featured {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
.article {
/* Füllt eine Zelle */
}
Zentrierung
.container {
display: grid;
place-items: center;
height: 100vh;
}
minmax() und auto-fit/auto-fill
/* minmax(min, max) */ grid-template-columns: minmax(200px, 1fr); /* auto-fill: Erstellt so viele Spalten wie möglich */ grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); /* auto-fit: Wie auto-fill, aber leere Spalten kollabieren */ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
Unterschied auto-fill vs auto-fit
/* Container 1000px breit, 3 Items */ /* auto-fill: 4 Spalten (1 leer) */ | Item | Item | Item | | /* auto-fit: 3 Spalten (dehnen sich) */ | Item | Item | Item |
Subgrid (moderne Browser)
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
.item {
display: grid;
grid-template-columns: subgrid; /* Übernimmt Parent-Grid */
grid-column: span 3;
}
Grid vs. Flexbox
| Aspekt | Grid | Flexbox |
|---|---|---|
| Dimension | 2D (Zeilen & Spalten) | 1D (Zeile oder Spalte) |
| Ansatz | Layout-first | Content-first |
| Ideal für | Seitenlayouts, Grids | Komponenten, Navigation |
💡 Tipp:
Nutzen Sie Grid für das Seitenlayout und Flexbox innerhalb der Grid-Zellen für die Komponenten-Ausrichtung.