Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.
Краткое описание
Свойство CSS background-image
устанавливает одно или несколько фоновых изображений для элемента. The images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.
The borders border
of the element are then drawn on top of them, and the background-color
is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the background-clip
and background-origin
CSS properties.
If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none
value.
background-color
. Если изображение не может быть загружено —например, в случае отказа сетевого подключения — у элемента будет отображён цветной фон.Начальное значение | none |
---|---|
Применяется к | все элементы. Это также применяется к ::first-letter и ::first-line . |
Наследуется | нет |
Отображение | визуальный |
Обработка значения | как указано, но с абсолютными значениями url |
Animation type | discrete |
Канонический порядок | уникальный неоднозначный порядок, определённый формальной грамматикой |
Синтаксис
background-image: none; background-image: url(http://www.example.com/bck.png); background-image: inherit;
Значения
none
- Is a keyword denoting the absence of images.
<image>
- Is an
<image>
denoting the image to display. There can be several of them, separated by commas, as multiple backgrounds are supported.
Formal syntax
<bg-image>#где
<bg-image> = none | <image>
где
<image> = <url> | <image()> | <image-set()> | <element()> | <cross-fade()> | <gradient>
где
<image()> = image( [ [ <image> | <string> ]? , <color>? ]! )
<image-set()> = image-set( <image-set-option># )
<element()> = element( <id-selector> )
<cross-fade()> = cross-fade( <cf-mixing-image> , <cf-final-image>? )
<gradient> = <linear-gradient()> | <repeating-linear-gradient()> | <radial-gradient()> | <repeating-radial-gradient()>где
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
<image-set-option> = [ <image> | <string> ] <resolution>
<cf-mixing-image> = <percentage>? && <image>
<cf-final-image> = <image> | <color>
<linear-gradient()> = linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<repeating-linear-gradient()> = repeating-linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<radial-gradient()> = radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )
<repeating-radial-gradient()> = repeating-radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )где
<rgb()> = rgb( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<rgba()> = rgba( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<hsl()> = hsl( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<hsla()> = hsla( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<side-or-corner> = [ left | right ] || [ top | bottom ]
<color-stop-list> = <color-stop>#{2,}
<ending-shape> = circle | ellipse
<size> = closest-side | farthest-side | closest-corner | farthest-corner | <length> | <length-percentage>{2}
<position> = [ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]где
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
<color-stop> = <color> <length-percentage>?
<length-percentage> = <length> | <percentage>
Примеры
Multiple backgrounds and transparency
Note that the star image is partially transparent and is layered over the cat image.
HTML Content
<div> <p class="catsandstars"> This paragraph is full of cats<br />and stars. </p> <p>This paragraph is not.</p> <p class="catsandstars"> Here are more cats for you.<br />Look at them! </p> <p>And no more.</p> </div>
CSS Content
pre, p { font-size: 1.5em; color: #FE7F88; background-color: transparent; } div { background-image: url("https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png"); } p { background-image: none; } .catsandstars { background-image: url("https://mdn.mozillademos.org/files/11991/startransparent.gif"), url("https://mdn.mozillademos.org/files/7693/catfront.png"); background-color: transparent; }
Спецификации
Specification | Status | Comment |
---|---|---|
CSS Backgrounds and Borders Module Level 3 Определение 'background-image' в этой спецификации. |
Кандидат в рекомендации | From CSS2 Revision 1, the property has been extended to support multiple backgrounds and any <image> CSS data type. |
CSS Level 2 (Revision 1) Определение 'background-image' в этой спецификации. |
Рекомендация | From CSS1, the way images with and without intrinsic dimensions are handled is now described. |
CSS Level 1 Определение 'background-image' в этой спецификации. |
Рекомендация |
Совместимость браузеров
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 или ранее)[1] | 4.0 | 3.5 | 1.0 |
Multiple backgrounds | 1.0 | 3.6 (1.9.2) | 9.0 | yes | 1.3 |
Gradients | 1.0-webkit | 3.6 (1.9.2)-moz | 10 | 11+-o | 4.0-webkit |
SVG images | 8.0 | 4.0 (2.0) | 9.0 | 9.5 | 5.0 [2] |
element |
? | (Да)-moz | Нет | Нет | Нет |
image-rect |
Нет | (Да)-moz | Нет | Нет | Нет |
Any <image> value. |
? | Нет | ? | ? | ? |
Feature | iOS Safari | Opera Mini | Opera Mobile | Android Browser |
---|---|---|---|---|
Basic support | yes | yes | yes | yes |
Multiple backgrounds | yes | yes | yes | yes |
Gradients | yes -webkit old webkit syntax for iOS 4.2 and older |
? | ? | yes-webkit old webkit syntax |
SVG images | yes [2] | yes | yes | Нет |
element |
(Да)-moz | ? | ? | ? |
image-rect |
(Да)-moz | Нет | Нет | Нет |
Any <image> value |
Нет | ? | ? | ? |
[1] If the browser.display.use_document_colors
user preference in about:config
is set to false
, background images will not de displayed.
[2] Support of SVG in CSS background is incomplete in the current version of iOS Safari (5.0). The same for Safari prior 5.0.
Смотрите также
- CSS Image Sprites
<img>
- Image-related articles:
<image>
,linear-gradient
,radial-gradient
,repeating-linear-gradient
,repeating-radial-gradient
,element
.