The backgroundImage property is used for setting or returning a background image for an element.
Syntax
Following is the syntax for −
Setting the backgroundImage property:
object.style.backgroundImage = "url('URL')|none|initial|inherit"Values
The above property values are explained as follows −
| Sr.No | Values & Description |
|---|---|
| 1 | url('URL') For specifying the image file location. |
| 2 | none For specifying no background image to be used and it is the default value. |
| 3 | initial For setting this property to initial value. |
| 4 | inherit To inherit the parent property value. |
Example
Let us look at the example for the backgroundImage property −
<!DOCTYPE html>
<html>
<head>
<style>
#DIV1 {
height: 300px;
width: 500px;
background-image: url("https://www.tutorialspoint.com/mongodb/images/mongodb-minilogo.jpg");
background-repeat: no-repeat;
background-size: cover;
}
</style>
<script>
function changeBackImage(){
document.getElementById("DIV1").style.backgroundImage="url('https://www.tutorialspoint.com/plsql/images/plsql-mini-logo.jpg')";
document.getElementById("Sample").innerHTML="The background image is now changed to PL/SQL Tutorial";
}
</script>
</head>
<body>
<h2>Learn</h2>
<div id="DIV1"></div>
<p>Change the above div background image by clicking the below button</p>
<button onclick="changeBackImage()">CHANGE IMAGE</button>
<p id="Sample"></p>
</body>
</html>Output
This will produce the following output −

On clicking the CHANGE IMAGE button −
