Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

background-attachment — background behavior during scrolling

The background-attachment property determines whether a background image is attached to the element’s box, its scrolling content, or the viewport. Its values are scroll, local, and fixed.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

background-attachment: <attachment>#;
<attachment> = scroll | fixed | local

background-attachment controls background images during scrolling. Its initial value is scroll; the property is not inherited.

scroll attaches the background to the element’s box, not its scrolling content. local attaches it to the content; background positioning and painting areas then refer to the scrollable overflow area. fixed normally attaches the background to the viewport without fixing the element itself. The background remains subject to clipping. If a transform is applied to a non-root element or to any of its ancestors, that element’s fixed background is painted as if the value were scroll, but the computed value remains fixed.

For multiple layers, provide comma-separated values, such as background-attachment: local, scroll. The first matches the first background-image; a short list repeats. The example compares scroll and local without external resources.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>background-attachment comparison</title>
<style>
  body { margin: 24px; font: 16px sans-serif; }
  .panel {
    width: 240px;
    height: 120px;
    overflow: auto;
    margin-bottom: 20px;
    border: 2px solid black;
    background-image: repeating-linear-gradient(
      to bottom, #cce5ff 0 20px, #fff 20px 40px
    );
  }
  .scroll { background-attachment: scroll; }
  .local { background-attachment: local; }
  .content { height: 400px; }
</style>
<h1>scroll / local</h1>
<h2>scroll</h2>
<div class="panel scroll"><div class="content">TOP</div></div>
<h2>local</h2>
<div class="panel local"><div class="content">TOP</div></div>
<script>
  for (const panel of document.querySelectorAll('.panel')) {
    panel.scrollTop = 30;
  }
</script>
</html>

Result

Both panels start with scrollTop = 30. In the scroll panel, a blue stripe starts at the inner top edge. In the local panel, a white stripe starts there. Scrolling either panel moves its content; only the local background moves with that content.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources