Background Attachment in CSS

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The CSS background-attachment property determines whether the background image stays fixed within the viewport or scrolls with the page. This property accepts keyword values such as scroll, fixed, local, inherit, and initial. Background attachments in CSS can support multiple background images.

Syntax

This property takes values like a scroll, fixed, local, etc. Its default value is scroll, which causes the background image to scroll with the page.

In the following section of the article, we will discuss the various keyword values that the CSS background property supports.

Background Attachment in CSS: Property Values

ValueDescriptionSyntax
scrollIt is the default value. It sets the background image to stay in its position relative to the containing element and causes it to scroll with the page.background-attachment ;
fixedIt fixes the background image at its position relative to the viewport. The background image remains fixed regardless of the container's scrolling mechanism.background-image ;
localIt specifies the background image to scroll along with the content of its container element instead of the entire page.background-attachment;
InitialIt sets the background-attachment property to its default value.background-attachment;
InheritIt inherits the property from its parent element.background-attachment ;

Multiple Background Images

The property supports multiple background images. Each background image can have a different background-attachment property value, separated by commas.

SYNTAX

EXAMPLE

We can understand the background-attachment in CSS through the following examples.

Examples

Simple Examples

Example 1 : This example describes the scroll value. The background image scrolls with its container, displaying the default behavior of the background-attachment property.

HTML

Output

example-scroll-value

To view this in a better way click here

Example 2 : This example describes the fixed value. The background image stays fixed when we scroll the page.

HTML

CSS

Output

example-fixed-value-background-image

To view this in a better way click here

Example 3 : This example describes the local value. The background-image scrolls with the element's content.

HTML

CSS

Output

example-local-value-background-image

To view this in a better way click here

Multiple Background Images

Example 1: This example describes the multiple background image support.

In this example, we are using background attachments in CSS on two background images. The first image has a scroll value, whereas the second one has a fixed value. Therefore, the first image scrolls with the page, and the second one remains fixed.

HTML

CSS

Output

example-multiple-background-image

To view this in a better way click here

Browser Support

The following browsers support the CSS border-style property:

BrowserVersion
Google Chrome2.0
Safari1.0
Mozilla Firefox1.0
Microsoft Edge12.0
Chrome Android18
Firefox for Android4
Opera Android10.1
Safari on iOS3.2
Samsung Internet1.0
WebView Android37

Conclusion

  • The background-attachment property specifies whether the background image is fixed or scrolls with the rest of the page.
  • It controls the scrolling of an image in the background.
  • The property takes keyword values such as scroll, fixed, local, initial, and inherit
  • scroll value displays the default behavior.
  • Background attachment in CSS supports multiple image backgrounds.
  • background-image
  • background-position
  • background-repeat
  • background-size