Stilpositionseigenschaft _ _
Beispiel
Ändern Sie die Position eines <div>-Elements von relativ zu absolut:
document.getElementById("myDIV").style.position = "absolute";
Definition und Verwendung
Die Positionseigenschaft legt die Art der Positionierungsmethode fest oder gibt sie zurück, die für ein Element verwendet wird (statisch, relativ, absolut oder fest).
Browser-Unterstützung
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Syntax
Geben Sie die Positionseigenschaft zurück:
object.style.position
Legen Sie die Positionseigenschaft fest:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Eigenschaftswerte
Value | Description |
---|---|
static | Elements renders in order, as they appear in the document flow. This is default. |
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:20" 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 |
Technische Details
Standardwert: | statisch |
---|---|
Rückgabewert: | Ein String, der den Positionstyp eines Elements darstellt |
CSS-Version | CSS2 |
Mehr Beispiele
Beispiel
Verwendung verschiedener Positionstypen:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Beispiel
Gibt die Position eines <h2>-Elements zurück:
alert(document.getElementById("myH2").style.position);
Verwandte Seiten
CSS-Tutorial: CSS-Positionierung
CSS-Referenz: Positionseigenschaft
❮ Style-Objekt