Web/CSS/background-attachment

From Get docs


The background-attachment CSS property sets whether a background image's position is fixed within the viewport, or scrolls with its containing block.


Syntax

/* Keyword values */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;

/* Global values */
background-attachment: inherit;
background-attachment: initial;
background-attachment: unset;

The background-attachment property is specified as one of the keyword values from the list below.

Values

fixed
The background is fixed relative to the viewport. Even if an element has a scrolling mechanism, the background doesn't move with the element. (This is not compatible with background-clip: text.)
local
The background is fixed relative to the element's contents. If the element has a scrolling mechanism, the background scrolls with the element's contents, and the background painting area and background positioning area are relative to the scrollable area of the element rather than to the border framing them.
scroll
The background is fixed relative to the element itself and does not scroll with its contents. (It is effectively attached to the element's border.)

Formal definition

Initial value scroll
Applies to all elements. It also applies to ::first-letter and ::first-line.
Inherited no
Computed value as specified
Animation type discrete

Formal syntax

<attachment>#where <attachment> = scroll | fixed | local

Examples

Simple example

HTML

<p>
  There were doors all round the hall, but they were all locked; and when
  Alice had been all the way down one side and up the other, trying every
  door, she walked sadly down the middle, wondering how she was ever to
  get out again.
</p>

CSS

p {
  background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif");
  background-attachment: fixed;
}

Result

Multiple background images

This property supports multiple background images. You can specify a different <attachment> for each background, separated by commas. Each image is matched with the corresponding <attachment> type, from first specified to last.

HTML

<p>
  There were doors all round the hall, but they were all locked; and when
  Alice had been all the way down one side and up the other, trying every
  door, she walked sadly down the middle, wondering how she was ever to
  get out again.

  Suddenly she came upon a little three-legged table, all made of solid
  glass; there was nothing on it except a tiny golden key, and Alice's
  first thought was that it might belong to one of the doors of the hall;
  but, alas! either the locks were too large, or the key was too small,
  but at any rate it would not open any of them. However, on the second
  time round, she came upon a low curtain she had not noticed before, and
  behind it was a little door about fifteen inches high: she tried the
  little golden key in the lock, and to her great delight it fitted!
</p>

CSS

p {
  background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"),
      url("https://mdn.mozillademos.org/files/12059/startransparent.gif");
  background-attachment: fixed, scroll;
  background-repeat: no-repeat, repeat-y;
}

Result

Specifications

Specification Status Comment
CSS Backgrounds and Borders Module Level 3The definition of 'background-attachment' in that specification. Candidate Recommendation The shorthand property has been extended to support multiple backgrounds and the local value.
CSS Level 2 (Revision 1)The definition of 'background-attachment' in that specification. Recommendation No significant change.
CSS Level 1The definition of 'background-attachment' in that specification. Recommendation No significant change.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
background-attachment Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 1

IE

Full support 4

Opera

Full support 3.5

Safari

Full support 1

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 10.1

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

local Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 25

IE

Full support 9

Opera

Full support 10.5

Safari

Full support 5

WebView Android

Full support 37

Chrome Android

Full support 18

Firefox Android

Full support 25

Opera Android

Full support 14

Safari iOS

Full support 4.2

Samsung Internet Android

Full support 1.0

Multiple backgrounds Chrome

Full support 1

Edge

Full support 12

Firefox

Full support 3.6

IE

Full support 9

Opera

Full support 10.5

Safari

Full support 1.3

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 4

Opera Android

Full support 11

Safari iOS

Full support 3.2

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support


See also