Open In App

HTML | DOM Style backgroundAttachment Property

The Style backgroundAttachment property in HTML DOM is used to set or return whether the background image should be fixed or scroll with the content. 

Syntax:



object.style.backgroundAttachment
object.style.backgroundAttachment = "scroll|fixed|local|initial|
inherit"

Return Values: It returns a string value, which representing how the background image is attached to the object within the document.

Property Values: Description of each property value with the example.



scroll: This value makes the background image scroll along with the element. It is the default value.

Example: 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style backgroundAttachment 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; margin-top: 100px;">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style backgroundAttachment Property</b>
     
    <p>
        Click on the button to change the attachment
        of the background image to 'scroll'.
    </p>
     
    <button onclick="changeAttachment()">
        Change attachment
    </button>
     
    <div id="scrolling-area"><br>
        This is a large area for scrolling.
    </div>
 
    <!-- Script to change backgroundAttachment -->
    <script>
        function changeAttachment() {
            document.body.style.backgroundAttachment
                    = 'scroll';
        }
    </script>
</body>
 
</html>                   

Output: 

Before clicking the button:

  

After clicking the button: 

fixed: This value makes the background image fixed with regard to the viewport.

Example: 




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

Output: 

Before clicking the button:

  

After clicking the button: 

local: This value makes the background image scroll along with the element’s content.

Example: 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style backgroundAttachment 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; margin-top: 100px;">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style backgroundAttachment Property</b>
     
    <p>
        Click on the button to change the attachment
        of the background image to 'scroll'.
    </p>
     
    <button onclick="changeAttachment()">
        Change attachment
    </button>
     
    <div id="scrolling-area"><br>
        This is a large area for scrolling.
    </div>
 
    <!-- Script to change backgroundAttachment -->
    <script>
        function changeAttachment() {
            document.body.style.backgroundAttachment
                    = 'local';
        }
    </script>
</body>
 
</html>                   

Output: 

Before clicking the button:

  

After clicking the button:

 

initial: It is used to set this property to its default value.

Example: 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style backgroundAttachment 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; margin-top: 100px;">
        GeeksforGeeks
    </h1>
     
    <b>DOM Style backgroundAttachment Property</b>
     
    <p>
        Click on the button to change the attachment
        of the background image to 'scroll'.
    </p>
     
    <button onclick="changeAttachment()">
        Change attachment
    </button>
     
    <div id="scrolling-area"><br>
        This is a large area for scrolling.
    </div>
 
    <!-- Script to change backgroundAttachment -->
    <script>
        function changeAttachment() {
            document.body.style.backgroundAttachment
                    = 'initial';
        }
    </script>
</body>
 
</html>                   

Output: 

Before clicking the button: 

 

After clicking the button:

 

inherit: It inherits the property from its parent element.

Example: 




<!DOCTYPE html>
<html>
     
<head>
    <title>
        DOM Style backgroundAttachment Property
    </title>
     
    <style>
        .bg-img {
            height: 150px;
            background: url(
            no-repeat right top / 200px;
        }
 
        #parent {
            background-attachment: fixed;
            height: 1000px;
        }
    </style>
</head>
 
<body>
    <div id="parent">
        <div class="bg-img"></div>
         
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
         
        <b>
            DOM Style backgroundAttachment Property
        </b>
         
        <p>
            Click on the button to change the attachment
            of the background image to 'inherit'.
        </p>
         
        <button onclick="changeAttachment()">
            Change attachment
        </button>
    </div>
 
    <!-- Script to change backgroundAttachment property -->
    <script>
        function changeAttachment() {
            elem = document.querySelector('.bg-img');
            elem.style.backgroundAttachment = 'inherit';
        }
    </script>
</body>
 
</html>                   

Output: 

Before clicking the button:

  

After clicking the button:

 

Supported Browsers: The browser supported by DOM Style backgroundAttachment property are listed below:


Article Tags :