Open In App

HTML | DOM Style background Property

The Style background property in HTML DOM is a short hand property used to set or return the background of an element. It can help to manipulate one or more of eight background properties. 

Syntax:



object.style.background
object.style.background = "color image repeat attachment 
position size origin clip|initial|inherit"

Return Values: It returns a string value, which represents the background of an element.

Property Values: There are 8 property in background those are described below:



Example 1: background-color property 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background color
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set the background color -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background = "green";
        }
    </script>
</body>
 
</html>                   

Output:

 

 

Example 2: background-image property 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background image
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set the background image -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background =
        }
    </script>
</body>
 
</html>                   

Output:

 

 

Example 3: This example using repeat-x property to repeat the background image along the x axis. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background image
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set the background color -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background =
        }
    </script>
</body>
 
</html>                   

Output:

 

 

Example 4: This example sets the attachment property to ‘scroll’. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        body {
            background: url(
            no-repeat right top / 200px;
            background-attachment: fixed;
        }
        #scrolling-area {
            height: 1000px;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background color
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <div id="scrolling-area"><br>
        This is a large area for scrolling.
    </div>
 
    <!-- Script to change backgroundAttachment -->
    <script>
        function setBg() {
            document.body.style.backgroundAttachment
                    = 'scroll';
        }
    </script>
</body>
 
</html>                    

Output:

Example 5: This example sets the position of the background image to ‘center’. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background image
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set the background color -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background =
        }
    </script>
</body>
 
</html>                   

Output:

 

 

Example 6: This example sets the size of the background image to ‘200px’ in width and ‘150px’ in height. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background image
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set the background color -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background =
        }
    </script>
</body>
 
</html>                   

Output:

Example 7: This example sets the background origin to ‘border-box’. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            padding: 20px;
            border: 10px dotted;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background image
    </p>
 
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background image
    </button>
     
    <!-- Script to set background origin property -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background =
        }
    </script>
</body>
</html>                   

Output:

 

Example 8: This example sets the background clip to ‘content-box’. 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border: 10px dotted;
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set
        the background color
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background color
    </button>
     
    <!-- Script to set background clip property -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background = "green content-box";
        }
    </script>
</body>
</html>                   

Output:

 

 

Example 9: It set the property it’s default 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            height: 250px;
            width: 250px;
            border-style: solid;
            background: green
        }
    </style>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style Background Property</b>
     
    <p>
        Click on the button to set the
        background to initial
    </p>
     
    <div class="GFG"></div>
     
    <button onclick="setBg()">
        Set background
    </button>
     
    <!-- Script to set background property -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background = "initial";
        }
    </script>
</body>
 
</html>                   

Output: 

 

 

Example 10: It is used to inherit the property from its parent




<!DOCTYPE html>
<html>
<head>
    <title>
        DOM Style Background Property
    </title>
     
    <style>
        .GFG {
            margin: 20px;
            height: 100px;
            width: 100px;
            border: 5px solid;
        }
 
        #parent {
            height: 250px;
            width: 250px;
            border: 1px solid;
            background:
        }
    </style>
</head>
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>DOM Style Background Property</b>
     
<p>
        Click on the button to set
        the background to inherit
    </p>
 
     
    <div id="parent">
        <div class="GFG"></div>
    </div>
     
    <button onclick="setBg()">
        Set background
    </button>
     
    <!-- Script to set background property -->
    <script>
        function setBg() {
            elem = document.querySelector('.GFG');
            elem.style.background = "inherit";
        }
    </script>
</body>
</html>

Output:

 

 

Supported Browsers: The browser supported by background Property are listed below:


Article Tags :