background-attachment
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
尝试一下
语法
css
/* 关键 属性值 */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;
/* 全局 属性值 */
background-attachment: inherit;
background-attachment: initial;
background-attachment: unset;
取值
标准语法
例子
简单的例子
CSS 样式表
css
p {
background-image: url("starsolid.gif");
background-attachment: fixed;
}
HTML 源码
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>
效果
多背景图支持
此属性支持多张背景图片。你可以用逗号分隔来为每一张背景图片指定不同的<attachment>属性值。
每一张背景图片顺序对应相应的 attachment 属性。
CSS 样式表
css
p {
background-image: url("starsolid.gif"), url("startransparent.gif");
background-attachment: fixed, scroll;
background-repeat: no-repeat, repeat-y;
}
HTML 源码
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>
效果
规范
Specification |
---|
CSS Backgrounds and Borders Module Level 3 # the-background-attachment |
初始值 | scroll |
---|---|
适用元素 | 所有元素. It also applies to ::first-letter and ::first-line . |
是否是继承属性 | 否 |
计算值 | as specified |
动画类型 | 离散值 |
浏览器兼容性
BCD tables only load in the browser