Pozycja CSS Właściwość
Przykład
Umieść element <h2>:
h2
{
position: absolute;
left: 100px;
top: 150px;
}
Więcej przykładów „Wypróbuj sam” poniżej.
Definicja i użycie
Właściwość position
określa typ metody pozycjonowania używanej dla elementu (statyczna, względna, bezwzględna, stała lub lepka).
Domyślna wartość: | statyczny |
---|---|
Dziedziczny: | nie |
Animowalny: | nie. Przeczytaj o animacji |
Wersja: | CSS2 |
Składnia JavaScript: | obiekt .style.position="absolutne" |
Obsługa przeglądarki
Liczby w tabeli określają pierwszą wersję przeglądarki, która w pełni obsługuje tę właściwość.
Property | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
Uwaga: ta sticky
wartość nie jest obsługiwana w przeglądarce Internet Explorer, Edge 15 i wcześniejszych wersjach.
Składnia CSS
position: static|absolute|fixed|relative|sticky|initial|inherit;
Wartości nieruchomości
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 |
Więcej przykładów
Przykład
Jak ustawić element w stosunku do jego normalnej pozycji:
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
Przykład
Więcej pozycjonowania:
#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;
}
Powiązane strony
Samouczek CSS: Pozycjonowanie CSS
Dokumentacja HTML DOM: właściwość pozycji