📚 Encyclopedia

Web Development · updated 2026-10-13

Fixed page backgrounds on mobile (background-attachment vs position fixed)

cssmobileperformanceiosandroidcompositing

Never use background-attachment: fixed for a full-page image. Use a position: fixed pseudo-element instead. The two look identical on a desktop and behave completely differently on phones.

Why

The pattern

body::before {
  content: ""; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background: url(...) center / cover no-repeat;
}
html { background-color: #...; }          /* fallback color lives here */
body { background-color: transparent; }   /* required */

Traps that follow immediately

  1. Paint order. A negative z-index child paints before any in-flow block background, so an opaque body background covers the layer entirely and the picture vanishes to flat color. Put the fallback color on html and make body transparent.
  2. Per-layer longhands. background-size, background-repeat and background-position take one value per layer. A tiled scanline gradient over a cover photo needs background-size: auto, cover, cover and background-repeat: repeat, no-repeat, no-repeat, or the scanlines stretch into fat stripes.
  3. Repeated cards: prefer box-shadow to filter: drop-shadow. Each filter is its own compositing layer, and over an opaque image the two look identical.

Example

The CRT theme on videos.jhampton.io looked broken on Justin's Android phone while the look.php sketch of the same design was perfect. Nothing reproduced in headless Chrome at any viewport. The bug was found by diffing the two pages rather than reproducing: the sketch used a position: fixed div, the theme used background-attachment: fixed on <body>. That was the whole difference. Diffing a working page against a broken one is often faster than reproduction.

See also

Linked from

Sources: justinahampton memory (web-fixed-background-mobile, videos-crt-theme), 2026-08-15