The background-attachment property determines whether a background image scrolls with the rest of the page or remains in a fixed position. This property is key for creating parallax scrolling effects and other interesting visual treatments for your backgrounds.
Example 1: The Default scroll Value
.container {
background-image: url('image.jpg');
/* The background image will scroll with the page (default behavior) */
background-attachment: scroll;
height: 500px;
border: 1px solid #ccc;
}
Explanation
scroll is the default value for background-attachment. It causes the background image to move along with the element as the user scrolls the page.
Example 2: The fixed Value
.container {
background-image: url('image.jpg');
background-size: cover;
/* The background image will remain fixed in the viewport */
background-attachment: fixed;
height: 500px;
border: 1px solid #ccc;
}
Explanation
When background-attachment is set to fixed, the background image does not scroll with the page. It remains in a fixed position relative to the viewport, creating a parallax-like effect.
Example 3: The local Value
.container {
background-image: url('image.jpg');
/* The background will scroll with the element's content */
background-attachment: local;
height: 300px;
overflow: auto;
border: 1px solid #ccc;
}
Explanation
The local value is useful for elements that have their own scrollbars. The background image will scroll along with the content inside that specific element, rather than the entire page.
Example 4: fixed for Full-Page Backgrounds
body {
background-image: url('background.jpg');
background-size: cover;
background-position: center;
/* Creates a full-page background that stays in place */
background-attachment: fixed;
}
Explanation
Applying background-attachment: fixed to the body element is a common technique for creating a full-page background that remains static as the user scrolls through the content.
Example 5: Multiple Backgrounds with Different Attachments
.container {
background-image: url('foreground.png'), url('background.jpg');
background-position: center, center;
background-repeat: no-repeat, no-repeat;
background-size: 150px, cover;
/* The foreground scrolls, the background is fixed */
background-attachment: scroll, fixed;
height: 500px;
}
Explanation
You can apply different background-attachment values to multiple background images. In this example, a smaller foreground image scrolls with the page, while the larger background image remains fixed.
Example 6: Combining local with Padding
.container {
background-image: url('pattern.png');
background-repeat: repeat;
/* The pattern will scroll with the text content */
background-attachment: local;
height: 200px;
padding: 20px;
overflow: auto;
border: 1px solid #ccc;
}
Explanation
This demonstrates how local works with padded content. The background pattern will be visible within the padded area and will scroll as the content within the container is scrolled.
Example 7: Parallax Effect with fixed
.parallax-section {
background-image: url('scenic.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100vh; /* Full viewport height */
}
Explanation
This code creates a classic parallax scrolling section. By setting the height to the full viewport height and using background-attachment: fixed, the background image appears to move at a different speed than the foreground content, creating a sense of depth.