Propriedade de posição CSS
Exemplo
Posicione um elemento <h2>:
h2
{
position: absolute;
left: 100px;
top: 150px;
}
Mais exemplos de "Experimente você mesmo" abaixo.
Definição e uso
A position
propriedade especifica o tipo de método de posicionamento usado para um elemento (estático, relativo, absoluto, fixo ou fixo).
Valor padrão: | estático |
---|---|
Herdado: | não |
Animavel: | não. Leia sobre animáveis |
Versão: | CSS2 |
Sintaxe JavaScript: | objeto .style.position="absoluto" |
Suporte ao navegador
Os números na tabela especificam a primeira versão do navegador que oferece suporte total à propriedade.
Property | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
Observação: o sticky
valor não é compatível com o Internet Explorer ou Edge 15 e versões anteriores.
Sintaxe CSS
position: static|absolute|fixed|relative|sticky|initial|inherit;
Valores de propriedade
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 |
Mais exemplos
Exemplo
Como posicionar um elemento em relação à sua posição normal:
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
Exemplo
Mais posicionamento:
#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;
}
Páginas relacionadas
Tutorial CSS: Posicionamento CSS
Referência HTML DOM: propriedade position