Style background 属性
示例
设置文档的背景样式
document.body.style.background = "#f3f3f3 url('img_tree.png') no-repeat right top";
自己动手试一试 »
更多“自己尝试”的例子见下文。
描述
background 属性以简写形式设置或返回最多八个独立的背景属性。
使用此属性,您可以设置/返回以下一个或多个(按任意顺序):
DOM 属性 | CSS 属性 |
---|---|
backgroundAttachment | background-attachment |
backgroundClip | background-clip |
backgroundColor | background-color |
backgroundImage | background-image |
backgroundOrigin | background-origin |
backgroundPosition | background-position |
backgroundRepeat | background-repeat |
backgroundSize | background-size |
上述属性也可以通过单独的样式属性设置。强烈建议非高级作者使用单独的属性,以获得更好的可控性。
语法
返回 background 属性
object.style.background
设置 background 属性
object.style.background = "color image repeat attachment position size origin clip|initial|inherit"
属性值
值 | 描述 |
---|---|
附件 | 设置背景图像是固定还是滚动 |
clip | 设置背景图像的绘制区域 |
color | 设置元素的背景颜色 |
图片 | 设置元素的背景图像 |
origin | 设置背景定位区域 |
position | 设置背景图像的起始位置 |
重复 | 设置背景图像的重复方式 |
大小 | 设置背景图像的大小 |
initial | 将此属性设置为其默认值。阅读关于 initial |
inherit | 从其父元素继承此属性。阅读关于 inherit |
技术详情
默认值 | transparent none repeat scroll 0% 0% auto padding-box border-box |
---|---|
返回值 | 一个字符串,表示元素的背景 |
CSS 版本 | CSS1 |
浏览器支持
background
是 CSS1 (1996) 的特性。
所有浏览器都完全支持。
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |
注意
CSS3 (1999) 中新增了 3 个属性
- background-clip
- background-origin
- background-size
更多示例
示例
更改 DIV 元素的背景
document.getElementById("myDIV").style.background = "url('smiley.gif') blue repeat-x center";
自己动手试一试 »