Course contentsShow
← Web Development
Lesson 196 of 2,9823. CSS FundamentalsFree lesson

Background Attachment

Control scrolling behavior with background-attachment using scroll, fixed, and local values.

Background Attachment

What you'll learn: How to control whether a background image scrolls with the page content or stays fixed in place.

The Core Idea

The background-attachment property determines how a background image behaves when you scroll the page. Think of it like choosing between wallpaper that moves with a sliding door versus wallpaper painted directly on the wall behind it.

There are three values you can use:

scroll (default)

The background image scrolls along with the element itself. If the page scrolls, the background moves with it. This is the standard behavior you see most often.

.hero {
  background-image: url('mountain.jpg');
  background-attachment: scroll;
}

fixed

The background image stays fixed relative to the viewport (browser window), not the element. As you scroll the page, the background appears to stay in place while content moves over it—creating a popular "parallax" effect.

.hero {
  background-image: url('mountain.jpg');
  background-attachment: fixed;
}

local

The background scrolls with the element's content. This matters when the element itself has a scrollbar (like an overflowing <div>). The background will scroll along with that internal content.

.scrollable-box {
  background-image: url('pattern.png');
  background-attachment: local;
  overflow: auto;
}

When to Use Each

  • Use scroll for standard backgrounds
  • Use fixed for dramatic parallax effects or stationary hero sections
  • Use local when you have scrollable containers and want the background to move with internal content

Key Takeaway: background-attachment controls whether backgrounds scroll with the page (scroll), stay fixed to the viewport (fixed), or scroll with an element's internal content (local).