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.
.panel { background-attachment: local; } .hero { background-attachment: fixed; }
Try it
Syntax
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
fixedis unreliable on touch devices. Many mobile browsers ignore it or treat it asscroll, 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-originhas no effect, and the positioning area is the viewport. - Parallax.
fixedis sometimes used for a cheap parallax look. Where it matters, do the effect withposition: sticky, transforms or scroll-driven animations instead, and respectprefers-reduced-motion. localfor scrolling panels. It is handy for the "shadows at the scroll edges" technique, where one layer islocaland another isscroll, 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.
Related
background-originโ the positioning areabackground-positionbackgroundoverflowโ scrolling boxes- Lesson: Backgrounds