CSS background-attachment Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Backgrounds & Layout

What You’ll Learn

The background-attachment property controls how a background image moves when the page or an element scrolls. It is especially useful for hero sections, parallax-style layouts, and scrollable panels.

01

Scroll Behavior

Control background movement.

02

Syntax

scroll, fixed, or local.

03

Fixed Hero

Viewport-locked backgrounds.

04

scroll Default

Normal page scrolling.

05

local Boxes

Inner overflow scrolling.

06

Background Shorthand

Part of background CSS.

Definition and Usage

The background-attachment CSS property sets whether a background image scrolls with its element, stays fixed relative to the viewport, or scrolls with the element’s own overflow content.

This property is commonly used with background-image to create fixed hero banners, layered page designs, and scrollable note areas where the background should move with inner text.

💡
Beginner Tip

If you do nothing, backgrounds use scroll by default. Use fixed only when you intentionally want a viewport-locked background effect.

📝 Syntax

Apply background-attachment to any element that has a background image or gradient:

syntax.css
selector {
  background-attachment: scroll | fixed | local;
}

Basic Example

background-attachment.css
.hero {
  background-image: url("hero.jpg");
  background-size: cover;
  background-attachment: fixed;
}

Syntax Rules

  • The property accepts the keywords scroll, fixed, and local.
  • The initial value is scroll.
  • It applies to background layers set with background-image or gradients.
  • fixed is relative to the viewport, not the element itself.
  • local is most noticeable inside elements with overflow: auto or overflow: scroll.

⚡ Quick Reference

QuestionAnswer
Initial valuescroll
Applies toBackground images and gradients
InheritedNo
AnimatableNo
Common useHero banners, parallax sections, scrollable panels

Default Value

The initial value of background-attachment is scroll. That means the background image moves with the element’s border box as the page scrolls, which is the normal behavior most layouts expect.

💎 Property Values

There are three keyword values for background-attachment.

ValueExampleMeaning
scrollbackground-attachment: scroll;The background scrolls with the element on the page
fixedbackground-attachment: fixed;The background stays fixed relative to the viewport
localbackground-attachment: local;The background scrolls with the element’s inner overflow content
scroll

The default behavior. The background moves normally with the element when the page scrolls.

Stripes move with the box when you scroll the page.

Best for standard sections and full-page backgrounds.

fixed

Keeps the background anchored to the viewport for hero and parallax-style effects.

Stripes stay pinned to the screen while the box moves.

Popular for landing-page hero banners.

local

The background scrolls with content inside a box that has its own scrollbar.

Useful for notes, chat windows, and inner panels.

See the Difference: scroll vs fixed

Scroll this page slowly. The left panel keeps its stripes aligned with the box. The right panel keeps stripes fixed to the viewport, so the pattern appears to slide inside the box.

↓ Scroll the page to compare both panels

scroll
fixed

scroll: background moves with the element. fixed: background stays locked to the browser window.

When to Use Each Value

  • scroll — Default page sections, cards, and normal background images.
  • fixed — Hero headers, full-width banners, and parallax-style visual effects.
  • local — Scrollable containers such as note panels, code blocks, or chat areas.

scroll vs fixed vs local

ValueMoves withBest for
scrollThe element on the pageStandard layouts and section backgrounds
fixedThe viewportHero images and parallax-style designs
localThe element’s inner scrollable contentOverflow boxes and nested scroll areas

👀 Live Preview

Use the side-by-side panels above in the Property Values section, or try this full-width fixed hero while scrolling the page:

Fixed Background Hero

The gradient stays viewport-fixed while the rest of the page scrolls.

Compare this hero with the scroll vs fixed panels to see how attachment changes background motion.

Examples Gallery

Try background-attachment with all three values, a fixed hero, a local scroll box, and default scroll sections.

📚 Basic Values

Compare scroll, fixed, and local to understand how each one behaves.

Example 1 — scroll, fixed, and local

Demonstrate all three keyword values in one page.

attachment-compare.css
.scroll { background-attachment: scroll; }
.fixed { background-attachment: fixed; }
.local { background-attachment: local; }
Try It Yourself

How It Works

Each box uses the same kind of background image but a different attachment rule, which changes how the background moves during scrolling.

Example 2 — Fixed Hero Background

Create a viewport-fixed hero section that feels layered as content scrolls over it.

fixed-hero.css
.hero {
  background-image: url("hero.jpg");
  background-size: cover;
  background-attachment: fixed;
}
Try It Yourself

How It Works

With fixed, the hero background stays in place while later page content scrolls above it.

🎨 Scroll Patterns

Use local and default scroll for nested and standard layout behavior.

Example 3 — Local Background in a Scroll Box

Make the background move with text inside an overflow container.

local-scroll.css
.notes {
  overflow: auto;
  background-image: linear-gradient(180deg, #dbeafe, #ede9fe);
  background-attachment: local;
}
Try It Yourself

How It Works

local is most useful when the element itself scrolls internally, not just when the whole page scrolls.

Example 4 — Default scroll Sections

Use the default behavior for normal full-page section backgrounds.

scroll-sections.css
.section {
  background-image: linear-gradient(135deg, #0d9488, #2563eb);
  background-attachment: scroll;
  background-size: cover;
}
Try It Yourself

How It Works

Because scroll is the default, you often do not need to write it unless you are overriding another value.

🧠 How background-attachment Works

1

You add a background image

Set a photo, pattern, or gradient with background-image.

Background layer
2

You choose an attachment value

Pick scroll, fixed, or local.

Attachment rule
3

The browser decides what moves

The background either follows the page, stays in the viewport, or scrolls inside the element.

Scroll behavior
=

Controlled background motion

You get the scroll effect you want without changing the image itself.

Universal Browser Support

background-attachment is widely supported, but fixed can behave differently on some mobile browsers.

Baseline · Background properties

Reliable support in modern browsers

Chrome, Firefox, Safari, Edge, and Opera all support scroll, fixed, and local.

96% Modern browser support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox1+ · Desktop & Mobile
Full support
Apple Safari1+ · macOS & iOS
Full support
Microsoft Edge12+ · Modern versions
Full support
Opera3.5+ · Modern versions
Full support

Mobile caveat

Some mobile browsers treat background-attachment: fixed like scroll for performance reasons.

📱
iOS Safari Fixed backgrounds may not behave as expected · Test on device
Partial
background-attachment property 96% supported

Bottom line: Use background-attachment confidently, but test fixed layouts on mobile and provide a graceful fallback.

Conclusion

The background-attachment property gives you control over how background images move during scrolling. Use scroll for normal layouts, fixed for hero and parallax-style designs, and local for inner scroll containers.

Understanding these three values helps you choose the right background behavior without extra JavaScript or complicated workarounds.

💡 Best Practices

✅ Do

  • Use scroll for most standard page sections
  • Pair fixed with background-size: cover on hero banners
  • Use local in overflow containers like notes and chat panels
  • Test fixed backgrounds on mobile devices
  • Keep text readable over background images

❌ Don’t

  • Assume fixed will look identical on every mobile browser
  • Use fixed everywhere without a performance reason
  • Forget that scroll is already the default
  • Expect local to matter without inner scrolling content
  • Rely on fixed backgrounds for essential readability alone

Key Takeaways

Knowledge Unlocked

Five things to remember about background-attachment

Use these points when choosing background scroll behavior.

5
Core concepts
📝02

Three Values

scroll, fixed, local.

Values
🏠03

Fixed Hero

Viewport-locked backgrounds.

Use case
04

scroll Default

Normal page behavior.

Default
📱05

Test Mobile

Fixed may differ on iOS.

Caution

❓ Frequently Asked Questions

The background-attachment property controls whether a background image scrolls with the element, stays fixed in the viewport, or scrolls with the element's own overflow content.
The initial value is scroll, which means the background image moves with the element as the page scrolls.
scroll moves the background with the element. fixed keeps the background anchored to the viewport, which can create a parallax-style effect.
Use local when an element has its own scrollable content and you want the background to move with that inner content rather than staying fixed to the element box.
Support varies on mobile browsers, especially iOS Safari, where fixed backgrounds may behave like scroll. Always test on real devices and provide a fallback design.

Practice in the Live Editor

Open the HTML editor, try background-attachment: fixed, and compare it with scroll and local.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful