You are here: Reference > JavaScript > client-side > style handling > properties > backgroundImage

backgroundImage style property

Browser support:
Specifies or returns the image to use as the background of the object.

Syntax:

object.backgroundImage;
You can find the related objects in the Supported by objects section below.
This property is read/write.
CSS page for this property: background-image

Possible values:

The type of this property is string.
 One of the following values: 
url(URI)
Where URI specifies the location of the image file.
inherit
Takes the value of this property from the computed style of the parent element.
none
Default. Specifies that the current element does not have a background image.
Default: none.

Example HTML code 1:

This example illustrates the use of the background-image property:
<head>
    <style>
        .example {
            background-image: url("bg.jpg");
            background-repeat: repeat;
            width: 300px;
            height: 200px;
            overflow: scroll;
        }
    </style>
</head>
<body>
    <div class="example">
        Some content ..... <br /> ..... <br /> ..... <br />
        Some content ..... <br /> ..... <br /> ..... <br />
        Some content ..... <br /> ..... <br /> ..... <br />
        Some content ..... <br /> ..... <br /> ..... <br />
    </div>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the backgroundImage property in JavaScript:
<head>
    <script type="text/javascript">
        function ChangeImg () {
            div = document.getElementById ("myDiv");
            input = document.getElementById ("myInput");
            alert (input.value);

            div.style.backgroundImage = "url('" + input.value + "')";
        }
    </script>
</head>
<body>
    <div id="myDiv">Change the background-image</div>

    Please set a .gif, .png, .jpg or a .bmp file to see the effect.
    <input id="myInput" type="file" />
    <button onclick="ChangeImg ()">Change image</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content