CSS background-attachment Property

The background-attachment property controls how a background image behaves when the page or an element's contents are scrolled.

It determines whether a background scrolls with its element, remains fixed relative to the viewport, or moves with the contents of a scrollable container. The property is commonly used for fixed background effects, decorative sections, and scrollable panels.

background-attachment Syntax

selector {
  background-attachment: scroll;
}

The property accepts three keywords: scroll, fixed, and local.

.scroll-background {
  background-attachment: scroll;
}
.fixed-background {
  background-attachment: fixed;
}
.local-background {
  background-attachment: local;
}

Multiple values can be specified as a comma-separated list when an element has multiple background images.

.layered-background {
  background-attachment: fixed, scroll;
}

The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.

Property Values

Value Description
scroll The background is fixed relative to the element's border box. It moves with the element when the page scrolls but does not move with the element's internally scrolling content. This is the initial value.
fixed The background is fixed relative to the viewport in typical screen layouts, rather than moving with the element when the page scrolls.
local The background scrolls with the element's contents, making it useful for scrollable containers.

The property applies to all elements, is not inherited, and has an initial value of scroll. Its computed value is a list of attachment keywords, and its animation type is discrete.

Each value controls the attachment behavior of a background image layer. The property does not directly change the background color, image, size, or repetition.

How background-attachment Works

The background-attachment property determines the reference used when painting a background image during scrolling.

Its behavior depends on whether the page itself scrolls or whether an element contains its own scrollable content.

Page Scrolling

When a page scrolls, an element using scroll carries its background with it. An element using fixed generally displays a background anchored to the viewport.

.page-section {
  background-image: url("background.jpg");
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

The background remains anchored to the viewport while the section moves across it. The image is still painted only within the section's visible background area.

Internal Scrolling

When a container uses overflow: auto or overflow: scroll, its contents can move independently of the container itself.

In that situation, scroll keeps the background stationary relative to the container, while local moves it with the scrollable contents.

The difference between these values is easiest to observe in a container with more content than can fit inside its specified height.

Using scroll

The scroll value is the default background attachment behavior.

.section {
  background-image: url("pattern.png");
  background-repeat: repeat;
  background-attachment: scroll;
}

When the page scrolls, the element and its background move together.

However, if the element has its own scrolling area, the background remains fixed relative to the element's border box rather than moving with its internal content.

Scrollable Container

.scroll-panel {
  height: 180px;
  overflow-y: auto;
  background-image: linear-gradient(135deg, #e8f2fb, #c4e1f5);
  background-attachment: scroll;
}

As the panel's text scrolls, the background stays in the same position within the panel.

This value is suitable for ordinary page sections and containers that do not require special background scrolling effects.

Using fixed

The fixed value anchors the background image to the viewport in typical screen layouts.

.hero-section {
  min-height: 400px;
  background-image: url("landscape.jpg");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

As the page scrolls, the element moves while the background remains anchored to the viewport. This can create a visual effect in which the background appears to move at a different rate from the foreground content.

Using a Gradient

.fixed-gradient {
  min-height: 350px;
  background: linear-gradient(135deg, #1769aa, #69b7e8) center / cover no-repeat fixed;
}

The same attachment behavior can be used with CSS-generated gradients.

Background Size and Position

For fixed backgrounds, the background positioning area is generally the viewport. This means background-size: cover may size the image to cover the viewport rather than the individual element.

Only the portion of the fixed background that falls within the element's painting area is displayed.

Mobile Considerations

Fixed background behavior can differ on mobile devices because browsers may optimize scrolling or handle viewport dimensions differently.

Some mobile browsers do not render fixed backgrounds consistently. A scrollable background is often a more dependable fallback.

Use fixed backgrounds selectively, especially when large images or multiple sections are involved.

Using local

The local value makes a background image scroll with the contents of an element.

It is most useful for containers with their own scrolling area.

<div class="local-panel">
  <p>First paragraph of content.</p>
  <p>Second paragraph of content.</p>
  <p>Third paragraph of content.</p>
  <p>Fourth paragraph of content.</p>
</div>
.local-panel {
  height: 150px;
  overflow-y: auto;
  padding: 15px;
  background-image: repeating-linear-gradient(0deg, #e8f2fb 0, #e8f2fb 30px, #c4e1f5 30px, #c4e1f5 60px);
  background-attachment: local;
}

As the user scrolls through the panel, the striped background moves with its contents.

Changing the attachment value to scroll keeps the background stationary relative to the panel while the text moves over it.

Using local with Background Images

.content-panel {
  height: 250px;
  overflow-y: auto;
  background: url("texture.png") repeat local;
}

The background image scrolls with the contents of the panel rather than remaining anchored to the panel's visible box.

If the element has no internal scrolling, the difference between local and scroll may not be noticeable.

Comparing Attachment Values

The three attachment values differ primarily in what the background is anchored to.

Value Reference Behavior
scroll Element's border box Moves with the element during page scrolling but stays stationary during internal content scrolling.
fixed Viewport Remains anchored to the viewport while the element scrolls across it.
local Element's scrollable contents Moves with the contents when the element scrolls internally.

For ordinary page backgrounds, scroll is generally sufficient. Use fixed for viewport-anchored decorative effects and local for backgrounds that should follow internally scrolling content.

Remember that fixed can have additional limitations in paged media and mobile browsers.

Multiple Background Layers

When an element has multiple background images, each layer can use a different attachment value.

.layered-panel {
  background-image: linear-gradient(rgb(0 0 0 / 25%), rgb(0 0 0 / 25%)), url("landscape.jpg");
  background-position: center, center;
  background-size: cover, cover;
  background-repeat: no-repeat, no-repeat;
  background-attachment: scroll, fixed;
}

The first attachment value applies to the gradient, while the second applies to the image.

Background layers are painted from front to back, so the gradient appears over the photograph.

Matching Attachment Values to Layers

.multiple-patterns {
  background-image: url("pattern-one.png"), url("pattern-two.png");
  background-attachment: local, scroll;
}

The first image moves with internally scrolling content. The second remains stationary relative to the element.

If fewer attachment values are supplied than background image layers, the browser repeats the list of attachment values as necessary. Extra attachment values are not used when there are fewer image layers.

Each background layer can also have its own position, size, repetition, and other background settings.

Using the background Shorthand

The background-attachment property can be included in the background shorthand declaration.

.banner {
  background: url("landscape.jpg") center / cover no-repeat fixed;
}

This sets the background image, position, size, repetition, and attachment in one declaration.

Equivalent Longhand Declarations

.banner {
  background-image: url("landscape.jpg");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

Both approaches produce the same background settings.

Shorthand Reset Behavior

The background shorthand resets omitted background longhand properties to their initial values.

.panel {
  background-attachment: fixed;
  background: url("pattern.png") repeat;
}

The final shorthand declaration resets background-attachment to scroll, because no attachment value was included.

To preserve a previously configured attachment value, use individual longhand properties or place the attachment declaration after the shorthand.

.panel {
  background: url("pattern.png") repeat;
  background-attachment: fixed;
}

background-attachment Example

The following example compares scroll and local inside two scrollable containers.

Both panels use the same striped gradient and contain enough text to scroll. The first background remains stationary within its panel, while the second moves with its contents.

<div class="attachment-demo">
  <div class="attachment-item">
    <strong>scroll</strong>
    <div class="attachment-panel scroll-panel">
      <p>First paragraph of content.</p>
      <p>Second paragraph of content.</p>
      <p>Third paragraph of content.</p>
      <p>Fourth paragraph of content.</p>
      <p>Fifth paragraph of content.</p>
    </div>
  </div>
  <div class="attachment-item">
    <strong>local</strong>
    <div class="attachment-panel local-panel">
      <p>First paragraph of content.</p>
      <p>Second paragraph of content.</p>
      <p>Third paragraph of content.</p>
      <p>Fourth paragraph of content.</p>
      <p>Fifth paragraph of content.</p>
    </div>
  </div>
</div>
.attachment-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 20px;
}
.attachment-item strong {
  display: block;
  margin-bottom: 8px;
  text-align: center;
}
.attachment-panel {
  height: 180px;
  overflow-y: auto;
  padding: 15px;
  box-sizing: border-box;
  border: 2px solid #1769aa;
  border-radius: 6px;
  background-image: repeating-linear-gradient(0deg, #e8f2fb 0, #e8f2fb 35px, #b7d9f1 35px, #b7d9f1 70px);
  background-repeat: repeat;
}
.attachment-panel p {
  margin: 0 0 25px;
  color: #173b5e;
}
.attachment-panel.scroll-panel {
  background-attachment: scroll;
}
.attachment-panel.local-panel {
  background-attachment: local;
}

Scroll inside each panel to compare the two behaviors.

Browser Result
scroll

First paragraph of content.

Second paragraph of content.

Third paragraph of content.

Fourth paragraph of content.

Fifth paragraph of content.

local

First paragraph of content.

Second paragraph of content.

Third paragraph of content.

Fourth paragraph of content.

Fifth paragraph of content.

In the scroll panel, the text moves over a stationary striped background. In the local panel, the stripes move with the scrolling text.

Use the mouse wheel, touch scrolling, or keyboard navigation to explore the difference. The panels can receive keyboard focus to support scrolling without a mouse.

The demonstration uses CSS gradients rather than external image files, so the background patterns are generated directly by the browser.

Important Considerations

Keep the following points in mind when using background-attachment:

  • Default value: The initial value is scroll.
  • Page scrolling: The scroll value moves the background with its element when the page scrolls.
  • Fixed backgrounds: The fixed value generally anchors the background to the viewport.
  • Internal scrolling: The local value makes the background scroll with an element's contents.
  • Scrollable containers: The difference between scroll and local is most noticeable when an element has its own scrolling area.
  • Multiple layers: A comma-separated list can assign different attachment values to different background images.
  • Shorthand behavior: The background shorthand resets attachment to scroll when no attachment value is supplied.
  • Fixed positioning: A fixed background's positioning area is generally the viewport, which can affect background-size and background-position.
  • Mobile compatibility: Fixed backgrounds may behave differently or be unsupported in some mobile browsers.
  • Performance: Large fixed background images can increase rendering costs during scrolling.
  • Accessibility: Keep text readable over decorative backgrounds and ensure scrollable containers remain usable with keyboard and touch input.

Choose scroll for normal background behavior, fixed for viewport-anchored effects, and local when the background should follow the contents of a scrollable element.

Browser Support

The background-attachment property has Baseline Widely available status for its standard values and is supported across modern desktop browsers, including Chrome, Edge, Firefox, and Safari.

Support for background-attachment: fixed can be inconsistent on mobile devices, particularly in browsers that optimize scrolling or use dynamic viewport behavior.

For a reliable fallback, use the default scrolling background and apply the fixed effect only where appropriate.

.hero {
  background: url("landscape.jpg") center / cover no-repeat scroll;
}
@media (min-width: 768px) and (hover: hover) {
  .hero {
    background-attachment: fixed;
  }
}

This example limits the fixed background to wider devices with hover-capable input. These media conditions do not guarantee support for fixed backgrounds, but they can help avoid applying the effect to some mobile layouts.

Visit Can I Use? to check browser compatibility and mobile limitations.

Specifications

The background-attachment property is defined in the CSS Backgrounds and Borders Module Level 3 specification.

CSS Backgrounds and Borders Module Level 3