Display & Position in CSS
Verstehe die CSS-Eigenschaften display und position und lerne, wie du Elemente präzise auf der Seite platzierst.
Display & Position
Die Eigenschaften display und position bestimmen, wie und wo Elemente auf der Seite erscheinen. Sie sind die Grundlage fuer jedes CSS-Layout.
Die display-Eigenschaft
display: block
Block-Elemente nehmen die volle Breite ein und starten in einer neuen Zeile:
div, p, h1, section, article {
display: block; /* Standardwert fuer diese Elemente */
}
.block-example {
display: block;
width: 300px;
padding: 1rem;
margin-bottom: 1rem;
background: #e2e8f0;
}
display: inline
Inline-Elemente nehmen nur so viel Platz wie noetig und bleiben im Textfluss:
span, a, strong, em {
display: inline; /* Standardwert fuer diese Elemente */
}
Wichtig: Bei Inline-Elementen haben width, height und vertikales margin keine Wirkung!
display: inline-block
Kombiniert das Beste aus beiden Welten:
.badge {
display: inline-block;
padding: 0.25rem 0.75rem;
background: #e44d26;
color: white;
border-radius: 4px;
/* width und height funktionieren! */
/* Bleibt aber im Textfluss! */
}
Vergleich
| Eigenschaft | block | inline | inline-block |
|---|---|---|---|
| Neue Zeile | Ja | Nein | Nein |
| Width/Height | Funktioniert | Ignoriert | Funktioniert |
| Vertikales Margin | Funktioniert | Ignoriert | Funktioniert |
| Volle Breite | Standard | Nein | Nein |
display: none
Entfernt das Element komplett aus dem Layout:
.hidden {
display: none; /* Element ist unsichtbar UND nimmt keinen Platz ein */
}
.invisible {
visibility: hidden; /* Element ist unsichtbar, nimmt aber Platz ein */
}
display: flex und display: grid
Die modernsten Layout-Methoden — diese behandeln wir in eigenen Kapiteln:
.flex-container { display: flex; }
.grid-container { display: grid; }
Die position-Eigenschaft
position: static (Standard)
Elemente folgen dem normalen Dokumentfluss:
.static {
position: static; /* Standardwert */
/* top, right, bottom, left haben KEINE Wirkung */
}
position: relative
Das Element bleibt im Dokumentfluss, kann aber relativ zu seiner normalen Position verschoben werden:
.relative {
position: relative;
top: 10px; /* 10px nach unten verschieben */
left: 20px; /* 20px nach rechts verschieben */
}
Wichtig: Der urspruengliche Platz bleibt reserviert — andere Elemente verschieben sich nicht.
position: absolute
Das Element wird aus dem Dokumentfluss entfernt und relativ zum naechsten positionierten Elternelement platziert:
.parent {
position: relative; /* Macht das Elternelement zum Bezugspunkt */
}
.absolute-child {
position: absolute;
top: 0;
right: 0;
/* Sitzt jetzt in der oberen rechten Ecke des Parents */
}
Praktisches Beispiel: Badge auf einer Card
<div class="card">
<span class="badge">Neu</span>
<img src="produkt.jpg" alt="Produkt">
<h3>Produktname</h3>
</div>
.card {
position: relative; /* Bezugspunkt fuer das Badge */
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
}
.badge {
position: absolute;
top: 10px;
right: 10px;
background: #e44d26;
color: white;
padding: 0.25rem 0.75rem;
border-radius: 4px;
font-size: 0.875rem;
font-weight: 600;
}
position: fixed
Das Element ist relativ zum Viewport fixiert — es scrollt nicht mit:
/* Fixierte Navigation */
.navbar {
position: fixed;
top: 0;
left: 0;
right: 0;
background: white;
padding: 1rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
/* Platz fuer die fixierte Nav schaffen */
body {
padding-top: 60px;
}
position: sticky
Eine Kombination aus relative und fixed — das Element scrollt normal mit, bis es eine bestimmte Position erreicht:
/* Klebt oben am Viewport fest, sobald man dorthin scrollt */
.sticky-header {
position: sticky;
top: 0;
background: white;
padding: 1rem;
border-bottom: 1px solid #e2e8f0;
z-index: 100;
}
Vergleich der Position-Werte
| Wert | Im Dokumentfluss | Bezugspunkt | Scrollt mit |
|---|---|---|---|
static | Ja | — | Ja |
relative | Ja | Eigene Position | Ja |
absolute | Nein | Positionierter Parent | Ja |
fixed | Nein | Viewport | Nein |
sticky | Ja (bis zum Schwellwert) | Viewport | Bis zum Schwellwert |
Der z-index
Wenn sich positionierte Elemente ueberlappen, bestimmt z-index die Stapelreihenfolge:
.hintergrund {
position: absolute;
z-index: 1;
}
.vordergrund {
position: absolute;
z-index: 2; /* Liegt ueber dem Hintergrund */
}
.ganz-oben {
position: fixed;
z-index: 1000; /* Ueber allem */
}
Tipp: Verwende z-index-Stufen mit Abstaenden (10, 20, 30…), damit du spaeter noch Werte dazwischen einfuegen kannst.
z-index und Stacking Contexts
/* Jedes positionierte Element mit z-index erstellt einen neuen Stacking Context */
.container {
position: relative;
z-index: 1;
}
/* Kind-Elemente koennen nur innerhalb ihres Stacking Contexts gestapelt werden */
.container .child {
position: absolute;
z-index: 999; /* Trotzdem unter Elementen mit z-index: 2 ausserhalb */
}
Zentrierungstechniken
Horizontal zentrieren
/* Block-Element */
.centered-block {
width: 80%;
margin: 0 auto;
}
/* Inline-Inhalt */
.centered-text {
text-align: center;
}
Vertikal und horizontal zentrieren (mit Position)
/* Methode 1: Absolute + Transform */
.center-absolute {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Methode 2: Flexbox (bevorzugt!) */
.center-flex {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Methode 3: Grid (noch einfacher!) */
.center-grid {
display: grid;
place-items: center;
min-height: 100vh;
}
Praktisches Beispiel: Overlay-Modal
<div class="overlay">
<div class="modal">
<h2>Willkommen!</h2>
<p>Das ist ein modales Fenster.</p>
<button>Schliessen</button>
</div>
</div>
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: grid;
place-items: center;
z-index: 1000;
}
.modal {
background: white;
padding: 2rem;
border-radius: 12px;
max-width: 500px;
width: 90%;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
Uebungen
Uebung 1: Fixierte Navigation
Erstelle eine fixierte Navigation oben auf der Seite. Fuege genug Inhalt hinzu, damit man scrollen kann, und beobachte, wie die Navigation fixiert bleibt.
Uebung 2: Badge-System
Erstelle Cards mit verschiedenen Badges (Neu, Sale, Beliebt) in verschiedenen Ecken. Nutze position: absolute fuer die Badges.
Uebung 3: Sticky Sidebar
Erstelle ein Layout mit einem Hauptinhalt und einer Sidebar. Die Sidebar soll position: sticky verwenden, damit sie beim Scrollen sichtbar bleibt.
Was kommt als Naechstes?
Im naechsten Kapitel lernst du Flexbox — die modernste Methode fuer eindimensionale Layouts:
- Flex-Container und Flex-Items
- Ausrichtung und Verteilung
- Responsive Layouts ohne Media Queries
Zusammenfassung
displaybestimmt, wie ein Element dargestellt wird:block,inline,inline-block,noneposition: relativeverschiebt relativ zur eigenen Positionposition: absoluteentfernt aus dem Fluss und positioniert relativ zum Parentposition: fixedfixiert am Viewport — scrollt nicht mitposition: stickyklebt ab einem Schwellwert festz-indexkontrolliert die Stapelreihenfolge ueberlappender Elemente- Zum Zentrieren: Flexbox oder Grid mit
place-items: center
Pro-Tipp: Wenn position: sticky nicht funktioniert, pruefe ob ein Elternelement overflow: hidden oder overflow: auto hat. Das ist der haeufigste Grund, warum Sticky nicht greift!