CSS Eigenschaft background-position
Beispiel
So positionieren Sie ein Hintergrundbild:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Weitere „Probieren Sie es selbst“-Beispiele weiter unten.
Definition und Verwendung
Die background-position
Eigenschaft legt die Startposition eines Hintergrundbilds fest.
Tipp: Standardmäßig wird ein Hintergrundbild in der oberen linken Ecke eines Elements platziert und sowohl vertikal als auch horizontal wiederholt.
Standardwert: | 0 % 0 % |
---|---|
Vererbt: | Nein |
Animierbar: | ja. Lesen Sie mehr über animierbare |
Ausführung: | CSS1 |
JavaScript-Syntax: | Objekt .style.backgroundPosition="center" |
Browser-Unterstützung
Die Zahlen in der Tabelle geben die erste Browserversion an, die die Eigenschaft vollständig unterstützt.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Hinweis: IE8 und frühere Versionen unterstützen nicht mehrere Hintergrundbilder auf einem Element.
CSS-Syntax
background-position: value;
Eigenschaftswerte
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 |
Mehr Beispiele
Beispiel
So positionieren Sie ein Hintergrundbild so, dass es oben zentriert ist:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Beispiel
So positionieren Sie ein Hintergrundbild rechts unten:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Beispiel
So positionieren Sie ein Hintergrundbild mit Prozent:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Beispiel
So positionieren Sie ein Hintergrundbild mit Pixeln:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Beispiel
Verwenden Sie verschiedene Hintergrundeigenschaften, um ein „Helden“-Bild zu erstellen:
.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 */
}
Verwandte Seiten
CSS-Tutorial: CSS-Hintergrund
CSS-Referenz: background-image-Eigenschaft
HTML-DOM-Referenz: backgroundPosition-Eigenschaft