background-attachment - CSS(层叠样式表) 编辑

background-attachment CSS 属性决定背景图像的位置是在视口内固定,或者随着包含它的区块滚动。

语法

/* 关键 属性值 */
background-attachment: scroll;
background-attachment: fixed;
background-attachment: local;

/* 全局 属性值 */
background-attachment: inherit;
background-attachment: initial;
background-attachment: unset;

取值

fixed
此关键属性值表示背景相对于视口固定。即使一个元素拥有滚动机制,背景也不会随着元素的内容滚动。
local
此关键属性值表示背景相对于元素的内容固定。如果一个元素拥有滚动机制,背景将会随着元素的内容滚动, 并且背景的绘制区域和定位区域是相对于可滚动的区域而不是包含他们的边框。
scroll
此关键属性值表示背景相对于元素本身固定, 而不是随着它的内容滚动(对元素边框是有效的)。

标准语法

<attachment>#

where
<attachment> = scroll | fixed | local

例子

简单的例子

CSS样式表

p {
  background-image: url("https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/starsolid.gif");
  background-attachment: fixed;
}

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样式表

p {
  background-image: url("https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/starsolid.gif"), url("https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/startransparent.gif");
  background-attachment: fixed, scroll;
  background-repeat: no-repeat, repeat-y;
}

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版本推荐状态点评
CSS Backgrounds and Borders Module Level 3
background-attachment
Candidate Recommendation该简单上手的属性已经支持更多的背景图和局部(local)值
CSS Level 2 (Revision 1)
background-attachment
Recommendation无明显变化
CSS Level 1
background-attachment
Recommendation无明显变化
初始值scroll
适用元素all elements. It also applies to ::first-letter and ::first-line.
是否是继承属性
计算值as specified
Animation typediscrete

浏览器兼容性

BCD tables only load in the browser

本页中的兼容性表格是由结构化数据生成的。如果你想对数据提供补充,请查看此链接 https://github.com/mdn/browser-compat-data 并给我们一个请求补充的回复。

参见

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:44 次

字数:8194

最后编辑:8 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文