HTML DOM Style backgroundAttachment Property


The backgroundAttachment property is used to set or get if the background image should scroll with respect to the page content or not.

Syntax

Following is the syntax for −

Setting the backgroundAttachment property −

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

Values

Following are the values −

Sr.NoValue & Description
1Scroll
This is the default value and it makes the background scroll with the image.
2Fixed
This fixes the background with regard to the viewport.
3Local
To make the background scroll along with the element's contents.
4Initial
For setting this property to initial value.
5inherit
To inherit the parent property value.

Example

Let us look at the example for the backgroundAttachment property −

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      background-image: url("https://www.tutorialspoint.com/artificial_intelligence_with_python/images/artificialintelligence-with-python-mini-logo.jpg");
      background-repeat: no-repeat;
      background-attachment: fixed;
      background-position: top;
   }
   article {
      color: white;
      background: rgba(0, 0, 0, 0.4);
   }
</style>
<script>
   function changeAttachment(){
      document.getElementById("BODY1").style.backgroundAttachment="local";
      document.getElementById("Sample").innerHTML="The background will now scroll along with the page.";
   }
</script>
</head>
<body id="BODY1">
<h2>This is demo heading</h2>
<p>Curabitur tincidunt elit libero, sed ullamcorper urna ullamcorper ut.
Nulla scelerisque, quam in efficitur porta, nisi turpis blandit odio, vitae ultricies lectus ex vel felis.
Proin lectus nulla, cursus vitae massa ac, mollis gravida orci. Cras euismod imperdiet elit, aliquet placerat purus bibendum vel.
Nullam vehicula urna non ullamcorper congue. Etiam volutpat gravida tempus. Fusce non volutpat diam.
Nullam sagittis condimentum dui, a pharetra mi commodo nec. Cras vulputate hendrerit ornare.
Sed id ligula sit amet arcu dapibus molestie nec at risus.
Aliquam gravida, dolor non vehicula lobortis, augue erat gravida metus, id eleifend ante risus vel turpis.
Nunc fermentum scelerisque nulla, non gravida dui consequat malesuada. Pellentesque mollis scelerisque quam ac efficitur.
Nunc ante velit, efficitur vitae dignissim semper, maximus in mi.</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
<p> Vivamus eget nisi eu libero viverra vestibulum. Nulla luctus ultrices metus, nec dapibus magna dignissim eget. Ut sed posuere erat.</p>
<p>Nunc ante velit, efficitur vitae dignissim semper, maximus in mi.</p>
<p>Pellentesque vitae est eget est tempus pretium.
Phasellus semper erat efficitur justo tristique sollicitudin.
Morbi mi eros, aliquet ac gravida ac, tempor et ante. </p>
<p>Aenean non justo non lorem accumsan fermentum efficitur at quam.
Morbi rhoncus lacus sed est luctus, a hendrerit est ullamcorper.
Etiam quis nisl non sem fringilla ullamcorper. Interdum et malesuada fames ac ante ipsum primis in faucibus.
Donec ac malesuada turpis. Donec dapibus diam metus, nec blandit tellus vulputate quis.</p>
<p>Donec blandit tincidunt ante, sed blandit massa volutpat eu. Mauris ac porta lectus, et vulputate ante.</p>
<p>Click the below button to change the above div backface visibility value</p>
<button onclick="changeAttachment()">CHANGE ATTACHMENT</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE ATTACHMENT button the image will be fixed at top and won’t be visible at bottom −

Updated on: 20-Aug-2019

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements