Propriedade de posição de estilo
Exemplo
Altere a posição de um elemento <div> de relativo para absoluto:
document.getElementById("myDIV").style.position = "absolute";
Definição e uso
A propriedade position define ou retorna o tipo de método de posicionamento usado para um elemento (estático, relativo, absoluto ou fixo).
Suporte ao navegador
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Sintaxe
Retorne a propriedade position:
object.style.position
Defina a propriedade de posição:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Valores de propriedade
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 |
Detalhes técnicos
Valor padrão: | estático |
---|---|
Valor de retorno: | Uma String, representando o tipo de posição de um elemento |
Versão CSS | CSS2 |
Mais exemplos
Exemplo
Usando diferentes tipos de posição:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Exemplo
Retorna a posição de um elemento <h2>:
alert(document.getElementById("myH2").style.position);
Páginas relacionadas
Tutorial CSS: Posicionamento CSS
Referência CSS: propriedade position
❮ Objeto de estilo