CSS Background Attachment Explained with Examples

Learn CSS background-attachment property with scroll, fixed, and local values. Understand syntax, examples, and how to create parallax effects.

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-image
  • background-position
  • background-size
  • background-repeat

Syntax of background-attachment

selector {
  background-attachment: value;
}

Available Values

The property supports three main values:

ValueDescription
scrollBackground scrolls with the page (default)
fixedBackground stays fixed relative to viewport
localBackground 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: cover ensures full coverage.
  • background-position: center keeps image centered.
  • no-repeat prevents 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 fixed in 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

Featurescrollfixedlocal
Moves with pageYesNoYes (inside element)
Parallax effectNoYesNo
Works inside overflow elementNoNoYes
Default valueYesNoNo

Common Mistakes

  1. Forgetting to set height on element
  2. Not using background-size: cover
  3. Expecting fixed to work perfectly on mobile
  4. 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.