Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Colors and Backgrounds

The background-attachment property

Reference for CSS background-attachment: scroll, fixed and local, how each behaves in scrolling boxes and the viewport, parallax caveats and live examples.

The background-attachment property decides what a background image is fixed to when something scrolls: the element's own box, the element's scrolling content, or the viewport.

It matters only when scrolling is involved. The default, scroll, pins the background to the element's box, so it does not move when the element's content scrolls inside it. local ties the background to the content so it scrolls along. fixed ties it to the viewport, which is the basis of the classic "fixed background" effect.

CSS
.panel { background-attachment: local; }
.hero  { background-attachment: fixed; }

Try it

scroll versus local inside a scrolling box

Syntax

CSS
background-attachment: [ scroll | fixed | local ]#;

background-attachment: fixed;
background-attachment: scroll, local; /* two layers */
  • Initial value: scroll
  • Applies to: all elements
  • Inherited: no
  • Animatable: no
  • Computed value: as specified

Values

Value Description
scroll The background is fixed relative to the element itself. It does not move when the element's content scrolls, but it moves when the page scrolls the element.
local The background is fixed relative to the element's contents. It scrolls with the content when the element has its own scrollbar.
fixed The background is fixed relative to the viewport. The element acts like a window onto it.

Usage notes

  • fixed is unreliable on touch devices. Many mobile browsers ignore it or treat it as scroll, and it can make scrolling less smooth because the background must repaint. Do not depend on it for anything important, and test on real devices.
  • A fixed background ignores origin. With fixed, background-origin has no effect, and the positioning area is the viewport.
  • Parallax. fixed is sometimes used for a cheap parallax look. Where it matters, do the effect with position: sticky, transforms or scroll-driven animations instead, and respect prefers-reduced-motion.
  • local for scrolling panels. It is handy for the "shadows at the scroll edges" technique, where one layer is local and another is scroll, so shadows appear only when more content lies beyond the edge.
  • Layers. With multiple images you can mix values, one per layer.

Browser support

scroll and fixed are supported everywhere in desktop browsers, with the mobile caveat above. local is supported by current browsers; check a compatibility table for older ones.