Poloha stylu Vlastnost
Příklad
Změňte pozici prvku <div> z relativní na absolutní:
document.getElementById("myDIV").style.position = "absolute";
Definice a použití
Vlastnost position nastavuje nebo vrací typ metody umístění použité pro prvek (statická, relativní, absolutní nebo pevná).
Podpora prohlížeče
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Syntax
Vraťte vlastnost pozice:
object.style.position
Nastavte vlastnost position:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Hodnoty vlastností
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 |
Technické údaje
Výchozí hodnota: | statický |
---|---|
Návratová hodnota: | Řetězec představující typ pozice prvku |
Verze CSS | CSS2 |
Další příklady
Příklad
Použití různých typů pozic:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Příklad
Vraťte pozici prvku <h2>:
alert(document.getElementById("myH2").style.position);
Související stránky
CSS tutoriál: CSS Positioning
CSS reference: vlastnost position
❮ Objekt stylu