Pozycja stylu Właściwość
Przykład
Zmień pozycję elementu <div> z względnej na bezwzględną:
document.getElementById("myDIV").style.position = "absolute";
Definicja i użycie
Właściwość position ustawia lub zwraca typ metody pozycjonowania używanej dla elementu (statyczna, względna, bezwzględna lub stała).
Obsługa przeglądarki
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Składnia
Zwróć właściwość pozycji:
object.style.position
Ustaw właściwość pozycji:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Wartości nieruchomości
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 |
Szczegóły techniczne
Domyślna wartość: | statyczny |
---|---|
Wartość zwrotu: | String, reprezentujący typ pozycji elementu |
Wersja CSS | CSS2 |
Więcej przykładów
Przykład
Korzystanie z różnych typów pozycji:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Przykład
Zwróć pozycję elementu <h2>:
alert(document.getElementById("myH2").style.position);
Powiązane strony
Samouczek CSS: Pozycjonowanie CSS
Odniesienie CSS: właściwość pozycji
❮ Obiekt stylu