日本語

CSS background-attachment: fixed Not Working? Causes and Fixes

​​CSS​HTML,Playwright

To keep a background image from scrolling, add background-attachment: fixed to the element that has the background. The background stays fixed to the screen (the viewport), and only the element moves over it. Use this when you want a hero image at the top of a page, or a background image covering the whole page, to stay in place while you scroll. However, it stops working if the element or one of its ancestors has a transform, and it is reported to have no effect on Safari on iPhone even when the value is set.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. The screen size was 400×600px, and I used a background image with a different color on each row. I read the color at a fixed point from screenshots, then compared which row of the image was visible before and after scrolling. I did not test Firefox, Safari, or real smartphones. The Safari support information is taken from the content of browser-compat-data on MDN. For the section "Ways to write it without background-attachment," I also tested only in Chromium. I did not test whether it works in Safari on iPhone.

Writing fixed makes the background stick to the screen

The syntax is one line. Adding fixed to the background shorthand works the same way.

css
.hero {
  height: 800px;
  background-image: url(hero.jpg);
  background-attachment: fixed;
}

/* When written as a shorthand */
.hero {
  background: url(hero.jpg) no-repeat fixed;
}

I placed an 800px-high element 100px from the top of the page. The table compares which row of the background image was visible at the point 300px from the top of the screen, before and after scrolling.

background-attachment Before scrolling After scrolling 200px
scroll (initial value) Row 200 Row 400
fixed Row 300 Row 300
local Row 200 Row 400

With the initial value scroll, the background moves together with the element. After scrolling 200px, the row visible at the same point was 200 rows further along. With fixed, row 300 stayed visible after scrolling. The shorthand background: url(...) no-repeat fixed gave the same result.

Two rows compared. The top row uses background-attachment: scroll. Before scrolling, the band labeled "Background 0px" sits just below the band labeled "Top of the page." After scrolling 200px, "Background 200px" is in the same position. The bottom row uses background-attachment: fixed. Before and after scrolling 200px, bands such as "Background 100px" and "Background 200px" stay at the same height on the screen. The labels are in Japanese.
Screen before and after scrolling 200px, using scroll and fixed. With fixed, the background bands stay in the same position on the screen. The labels are in Japanese.

The CSS Backgrounds specification states that the background is fixed relative to the viewport. The MDN page for background-attachment also says that the background image does not move with the element when the element scrolls.

The background position is based on the top-left of the screen, not the element

With fixed, the screen is also the reference for positioning the background. In the example above, the row visible at the top edge of the element (100px from the top of the screen) was row 0 with scroll, and row 100 with fixed. With fixed, the image is drawn from the top-left corner of the screen, and the element only shows a part of it.

The background-origin section of the specification says that when the value is fixed, background-origin has no effect, and the positioning area becomes the initial containing block (a box the size of the screen). In the test environment, I added padding: 50px and background-origin: content-box. With scroll, row 0 of the image moved 50px down. With fixed, row 100 stayed visible at the top edge of the element, so nothing changed.

The background is visible only within the area where the element is. After scrolling 1000px and moving the element out of the screen, the point 300px from the top of the screen turned white, and the background was no longer visible. The specification also states that even when fixed, the background is visible only within the area where the element's background is painted.

If the element or an ancestor has a transform, fixed does not work

If a background set to fixed moves with the element anyway, first suspect transform. When I added a transform: translateX(0) that did not move anything to the parent of the fixed element, the background moved 200 rows after scrolling 200px, which is the same as scroll.

The CSS Transforms specification says that for an element with a transform and its descendants, fixed is treated as scroll. In this case, the value read by getComputedStyle() stays fixed. In the test environment, the background moved while the value still returned fixed. So if you only check the value, it is hard to notice the cause.

These are the results in Chromium of the properties I added to the parent element.

Added to the parent Background with fixed
transform: translateX(0) Moved
translate: 0, scale: 1, rotate: 0deg Moved
will-change: transform Moved
transform: none Stayed still
filter, backdrop-filter, perspective Stayed still
contain: paint, clip-path, overflow: hidden Stayed still

The individual translate, scale, and rotate properties, and will-change: transform, also stopped fixed from working, the same as transform. Check the ancestors of the element with the background, up to <html>, in the developer tools, and confirm that none of these properties is set.

When I added transform to the element itself, the background also moved.

For a page-wide background, writing it on body avoids the effect of transform

For a background that covers the whole page, you can write it on <body>. When <html> has no background, the background of <body> is drawn over the whole canvas. The CSS Transforms specification says that a background drawn on the canvas this way is not affected by transform.

css
body {
  background: url(paper.png) fixed;
}

In the test environment, the background stayed still even when I added transform: translateX(0) to <body>. However, when I set background: white on <html>, the background of <body> was no longer drawn on the canvas. In this state, the background moved 200 rows because of the transform on <body>.

Inside a scrolling box, the reference is still the screen

Even with a layout where content scrolls inside a box with overflow: auto, a fixed background stays fixed to the screen. When I scrolled inside a 600px-high box, the fixed background on an element inside the box did not move.

When I added the background to the scrolling box itself, the results depended on the value, as shown below.

background-attachment on the box When the content inside the box is scrolled 200px
scroll The background does not move
local The background moves with the content
fixed The background does not move (the reference is the screen)

A note in the specification also says that a fixed background on the box itself does not move with the box. However, for the box's own background, the background does not move when the content is scrolled, even with the initial value scroll. If you want the background to move with the content, use local.

On Safari on iPhone, fixed does not work even when written

According to the browser-compat-data on MDN, on iOS Safari fixed is accepted as a value but has no effect (WebKit bug report 275247). Safari on desktop has supported it since 3.1, but from 14 to 15.3 it did not work, just as on iPhone, and it has worked again since 15.4. Chrome has supported it since 1, and Firefox since 2.

Ways to write it without background-attachment

You can also split the background into a position: fixed layer and use clip-path to cut off the area outside the element.

css
.hero {
  position: relative;
  clip-path: inset(0);
}

.hero::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background: url(hero.jpg) no-repeat;
}

The pseudo-element ::before is fixed to cover the whole screen. The clip-path: inset(0) on .hero hides the parts that extend outside .hero. In the test environment, even after scrolling 200px, row 300 stayed visible at the point 300px from the top of the screen, which matched the result of background-attachment: fixed. When .hero moved out of the screen, the background was no longer visible.

You cannot remove clip-path. Without it, after scrolling 1000px, row 300 of the background remained visible at the point 300px from the top of the screen, even though .hero had moved out of the screen. The fixed layer stays visible behind the whole page.

This method also does not stop moving when the parent element has a transform. When I added transform: translateX(0) to the parent, the background moved 200 rows after scrolling 200px. This is because an element with position: fixed is positioned relative to its nearest ancestor that has a transform (CSS Transforms specification).

​Posted by 86