HTML DOM Style backgroundSize Property


The HTML DOM style background-size property is used to set or get the size of background image.

Syntax

Following is the syntax for −

Setting the backgroundSize property −

object.style.backgroundSize = "auto|length|cover|contain|intial|inherit"

Properties

The above properties are explained as follows −

Sr.NoProperties & Description
1auto
The background image displays at full size and it is the default value.
2length
For setting the width and height of the background image. The first value is for width and the second is for height. If only one value is given then the missing value defaults to auto.
3percentage
For setting the width and height of the background image in percentage. The percentage is calculated based on parents height and width. The first value sets the width, the second value sets the height. If only one value is given then the missing value defaults to auto.
4cover
For scaling the background image to be as large to cover the complete background area.
5contain
To scale the image so that its height and width can fit inside the content area.
6initial
For setting this property to initial value.
7inherit
To inherit the parent property value

Example

Let us look at the example for the HTML DOM style backgroundSize property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   #DIV1{
      box-shadow: 0 2px 0 4px mintcream;
      padding: 45px;
      background: url("https://www.tutorialspoint.com/plsql/images/plsql-mini-logo.jpg");
      background-size: 150px 150px;
   }
</style>
<script>
   function changeBackSize(){
      document.getElementById("DIV1").style.backgroundSize="100px 100px";
      document.getElementById("Sample").innerHTML="The background image size is now reduced";
   }
</script>
</head>
<body>
<h2>PL/SQL Tutorial</h2>
<div id="DIV1"></div>
<p>Change the background image size for the above div by clicking the below button</p>
<button onclick="changeBackSize()">CHANGE SIZE</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE SIZE button −

Updated on: 15-Feb-2021

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements