HTML DOM Style backgroundImage Property


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.NoValues & Description
1url('URL')
For specifying the image file location.
2none
For specifying no background image to be used and it is the default value.
3initial
For setting this property to initial value.
4inherit
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 −

Updated on: 20-Aug-2019

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements