--- title: background-attachment slug: Web/CSS/background-attachment tags: - CSS Background - CSS Property - CSS_参考 - Reference translation_of: Web/CSS/background-attachment ---
background-attachment
CSS 属性决定背景图像的位置是在{{glossary("视口")}}内固定,或者随着包含它的区块滚动。
该交互式示例网页的源码存储在GitHub中,如果你想为该网页项目做点贡献的话,请进此链接 https://github.com/mdn/interactive-examples 并给我们一个请求加入的回复。
/* 关键 属性值 */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;
/* 全局 属性值 */
background-attachment: inherit;
background-attachment: initial;
background-attachment: unset;
fixed
local
scroll
p { background-image: url("https://mdn.mozillademos.org/files/12057/starsolid.gif"); background-attachment: fixed; }
<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>
{{EmbedLiveSample("简单的例子")}}
此属性支持多张背景图片。你可以用逗号分隔来为每一张背景图片指定不同的<attachment>属性值。
每一张背景图片顺序对应相应的 attachment 属性。
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; }
<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>
{{EmbedLiveSample("多背景图支持")}}
CSS版本 | 推荐状态 | 点评 |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#the-background-attachment', 'background-attachment')}} | {{Spec2('CSS3 Backgrounds')}} | 该简单上手的属性已经支持更多的背景图和局部(local)值 |
{{SpecName('CSS2.1', 'colors.html#propdef-background-attachment', 'background-attachment')}} | {{Spec2('CSS2.1')}} | 无明显变化 |
{{SpecName('CSS1', '#background-attachment', 'background-attachment')}} | {{Spec2('CSS1')}} | 无明显变化 |
{{cssinfo}}
{{Compat("css.properties.background-attachment")}}