background-attachment
CSS
property sets whether a background image's position is fixed within the
viewport
, or scrolls with its containing block.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
/* Keyword values */ background-attachment: scroll; background-attachment: fixed; background-attachment: local; /* Global values */ background-attachment: inherit; background-attachment: initial; background-attachment: unset;
background-attachment
property is specified as one of the keyword values from the list below.
fixed
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.)
| 初始值 |
scroll
|
|---|---|
| 适用于 |
所有元素。它还适用于
::first-letter
and
::first-line
.
|
| 继承 | no |
| 计算值 | 如指定 |
| 动画类型 | discrete |
<attachment>#
<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>
p {
background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif");
background-attachment: fixed;
}
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.
<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>
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;
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Backgrounds and Borders Module Level 3
The definition of 'background-attachment' in that specification. |
候选推荐 |
The shorthand property has been extended to support multiple backgrounds and the
local
值。
|
|
CSS Level 2 (Revision 1)
The definition of 'background-attachment' in that specification. |
推荐 | No significant change. |
|
CSS Level 1
The definition of 'background-attachment' in that specification. |
推荐 | No significant change. |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
background-attachment
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 3.5 | Safari 完整支持 1 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 3.2 | Samsung Internet Android 完整支持 1.0 |
local
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 25 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 5 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 25 | Opera Android 完整支持 14 | Safari iOS 完整支持 4.2 | Samsung Internet Android 完整支持 1.0 |
| Multiple backgrounds | Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 3.6 | IE 完整支持 9 | Opera 完整支持 10.5 | Safari 完整支持 1.3 | WebView Android 完整支持 ≤37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 11 | Safari iOS 完整支持 3.2 | Samsung Internet Android 完整支持 1.0 |
完整支持
background
background-attachment
background-clip
background-color
background-image
background-origin
background-position
background-position-x
background-position-y
background-repeat
background-size
border
border-bottom
border-bottom-color
border-bottom-left-radius
border-bottom-right-radius
border-bottom-style
border-bottom-width
border-color
border-image
border-image-outset
border-image-repeat
border-image-slice
border-image-source
border-image-width
border-left
border-left-color
border-left-style
border-left-width
border-radius
border-right
border-right-color
border-right-style
border-right-width
border-style
border-top
border-top-color
border-top-left-radius
border-top-right-radius
border-top-style
border-top-width
border-width
box-shadow