CSS background-attachment Property

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The background-attachment property decides what a background-image is locked to when scrolling happens. The image can stick to the element (scroll), to the viewport (fixed), or to the element's own overflowing content (local).

The initial value is scroll. The property is not inherited. It applies to every element, including ::first-letter and ::first-line. With several images, list one keyword per layer, separated by commas. With fixed, background-origin is ignored.

Standard-track.

Not deprecated and not experimental. Baseline widely available. On many iOS Safari versions, fixed still behaves like scroll by design — see Edge Cases.

CSS Syntax

syntax.css
background-attachment: scroll | fixed | local | initial | inherit;

For multiple images, write a comma list such as fixed, scroll. Each keyword matches one image in the same order.

Property Values

ValueDescriptionTry it
scrollDefault. Image sticks to the element. Inner overflow does not move itTry it
fixedImage sticks to the viewport. The element slides over itTry it
localImage sticks to the content. Inner overflow scrolls the image tooTry it
fixed, scrollOne keyword per background layer, in the same order as the imagesTry it
initialResets this property to scrollTry it
inheritCopies this property from the parent elementTry it

How to Use background-attachment

Add an image first, then choose what that image is locked to when something scrolls.

  1. Set a background-image (a file or a gradient). Attachment has nothing to move without an image.
  2. Give the element a size. For local or to compare scroll inside a box, also set overflow: auto and enough content to scroll.
  3. Set background-attachment on that element. It is not inherited.
  4. With several images, list one keyword per layer: fixed, scroll.
PieceRequired?Role
background-imageYesThe paint that attachment moves or pins
A visible boxYesHeight or content so you can see the image
Overflow + tall contentFor local / inner scrollLets you scroll inside the element and compare keywords
KeywordYesscroll, fixed, or local

Live Preview

A short scrollable box with a striped image. Change background-attachment, then scroll inside the box.

Scroll the striped box. With scroll, stripes stay. With local, they move with the text.

Attachment

Line 1 — scroll inside this box.

Line 2 — watch the stripes.

Line 3 — scroll keeps them still.

Line 4 — local moves them.

Line 5 — fixed pins to the viewport.

Line 6 — end of the demo text.

background-attachment: scroll

CSS Examples

Each snippet adds only what background-attachment needs: an image, a box, and often overflow so you can scroll. Use Try It Yourself for a live editor.

Example 1 — scroll (Default)

Keep the stripes glued to the box while the text inside scrolls.

CSS
<style>
  div {
    height: 140px;
    overflow: auto;
    padding: 16px;
    color: #fff;
    background-image: repeating-linear-gradient(
      135deg,
      #1d4ed8 0 16px,
      #2563eb 16px 32px,
      #38bdf8 32px 48px
    );
    background-attachment: scroll;
  }
</style>

<div>
  <p>Line 1</p>
  <p>Line 2</p>
  <p>Line 3</p>
  <p>Line 4</p>
  <p>Line 5</p>
</div>
Try It Yourself

How It Works

  1. Paint an image. The repeating gradient is the background-image. Attachment only controls how that image moves.
  2. Make the box scroll. height plus overflow: auto and several lines create an inner scrollbar.
  3. Lock the image to the element. scroll sticks the stripes to the box border. The text moves; the stripes do not.
  4. This is the default. Omitting the property gives the same result as background-attachment: scroll.

Example 2 — local

Move the stripes with the text when you scroll inside the box.

CSS
<style>
  div {
    height: 140px;
    overflow: auto;
    padding: 16px;
    color: #fff;
    background-image: repeating-linear-gradient(
      135deg,
      #1d4ed8 0 16px,
      #2563eb 16px 32px,
      #38bdf8 32px 48px
    );
    background-attachment: local;
  }
</style>

<div>
  <p>Line 1</p>
  <p>Line 2</p>
  <p>Line 3</p>
  <p>Line 4</p>
  <p>Line 5</p>
</div>
Try It Yourself

How It Works

  1. Same image and overflow. Only the attachment keyword changes from Example 1.
  2. Lock the image to the content. local treats the paint as part of the scrollable content area.
  3. Scroll the box. The stripes travel with the lines. That is the visible difference from scroll.
  4. Without overflow, it looks like scroll. If the box cannot scroll inside, local and scroll look the same.

Example 3 — fixed

Pin the stripes to the viewport so the box slides over a still background.

CSS
<style>
  body {
    margin: 0;
  }
  .panel {
    min-height: 180px;
    padding: 16px;
    color: #fff;
    background-image: repeating-linear-gradient(
      135deg,
      #1d4ed8 0 16px,
      #2563eb 16px 32px,
      #38bdf8 32px 48px
    );
    background-attachment: fixed;
  }
  .spacer {
    height: 60vh;
    padding: 16px;
  }
</style>

<div class="panel">fixed — scroll the page</div>
<div class="spacer">Keep scrolling. The stripes stay locked to the window.</div>
Try It Yourself

How It Works

  1. Pin to the viewport. fixed locks the image to the browser window, not to the element.
  2. Scroll the page. The panel moves. The stripes stay still, so the panel slides across them.
  3. Origin is ignored. With fixed, background-origin does not change where the image is positioned.
  4. Mobile caveat. On many iOS Safari versions, fixed still scrolls. Open Try It Yourself on a desktop browser to see the classic effect.

Edge Cases & Pitfalls

If the image never seems to change when you scroll, one of these is usually why.

ios

fixed is unreliable on iOS Safari

Many iPhone and iPad browsers treat fixed like scroll by design. Prefer a position: fixed backdrop element if you need a pinned image on mobile.

overflow

local needs a scrolling box

Without overflow and tall content, local and scroll look the same. The difference appears only when the element itself scrolls.

image

No image, no effect

Attachment only moves a background-image. A solid background-color alone does not show scroll vs fixed vs local.

origin

fixed ignores background-origin

When the image is pinned to the viewport, background-origin is ignored for that layer. Position is relative to the viewport instead.

a11y

Background images are decoration

Screen readers do not announce background images. Put meaning in an img with alt, and keep text contrast strong over moving paint.

Browser Support

background-attachment is a browser painting property. Node.js, Deno, and Bun do not apply it. scroll and fixed are older than local. fixed remains a known weak spot on iOS Safari.

✓ Baseline · Widely available since 2015

background-attachment

scroll: every current browser. fixed: desktop browsers yes; iOS Safari often acts like scroll. local: Chrome 4+, Firefox 25+, Safari 5+ (full again from 15.4), Edge 12+, Internet Explorer 9+.

95% Desktop support
Google Chrome 1+ scroll/fixed · 4+ local
Full support
Mozilla Firefox 1+ scroll/fixed · 25+ local
Full support
Apple Safari 1+ scroll/fixed · 5+ local
Full support
Microsoft Edge 12+
Full support
Opera 15+ local · fixed on desktop
Full support
Samsung Internet 4+
Full support
Internet Explorer 4+ scroll/fixed · 9+ local
Partial
background-attachment Widely supported

Bottom line: Safe for scroll and local in current browsers. Treat fixed as a progressive enhancement: it fails open as scroll on many iPhones. Internet Explorer 8 and older lack local.

Key Takeaways

  • Role: choose whether the background image locks to the element, the viewport, or the element's content.
  • Keywords: scroll (default), fixed (viewport), local (inner content).
  • Layers: one keyword per image, in the same order as background-image.
  • Mobile: do not rely on fixed alone on iOS Safari.

One line: background-attachment answers “what does this image stay stuck to when something scrolls?”

Frequently Asked Questions

It chooses what a background image is fixed to when scrolling happens: the element (scroll), the viewport (fixed), or the element's own overflowing content (local).
scroll. The image is stuck to the element. When you scroll the page, the image moves with that element. When you scroll inside the element, the image stays put against the element's border.
Both move with the page when the element moves. Inside a scrollable box, scroll keeps the image glued to the box edge, while local moves the image with the text as you scroll that box.
It pins the image to the viewport. As the page scrolls, the element moves over a background that stays in place. On that value, background-origin is ignored.
Often no. On many iOS Safari versions, fixed behaves like scroll by design. Test on a real device, and prefer a position: fixed layer if you need a reliable pinned backdrop on mobile.
scroll and fixed are Baseline and very old. local needs Chrome 4, Firefox 25, Safari 5, Edge 12, or Internet Explorer 9. fixed is unreliable on iOS Safari. This is a browser painting property. Node.js, Deno, and Bun do not apply it.

Next: background-blend-mode

Learn how stacked background layers mix with each other and with the backdrop.

background-blend-mode tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful