Propriedade CSS background-position
Exemplo
Como posicionar uma imagem de fundo:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Mais exemplos de "Experimente você mesmo" abaixo.
Definição e uso
A background-position
propriedade define a posição inicial de uma imagem de fundo.
Dica: Por padrão, uma imagem de fundo é colocada no canto superior esquerdo de um elemento e repetida vertical e horizontalmente.
Valor padrão: | 0% 0% |
---|---|
Herdado: | não |
Animavel: | sim. Leia sobre animáveis |
Versão: | CSS1 |
Sintaxe JavaScript: | objeto .style.backgroundPosition="center" |
Suporte ao navegador
Os números na tabela especificam a primeira versão do navegador que oferece suporte total à propriedade.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Observação: o IE8 e versões anteriores não oferecem suporte a várias imagens de plano de fundo em um elemento.
Sintaxe CSS
background-position: value;
Valores de propriedade
Value | Description | Play it |
---|---|---|
left top left center left bottom right top right center right bottom center top center center center bottom |
If you only specify one keyword, the other value will be "center" | |
x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. Default value is: 0% 0% | |
xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions | |
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 uma imagem de fundo para ser centralizada no topo:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Exemplo
Como posicionar uma imagem de fundo no canto inferior direito:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Exemplo
Como posicionar uma imagem de fundo usando porcentagem:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Exemplo
Como posicionar uma imagem de fundo usando pixels:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Exemplo
Use diferentes propriedades de plano de fundo para criar uma imagem "herói":
.hero-image {
background-image: url("photographer.jpg"); /* The
image used */
background-color: #cccccc; /* Used if the image is
unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
Páginas relacionadas
Tutorial de CSS: Fundo de CSS
Referência CSS: propriedade de imagem de fundo
Referência HTML DOM: propriedade backgroundPosition