CSS - свойство background-position
Пример
Как расположить фоновое изображение:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
Другие примеры «Попробуйте сами» ниже.
Определение и использование
Свойство background-position
устанавливает начальную позицию фонового изображения.
Совет. По умолчанию фоновое изображение размещается в верхнем левом углу элемента и повторяется как по вертикали, так и по горизонтали.
Значение по умолчанию: | 0% 0% |
---|---|
Унаследовано: | нет |
Анимация: | да. Читать об анимации |
Версия: | CSS1 |
Синтаксис JavaScript: | объект .style.backgroundPosition="центр" |
Поддержка браузера
Цифры в таблице указывают на первую версию браузера, которая полностью поддерживает это свойство.
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Примечание. IE8 и более ранние версии не поддерживают несколько фоновых изображений для одного элемента.
Синтаксис CSS
background-position: value;
Значения свойств
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 |
Дополнительные примеры
Пример
Как расположить фоновое изображение по центру вверху:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
Пример
Как расположить фоновое изображение внизу справа:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
Пример
Как расположить фоновое изображение с помощью процентов:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
Пример
Как расположить фоновое изображение с помощью пикселей:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
Пример
Используйте различные свойства фона, чтобы создать изображение «героя»:
.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 */
}
Связанные страницы
Учебное пособие по CSS: Фон CSS
Справочник по CSS: свойство background-image
Справочник по HTML DOM: свойство backgroundPosition