background-image

这篇翻译不完整。请帮忙从英语翻译这篇文章

概述

background-image 为页面元素指定背景图。

The CSS background-image property sets one or several background images for an element. 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 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.

Note: Even if the images are opaque and the color won't be displayed in normal circumstances, web developers should always specify a background-color. If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.

语法

background-image: none;
background-image: url(http://www.example.com/bck.png);

background-image: inherit;
 
 
 
 

取值

 
none
是一个表示无背景图的关键字。
<image>
Is an <image> denoting the image to display. There can be several of them, separated by commas, as multiple backgrounds are supported.

正规语法

<bg-image>#

例子

Transparent and opaque images

查看示例

Multiple backgrounds and transparency

Note that the star image is partially transparent and is layered over the cat image.

HTML 内容

<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 内容

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://developer.mozilla.org/samples/cssref/images/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
Candidate Recommendation 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
Recommendation From CSS1, the way images with and without intrinsic dimensions are handled is now described.
CSS Level 1
background-image
Recommendation  

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 1.0 (1.7 or earlier)[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 ? (Yes)-moz 未实现 未实现 未实现
image-rect 未实现 (Yes)-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 (Yes)-moz ? ? ?
image-rect (Yes)-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.

提示

开发人员应当在给元素设定背景图的同时给元素指定背景色background-color,当背景图不可用时背景色替代。

参见

 

 

 

文档标签和贡献者

向此页面作出贡献: FredWe, JeanLee, ziyunfei, teoli, Mickeyboy
最后编辑者: FredWe,