CSS Background Attachment
CSS background-attachment is a property that controls how a background image behaves when a user scrolls a webpage. It defines whether the background image moves with the content, stays fixed in place, or scrolls within a specific element.
This property plays an important role in modern web design. It helps developers create scrolling effects, parallax designs, and smooth visual experiences without using JavaScript.
What Is CSS Background Attachment?
The background-attachment property determines whether a background image scrolls with its element or remains fixed relative to the viewport.
In simple terms, it answers this question:
Should the background move when the page scrolls?
It works with:
background-imagebackground-positionbackground-sizebackground-repeat
Syntax of background-attachment
selector {
background-attachment: value;
}
Available Values
The property supports three main values:
| Value | Description |
|---|---|
| scroll | Background scrolls with the page (default) |
| fixed | Background stays fixed relative to viewport |
| local | Background scrolls with the element’s content |
1. background-attachment: scroll
This is the default behavior.
body {
background-image: url("image.jpg");
background-attachment: scroll;
}
How It Works
- The background moves when the page scrolls.
- It behaves like normal content.
- Most websites use this behavior by default.
When to Use
- Standard layouts
- Basic websites
- When no special effect is required
2. background-attachment: fixed
body {
background-image: url("image.jpg");
background-attachment: fixed;
}
How It Works
- The background stays fixed.
- Content scrolls over it.
- Creates a parallax-like effect.
Real-World Use
This effect became popular in modern landing pages and parallax scrolling websites.
Best Use Cases
- Hero sections
- Full-screen backgrounds
- Parallax design
- Creative portfolio websites
Important Note
On some mobile browsers, background-attachment: fixed may not work as expected due to performance restrictions.
3. background-attachment: local
div {
background-image: url("image.jpg");
background-attachment: local;
overflow: auto;
}
How It Works
- The background scrolls along with the element’s content.
- It only works when the element has scrolling (overflow).
When to Use
- Scrollable containers
- Chat boxes
- Custom scroll panels
- Text areas with background image
Combining background-attachment with Other Properties
To get the best result, combine it with:
.hero {
background-image: url("hero.jpg");
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
Why Combine These?
background-size: coverensures full coverage.background-position: centerkeeps image centered.no-repeatprevents tiling.
Shorthand Property
You can use background shorthand:
.hero {
background: url("hero.jpg") no-repeat center center fixed;
background-size: cover;
}
Example: Simple Parallax Section
<section class="parallax"></section>
.parallax {
height: 500px;
background-image: url("mountain.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
This creates a visually attractive parallax effect using only CSS.
Browser Support
background-attachment is widely supported in all modern browsers:
- Chrome
- Firefox
- Edge
- Safari
However:
- Mobile Safari may ignore
fixedin some cases. - Performance optimization is applied on mobile devices.
Performance Considerations
Using background-attachment: fixed can:
- Increase repaint cost
- Affect scroll performance
- Reduce smoothness on low-end devices
Optimization Tips
- Use compressed images
- Use
background-size: cover - Avoid large heavy images
- Test on mobile devices
Accessibility Considerations
Background images:
- Should not contain important text
- Should maintain good contrast
- Should not affect readability
If the image carries meaning, use <img> instead of background.
Advanced Design Techniques
1. Layered Backgrounds
body {
background-image: url("pattern.png"), url("main.jpg");
background-attachment: scroll, fixed;
}
Multiple backgrounds can have different attachment values.
2. Creating Modern Hero Sections
Common structure:
.hero {
height: 100vh;
background: url("hero.jpg") center / cover no-repeat fixed;
display: flex;
align-items: center;
justify-content: center;
}
Used widely in:
- Landing pages
- Portfolio websites
- Startup websites
Difference Between scroll, fixed, and local
| Feature | scroll | fixed | local |
|---|---|---|---|
| Moves with page | Yes | No | Yes (inside element) |
| Parallax effect | No | Yes | No |
| Works inside overflow element | No | No | Yes |
| Default value | Yes | No | No |
Common Mistakes
- Forgetting to set height on element
- Not using
background-size: cover - Expecting fixed to work perfectly on mobile
- Using large unoptimized images
Practical Real-World Example
Many modern frameworks and platforms use background-attachment for hero effects, including themes built with CSS frameworks like Bootstrap and utility frameworks like Tailwind CSS.
SEO and UX Perspective
Using background images with fixed attachment:
- Improves visual appeal
- Enhances first impression
- Reduces need for JavaScript
- Helps improve page speed when optimized correctly
However, always:
- Optimize images
- Avoid text embedded in background
- Ensure readability
Summary
CSS background-attachment is a powerful yet simple property that controls scrolling behavior of background images. It supports three values:
- scroll (default)
- fixed (viewport-based)
- local (element-based scrolling)
It is commonly used for:
- Parallax effects
- Hero sections
- Creative scrolling designs
- Modern UI layouts
When used properly with optimized images and responsive design, it can dramatically improve a website’s visual experience without affecting performance.
FAQ Section
What is CSS background-attachment?
CSS background-attachment is a property that controls whether a background image scrolls with the page, stays fixed, or scrolls within an element.
What are the values of background-attachment?
The three main values are:
- scroll (default)
- fixed
- local
How does background-attachment fixed work?
When set to fixed, the background image stays in place while the page content scrolls over it.
Why is background-attachment fixed not working on mobile?
Some mobile browsers limit or disable fixed backgrounds to improve performance and reduce rendering issues.
What is the difference between scroll and local?
Scroll moves the background with the page, while local moves the background only within a scrollable element.