Latest web development tutorials

HTML DOM Style background properties

Style Object Reference Style Objects

Definition and Usage

background shorthand property to set or return up to five separate background properties.

With this property, you can set / return:

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

The above properties may be provided separate style attributes. Strongly recommends using a separate attribute such a better controllability.

grammar

Set the background properties:

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

Back background properties:

Object.style.background

Tip: The default value of the property is the background: transparent none repeat scroll 0% 0 %.

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support background attributes.


Examples

Examples

Set document background style:

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "utf-8">
<Title> This tutorial (w3big.com) </ title>
<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 the background </ button>

</ Body>
</ Html>

try it"


Style Object Reference Style Objects