HTML | DOM Style backgroundSize Property
The HTML DOM Style backgroundSize Property is used to set or return the size of the background image.
Syntax:
- To get the backgroundSize property
object.style.backgroundSize
- To set the backgroundSize property
object.style.backgroundSize = "auto | length | percentage | cover| contain |initial | inherit"
Property Values:
- auto: This is used to display the background image in its original size. This is the default value.
- length: This is used to set the height and width of the image. Both the values set the width and height respectively. If only one value is given, the other one is set to ‘auto’.
- percentage: This sets the width and height in terms of percentage of the parent element. Both the values set the width and height respectively. If only one value is given, the other one is set to ‘auto’.
- cover: This is used to scale the background image to cover the whole container element.
- contain: This is used to scale the background image as large as possible such that both the height and width fit inside the container area.
- initial: This is used to set this property to its default value.
- inherit: This inherits the background size property from its parent.
- auto: This is used to display the background image in its original size it’s default value.
Example:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;/* we set the size ourselves to demonstrate auto */background-size: 100px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to autoelem.style.backgroundSize = 'auto';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- length: This is used to set the height and width of the image. Both the values set the width and height respectively. If only one value is given, the other one is set to ‘auto’.
Example:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to 200px in width// and 50px in heightelem.style.backgroundSize ='200px 50px';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- percentage: This sets the width and height in terms of percentage of the parent element. Both the values set the width and height respectively. If only one value is given, the other one is set to ‘auto’.
Example:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to 25% in width and 50% in heightelem.style.backgroundSize = '25% 50%';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- cover: This is used to scale the background image to cover the whole container element.
Example-4:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to coverelem.style.backgroundSize = 'cover';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- contain: This is used to scale the background image as large as possible such that both the height and width fit inside the container area.
Example-5:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to containelem.style.backgroundSize = 'contain';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- initial: This is used to set this property to its default value.
Example-6:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;/* we set the size ourselves to demonstrate initial */background-size: 100px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divclass="bg-img"></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to initialelem.style.backgroundSize = 'initial';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
- inherit: This inherits the background size property from its parent.
Example-7:
<!DOCTYPE html><htmllang="en"><head><title>DOM Style backgroundSize Property</title><style>.bg-img {height: 200px;width: 200px;border-style: solid;background-image: url(background-repeat: no-repeat;}#parent {background-size: 100px 200px;}</style></head><body><h1style="color: green">GeeksforGeeks</h1><b>DOM Style backgroundSize Property</b><p>Click on the button tochange the size of the background image</p><divid="parent"><divclass="bg-img"></div></div><buttononclick="changePos()">Change size of image</button><script>function changePos() {elem = document.querySelector('.bg-img');// Setting size to inheritelem.style.backgroundSize = 'inherit';}</script></body></html>chevron_rightfilter_noneOutput:
- Before pressing the button:

- After pressing the button:

- Before pressing the button:
Supported Browsers: The browser supported by backgroundSize Property are listed below:
- Chrome 4.0
- Internet Explorer 9.0
- Firefox 4.0
- Safari 4.1
- Opera 10.5
Recommended Posts:
- HTML | DOM Style right Property
- HTML | DOM Style top Property
- HTML | DOM Style flexFlow Property
- HTML | DOM Style orphans Property
- HTML | DOM Style perspectiveOrigin Property
- HTML | DOM Style borderCollapse Property
- HTML | DOM Style flex Property
- HTML | DOM Style tableLayout Property
- HTML | DOM Style border Property
- HTML | DOM Style emptyCells Property
- HTML | DOM Style justifyContent Property
- HTML | DOM Style minHeight Property
- HTML | DOM Style quotes Property
- HTML | DOM Style animationName Property
- HTML | DOM Style marginLeft 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.



