CSS -achtergrondpositie Eigenschap
Voorbeeld
Hoe een achtergrondafbeelding te positioneren:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Meer "Probeer het zelf" voorbeelden hieronder.
Definitie en gebruik
De background-position
eigenschap stelt de startpositie van een achtergrondafbeelding in.
Tip: Standaard wordt een achtergrondafbeelding in de linkerbovenhoek van een element geplaatst en zowel verticaal als horizontaal herhaald.
Standaardwaarde: | 0% 0% |
---|---|
geërfd: | Nee |
Animeerbaar: | Ja. Lees over animeren |
Versie: | CSS1 |
JavaScript-syntaxis: | object .style.backgroundPosition="center" |
Browserondersteuning
De getallen in de tabel geven de eerste browserversie aan die de eigenschap volledig ondersteunt.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Opmerking: IE8 en eerder ondersteunen niet meerdere achtergrondafbeeldingen op één element.
CSS-syntaxis
background-position: value;
Eigendomswaarden
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 |
Meer voorbeelden
Voorbeeld
Hoe een achtergrondafbeelding te positioneren om bovenaan gecentreerd te worden:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Voorbeeld
Hoe plaats je een achtergrondafbeelding rechtsonder:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Voorbeeld
Een achtergrondafbeelding positioneren met procent:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Voorbeeld
Hoe een achtergrondafbeelding te positioneren met behulp van pixels:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Voorbeeld
Gebruik verschillende achtergrondeigenschappen om een "held"-afbeelding te maken:
.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 */
}
Gerelateerde pagina's
CSS-zelfstudie: CSS-achtergrond
CSS -referentie: eigenschap achtergrondafbeelding
HTML DOM-referentie: backgroundPosition eigenschap