Style background 属性

Style 对象参考手册 Style 对象

定义和用法

background 属性以速记形式设置或返回最多五个独立的背景属性。

通过该属性,您可以设置/返回:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

上述的属性,也可以设置单独的样式属性。强烈建议使用单独的属性这样有更好的可控性。

语法

设置 background 属性:

Object.style.background="color image repeat attachment position"

返回 background 属性:

Object.style.background

提示:background 属性的默认值是:transparent none repeat scroll 0% 0%。

参数 描述
color 设置一个元素的背景颜色
image 设置一个元素的背景图像
repeat 设置如何重复背景图像
attachment 设置背景图像是否固定或者随着页面的其余部分滚动
position 设置背景图像的起始位置


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 background 属性。


实例

实例

设置文档背景的样式:

<html>
<head>
<script>
function displayResult()
{
document.body.style.background="#f3f3f3 url('img_tree.png') no-repeat right top";
}
</script>
</head>
<body>

<h1>Hello World!</h1>
<br>
<button type="button" onclick="displayResult()">Set background</button>

</body>
</html>

尝试一下 »


Style 对象参考手册 Style 对象