HTML | DOM Style backgroundImage Property
The DOM Style backgroundImage Property is used to set or return the background image of an element.
Syntax:
- To get the backgroundImage property
object.style.backgroundImage
- To set the backgroundImage property
object.style.backgroundImage = "image | none | initial | inherit"
Property Values:
- image: This sets the property to use the image specified. The image path may be specified in the url() function.
- none: This sets the property to use no background image.
- initial: This is used to set this property to its default value.
- inherit: This is used to inherit the property from its parent.
- image: This sets the property to use the image specified. The image path may be specified in the url() function.
Example-1:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundImage Property</title><style>.bg-img {border: solid;height: 180px;width: 200px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundImage Property</b><pclass="bg-img"></p><buttononclick="changeImage()">Change source of background image</button><script>function changeImage() {elem = document.querySelector('.bg-img');// Setting the backgroundImage to an urlelem.style.backgroundImage =}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:

- After clicking the button:

- Before clicking the button:
- none: This sets the property to use no background image.
Example-2:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundImage Property</title><style>.bg-img {border: solid;height: 180px;width: 200px;background-image:}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundImage Property</b><pclass="bg-img"></p><buttononclick="changeImage()">Change source of background image</button><script>function changeImage() {elem = document.querySelector('.bg-img');// Setting the backgroundImage to noneelem.style.backgroundImage = "none";}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:

- After clicking the button:

- Before clicking the button:
- initial: This is used to set this property to its default value.
Example-3:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundImage Property</title><style>.bg-img {border: solid;height: 180px;width: 200px;background-image:}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundImage Property</b><pclass="bg-img"></p><buttononclick="changeImage()">Change source of background image</button><script>function changeImage() {elem = document.querySelector('.bg-img');// Setting the backgroundImage to initialelem.style.backgroundImage = "initial";}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:

- After clicking the button:

- Before clicking the button:
- inherit: This is used to inherit the property from its parent.
Example-4:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundImage Property</title><style>#parent {border: solid;height: 200px;width: 300px;background:no-repeat;background-size: cover;}.bg-img {border: dashed;height: 100px;width: 100px;background-size: cover;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundImage Property</b><divid="parent"><pclass="bg-img"></p></div><buttononclick="changeImage()">Change source of background image</button><script>function changeImage() {elem = document.querySelector('.bg-img');// Setting the backgroundImage to inheritelem.style.backgroundImage = "inherit";}</script></body></html>chevron_rightfilter_noneOutput:
- Before clicking the button:

- After clicking the button:

- Before clicking the button:
Supported Browsers: The browser supported by backgroundImage Property are listed below:
- Chrome 1.0
- Internet Explorer 4.0
- Firefox 1.0
- Opera 3.5
- Safari 1.0
Recommended Posts:
- HTML | DOM Style top Property
- HTML | DOM Style right Property
- HTML | DOM Style position Property
- HTML | DOM Style isolation Property
- HTML | DOM Style transitionProperty Property
- HTML | DOM Style clear Property
- HTML | DOM Style order Property
- HTML | DOM Style columnFill Property
- HTML | DOM Style cssFloat Property
- HTML | DOM Style backgroundColor Property
- HTML | DOM Style flexBasis Property
- HTML | DOM Style wordSpacing Property
- HTML | DOM Style textAlign Property
- HTML | DOM Style visibility Property
- HTML | DOM Style marginTop Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



