Style backgroundImageProperty

Style 对象参考手册Style Object

Definition and Usage

The backgroundImage property sets or returns the background image of an element.

Syntax

Setting the backgroundImage property:

Object.style.backgroundImage="url('URL')|none|inherit"

Returning the backgroundImage property:

Object.style.backgroundImage

Value Description
url('URL') The location of the image file.
none Default. No background image.
inherit The setting of the background-image property is inherited from the parent element.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the backgroundImage property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".


Tips and Notes

Tip:In addition to the background image, you should also specify a background color. If the image is unavailable, the background color will be used.


Examples

Examples

Set background image:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example Tutorial (example.com)</title>
<script>
function displayResult(){
    document.body.style.backgroundColor="#f3f3f3";
    document.body.style.backgroundImage="url('img_tree.png')";
}
</script>
</head>
<body>

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

</body>
</html>

Try it Yourself »


Style 对象参考手册Style Object Other Extensions