Proprietà CSS in background
Esempio
Come posizionare un'immagine di sfondo:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Altri esempi "Provalo da solo" di seguito.
Definizione e utilizzo
La background-position
proprietà imposta la posizione iniziale di un'immagine di sfondo.
Suggerimento: per impostazione predefinita, un'immagine di sfondo viene posizionata nell'angolo superiore sinistro di un elemento e ripetuta sia verticalmente che orizzontalmente.
Valore di default: | 0% 0% |
---|---|
Ereditato: | no |
Animabile: | sì. Leggi su animabile |
Versione: | CSS1 |
sintassi JavaScript: | oggetto .style.backgroundPosition="centro" |
Supporto browser
I numeri nella tabella specificano la prima versione del browser che supporta completamente la proprietà.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Nota: IE8 e versioni precedenti non supportano più immagini di sfondo su un elemento.
Sintassi CSS
background-position: value;
Valori di proprietà
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 |
Altri esempi
Esempio
Come posizionare un'immagine di sfondo da centrare in alto:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Esempio
Come posizionare un'immagine di sfondo in basso a destra:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Esempio
Come posizionare un'immagine di sfondo usando la percentuale:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Esempio
Come posizionare un'immagine di sfondo usando i pixel:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Esempio
Usa diverse proprietà di sfondo per creare un'immagine "eroe":
.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 */
}
Pagine correlate
Tutorial CSS: Sfondo CSS
Riferimento CSS: proprietà immagine di sfondo
Riferimento HTML DOM: proprietà backgroundPosition