style backgroundPosition -Eigenschaft
Beispiel
Position eines Hintergrundbildes ändern:
document.body.style.backgroundPosition = "top right";
Weitere „Probieren Sie es selbst“-Beispiele weiter unten.
Definition und Verwendung
Die Eigenschaft backgroundPosition legt die Position eines Hintergrundbilds innerhalb eines Elements fest oder gibt sie zurück.
Browser-Unterstützung
Die Zahlen in der Tabelle geben die erste Browserversion an, die die Eigenschaft vollständig unterstützt.
Property | |||||
---|---|---|---|---|---|
backgroundPosition | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Syntax
Gibt die Eigenschaft backgroundPosition zurück:
object.style.backgroundPosition
Legen Sie die Eigenschaft backgroundPosition fest:
object.style.backgroundPosition = value
Eigenschaftswerte
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 |
Technische Details
Standardwert: | 0 % 0 % |
---|---|
Rückgabewert: | Ein String, der die Position eines Hintergrundbilds darstellt |
CSS-Version | CSS1 |
Mehr Beispiele
Beispiel
Ändern Sie die Position des Hintergrundbilds in einem <div>-Element auf unten zentriert:
document.getElementById("myDiv").style.backgroundPosition = "center bottom";
Beispiel
Ändern Sie die Position des Hintergrundbilds in einem <div>-Element auf 200 Pixel horizontal und 40 Pixel vertikal:
document.getElementById("myDiv").style.backgroundPosition = "200px 40px";
Beispiel
Gibt die Position des Hintergrundbilds in einem <div>-Element zurück:
document.getElementById("myDiv").style.backgroundPosition;
Verwandte Seiten
CSS-Tutorial: CSS-Hintergrund
CSS-Referenz: background-image-Eigenschaft
CSS-Referenz: Eigenschaft background-position
HTML-DOM-Referenz: Hintergrundeigenschaft