Vlastnost CSS na pozadí
Příklad
Jak umístit obrázek na pozadí:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Další příklady „Vyzkoušejte si to sami“ níže.
Definice a použití
Vlastnost background-position
nastavuje počáteční pozici obrázku na pozadí.
Tip: Ve výchozím nastavení je obrázek pozadí umístěn v levém horním rohu prvku a opakuje se svisle i vodorovně.
Výchozí hodnota: | 0 % 0 % |
---|---|
Zděděno: | Ne |
Animovatelné: | Ano. Přečtěte si o tématu Animable |
Verze: | CSS1 |
Syntaxe JavaScriptu: | objekt .style.backgroundPosition="center" |
Podpora prohlížeče
Čísla v tabulce určují první verzi prohlížeče, která plně podporuje vlastnost.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Poznámka: IE8 a starší nepodporují více obrázků pozadí na jednom prvku.
Syntaxe CSS
background-position: value;
Hodnoty vlastností
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 |
Další příklady
Příklad
Jak umístit obrázek na pozadí tak, aby byl vycentrován nahoře:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Příklad
Jak umístit obrázek na pozadí tak, aby byl vpravo dole:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Příklad
Jak umístit obrázek na pozadí pomocí procent:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Příklad
Jak umístit obrázek na pozadí pomocí pixelů:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Příklad
K vytvoření „hrdinského“ obrázku použijte různé vlastnosti pozadí:
.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 */
}
Související stránky
CSS tutoriál: CSS pozadí
CSS reference: vlastnost background-image
HTML DOM reference: vlastnost backgroundPosition