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.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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 ;
Sharpen Your Fundamentals with Free Learning

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.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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