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:
- It returns the backgroundAttachment property.
object.style.backgroundAttachment
- It is used to set the backgroundAttachment property.
object.style.backgroundAttachment = "scroll|fixed|local|initial| inherit"
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><h1style="color: green; margin-top: 100px;">GeeksforGeeks</h1><b>DOM Style backgroundAttachment Property</b><p>Click on the button to change the attachmentof the background image to 'scroll'.</p><buttononclick="changeAttachment()">Change attachment</button><divid="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>chevron_rightfilter_none - Output:
Before clicking the button:

After clicking the button:

- 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><h1style="color: green; margin-top: 100px;">GeeksforGeeks</h1><b>DOM Style backgroundAttachment Property</b><p>Click on the button to change the attachmentof the background image to 'scroll'.</p><buttononclick="changeAttachment()">Change attachment</button><divid="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>chevron_rightfilter_none - Output:
Before clicking the button:

After clicking the button:

- 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><h1style="color: green; margin-top: 100px;">GeeksforGeeks</h1><b>DOM Style backgroundAttachment Property</b><p>Click on the button to change the attachmentof the background image to 'scroll'.</p><buttononclick="changeAttachment()">Change attachment</button><divid="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>chevron_rightfilter_none - Output:
Before clicking the button:

After clicking the button:

- 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><h1style="color: green; margin-top: 100px;">GeeksforGeeks</h1><b>DOM Style backgroundAttachment Property</b><p>Click on the button to change the attachmentof the background image to 'scroll'.</p><buttononclick="changeAttachment()">Change attachment</button><divid="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>chevron_rightfilter_none - Output:
Before clicking the button:

After clicking the button:

- 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><divid="parent"><divclass="bg-img"></div><h1style="color: green;">GeeksforGeeks</h1><b>DOM Style backgroundAttachment Property</b><p>Click on the button to change the attachmentof the background image to 'inherit'.</p><buttononclick="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>chevron_rightfilter_none - Output:
Before clicking the button:

After clicking the button:

fixed: This value makes the background image fixed with regard to the viewport.
local: This value makes the background image scroll along with the element’s content.
initial: It is used to set this property to its default value.
inherit: It inherits the property from its parent element.
Supported Browsers: The browser supported by DOM Style backgroundAttachment property are listed below:
- Google Chrome 1.0
- Internet Explorer 4.0
- Firefox 1.0
- Opera 3.5
- Safari 1.0
Recommended Posts:
- HTML | DOM Style right Property
- HTML | DOM Style top Property
- HTML | DOM Style borderLeft Property
- HTML | DOM Style borderRight Property
- HTML | DOM Style color Property
- HTML | DOM Style borderBottom Property
- HTML | DOM Style borderBottomColor Property
- HTML | DOM Style filter Property
- HTML | DOM Style margin Property
- HTML | DOM Style objectFit Property
- HTML | DOM Style tableLayout Property
- HTML | DOM Style flexBasis Property
- HTML | DOM Style flexDirection Property
- HTML | DOM Style borderCollapse Property
- HTML | DOM Style columnRule 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.



