CSS / Reference / background-attachment: Viewport, Box, and Internal Scroll Attachment
CSS background-attachment sets the reference for how a background image moves. scroll anchors it to the element’s box, local moves it with the contents inside the box, and fixed anchors it to the viewport as the page scrolls.
What each background-attachment value anchors to
The initial value is scroll, and the property is not inherited. It affects images and gradients drawn by background-image. A solid background color alone makes the values look the same.
| Value | Anchor | Scroll inside the box | Scroll the page |
|---|---|---|---|
scroll |
Element box | The background stays with the box. | The background moves with the box. |
local |
Scrolling contents | The background moves with the contents. | The background moves with the box. |
fixed |
Viewport | Only the contents move. | The box moves while the pattern stays in place on the screen. |
CSS Backgrounds and Borders definition of background-attachment
Compare scrolling a box with scrolling the page
Save the code as background-attachment-example.html in UTF-8 and open it in a browser. The three panels use the same stripes and height. Only background-attachment changes. On narrow screens, the panels stack.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare CSS background scrolling</title>
<style>
body { margin: 16px; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
main { max-width: 56rem; margin-inline: auto; }
.samples { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); gap: 16px; }
.panel {
box-sizing: border-box; height: 180px; overflow: auto;
outline: 1px solid #496275; background-color: #064f6d;
background-image: repeating-linear-gradient(#064f6d 0 48px, #4c7489 48px 96px, #567665 96px 144px);
background-position: left top;
}
.scroll { background-attachment: scroll; }
.local { background-attachment: local; }
.fixed { background-attachment: fixed; }
.panel p { width: fit-content; margin: 0 0 52px; padding: 4px 8px; background: white; color: #172a3a; }
.panel:focus-visible { outline: 3px solid #005fcc; }
body::after { content: ""; display: block; height: 80vh; }
</style>
</head>
<body>
<main>
<h1>Compare CSS background scrolling</h1>
<div class="samples">
<section aria-labelledby="title-scroll">
<h2 id="title-scroll">scroll</h2>
<div class="panel scroll" tabindex="0" role="region" aria-labelledby="title-scroll">
<p>First line</p>
<p>Second line</p>
<p>Third line</p>
<p>Fourth line</p>
<p>Fifth line</p>
</div>
</section>
<section aria-labelledby="title-local">
<h2 id="title-local">local</h2>
<div class="panel local" tabindex="0" role="region" aria-labelledby="title-local">
<p>First line</p>
<p>Second line</p>
<p>Third line</p>
<p>Fourth line</p>
<p>Fifth line</p>
</div>
</section>
<section aria-labelledby="title-fixed">
<h2 id="title-fixed">fixed</h2>
<div class="panel fixed" tabindex="0" role="region" aria-labelledby="title-fixed">
<p>First line</p>
<p>Second line</p>
<p>Third line</p>
<p>Fourth line</p>
<p>Fifth line</p>
</div>
</section>
</div>
</main>
</body>
</html>
- Scroll inside each panel with the mouse wheel, or press Tab to focus it and use the arrow keys. With
local, the stripes move up with the lines. In the other panels only the text moves. - Scroll the page a little from outside the panels. With
scrollandlocal, the stripes move with their boxes. Withfixed, the stripes stay at the same height in the viewport and the moving box reveals a different part of the pattern. - Change only the value on
.localtoscroll. Reload and scroll inside it again. Once you see the background stay with the box, restorelocal.
Position, size, and multiple background layers
With local, the scrollable contents area affects where the background is positioned and painted. With fixed, the positioning area is the viewport and background-origin is ignored. The element’s background painting area still clips the image, so the effect stays within the moving box.
For multiple backgrounds, list attachment values in the same order as background-image. This example uses a gradient on top of photo.jpg from the same folder. The first attachment is for the overlay and the second is for the photo.
.banner {
background-image: linear-gradient(#0007, #0007), url('photo.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll, fixed;
}
Give .banner height through content or min-height and scroll the page. The photo uses viewport-based center and cover. Change the second attachment to scroll to size and position the photo against the box.
When fixed backgrounds behave differently
On a non-root element, a transform other than none on the element or an ancestor can make the browser paint fixed like scroll. Developer tools can still show a computed value of fixed. Disable the transform temporarily to compare scrolling.
CSS Transforms rules for fixed backgrounds
WebKit tracks cases where the fixed background moves with the page in iOS Safari. Test an actual target device if the design relies on this effect. Use scroll as the baseline when readable content and predictable cropping matter most.
WebKit report on fixed backgrounds on iOS
Troubleshoot unexpected background scrolling
- Check that there is an internal scroll range.
overflow: autoneeds content taller than the constrained box. - If the image is missing, check the element height, the image request, and whether a
no-repeatimage falls outside the painted area. - If the computed value returns to
scroll, check for a laterbackgroundshorthand that resets it.
Related background properties and examples. Setting background colors and images · Sizing images with background-size





