CSS - Positionseigenschaft
Beispiel
Positionieren Sie ein <h2>-Element:
h2
{
position: absolute;
left: 100px;
top: 150px;
}
Weitere „Probieren Sie es selbst“-Beispiele weiter unten.
Definition und Verwendung
Die position
Eigenschaft gibt die Art der Positionierungsmethode an, die für ein Element verwendet wird (statisch, relativ, absolut, fixiert oder fixiert).
Standardwert: | statisch |
---|---|
Vererbt: | Nein |
Animierbar: | Nein. Lesen Sie mehr über animierbare |
Ausführung: | CSS2 |
JavaScript-Syntax: | Objekt .style.position="absolut" |
Browser-Unterstützung
Die Zahlen in der Tabelle geben die erste Browserversion an, die die Eigenschaft vollständig unterstützt.
Property | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
Hinweis: Der sticky
Wert wird in Internet Explorer oder Edge 15 und früheren Versionen nicht unterstützt.
CSS-Syntax
position: static|absolute|fixed|relative|sticky|initial|inherit;
Eigenschaftswerte
Value | Description | Play it |
---|---|---|
static | Default value. Elements render in order, as they appear in the document flow | |
absolute | The element is positioned relative to its first positioned (not static) ancestor element | |
fixed | The element is positioned relative to the browser window | |
relative | The element is positioned relative to its normal position, so "left:20px" adds 20 pixels to the element's LEFT position | |
sticky | The element is positioned based on the user's scroll position
A sticky element toggles between |
|
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
Mehr Beispiele
Beispiel
So positionieren Sie ein Element relativ zu seiner normalen Position:
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
Beispiel
Mehr Positionierung:
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position:
absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
Verwandte Seiten
CSS-Tutorial: CSS-Positionierung
HTML-DOM-Referenz: Positionseigenschaft