Stile backgroundPosizione Proprietà
Esempio
Cambia la posizione di un'immagine di sfondo:
document.body.style.backgroundPosition = "top right";
Altri esempi "Provalo da solo" di seguito.
Definizione e utilizzo
La proprietà backgroundPosition imposta o restituisce la posizione di un'immagine di sfondo all'interno di un elemento.
Supporto browser
I numeri nella tabella specificano la prima versione del browser che supporta completamente la proprietà.
Property | |||||
---|---|---|---|---|---|
backgroundPosition | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Sintassi
Restituisce la proprietà backgroundPosition:
object.style.backgroundPosition
Imposta la proprietà backgroundPosition:
object.style.backgroundPosition = value
Valori di proprietà
Value | Description |
---|---|
top left top center top right center left center center center right bottom left bottom center bottom right |
If you only specify one keyword, the other value will be "center". |
x% y% | The x value indicates the horizontal position and the y value indicates the vertical position. 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%. |
xpos ypos | The x value indicates the horizontal position and the y value indicates the vertical position. 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 units |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Dettagli tecnici
Valore di default: | 0% 0% |
---|---|
Valore di ritorno: | Una stringa, che rappresenta la posizione di un'immagine di sfondo |
Versione CSS | CSS1 |
Altri esempi
Esempio
Cambia la posizione dell'immagine di sfondo in un elemento <div> al centro in basso:
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
Esempio
Cambia la posizione dell'immagine di sfondo in un elemento <div> a 200 px in orizzontale e 40 px in verticale:
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
Esempio
Restituisce la posizione dell'immagine di sfondo in un elemento <div>:
document.getElementById("myDiv").style.backgroundPosition;
Pagine correlate
Tutorial CSS: Sfondo CSS
Riferimento CSS: proprietà immagine di sfondo
Riferimento CSS: proprietà posizione di sfondo
Riferimento HTML DOM: proprietà in background