CSS-背景附件 | background-attachment
如果指定了 background-image ,那么 background-attachment 决定背景是在视口中固定的还是随包含它的区块滚动的。
/* Keyword values */ background-attachment: scroll; background-attachment: fixed; background-attachment: local; /* Global values */ background-attachment: inherit; background-attachment: initial; background-attachment: unset;
|
初始值 |
scroll |
|---|---|
|
适用元素 |
all elements. It also applies to ::first-letter and ::first-line. |
|
是否是继承属性 |
no |
|
适用媒体 |
visual |
|
计算值 |
as specified |
|
Animation type |
discrete |
|
正规顺序 |
the unique non-ambiguous order defined by the formal grammar |
语法
background-attachment属性被指定为下面列表中的一个关键字值。
值
fixed——此关键字表示背景相对于视口固定。即使一个元素拥有滚动机制,背景也不会随着元素的内容滚动。
local——此关键字表示背景相对于元素的内容固定。如果一个元素拥有滚动机制,背景将会随着元素的内容滚动, 并且背景的绘制区域和定位区域是相对于可滚动的区域而不是包含他们的边框。
scroll——此关键字表示背景相对于元素本身固定, 而不是随着它的内容滚动(对元素边框是有效的)。
正式语法
<attachment>#where <attachment> = scroll | fixed | local
示例
简单例子
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; }
结果
多背景图支持
此属性支持多张背景图片。你可以用逗号分隔来为每一张背景图片指定不同的<attachment>。每一张背景图片顺序对应相应的attachment 类型。
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; }
结果
规范
|
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. |
浏览器兼容性
|
Feature |
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
|---|---|---|---|---|---|---|
|
Basic Support |
1.0 |
12 |
1.0 |
4 |
3.5 |
1.0 |
|
Multiple backgrounds |
1.0 |
12 |
3.6 |
9.0 |
10.5 |
1.3 |
|
local |
1.0 |
12 |
25 |
9.0 |
10.5 |
5.0 |
|
Feature |
Android |
Chrome for Android |
Edge mobile |
Firefox for Android |
IE mobile |
Opera Android |
iOS Safari |
|---|---|---|---|---|---|---|---|
|
Basic Support |
(Yes) |
(Yes) |
(Yes) |
1.0 |
(Yes) |
10.0 |
3.2 |
|
Multiple backgrounds |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
10.0 |
3.2 |
|
local |
? |
(Yes) |
(Yes) |
25 |
(Yes) |
? |
? |
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
CSS-定位 | <position>
CSSposition属性用于指定一个元素在文档中的定位方式。top,right,bottom和left属性则决定了该元素的最终位置。 位置坐标由一个或两个关键字指定,并带有可选的偏移量。每个关键字(left,right,top,bottom,或center)表示任一元件的盒子的边缘或两个边缘之间的中心线上。根据上下文,center表示左边缘和右边缘之间的中心,或者表示顶边和底边之间的中心。 如果指定,偏移量可以是相对<percentage>值或绝对值<length>。正值向右或向下偏移,以适当值为准。负值在相反的方向上偏移。 如果只指定一个关键字或偏移量,则它定义x坐标; 另一个轴的值被假定为center。 内插值 横坐标和纵坐标都是独立插值的。由于速度由<timing-function>两者定义,点将沿直线移动。 值 /* 1-value syntax */ keyword /* Either the horizontal or vertical position; the other axis defaults to center */ &...
-
下一篇
CSS-背景裁剪 | background-clip
background-clip 设置元素的背景(背景图片或颜色)是否延伸到边框下面。 /* Keyword values */ background-clip: border-box; background-clip: padding-box; background-clip: content-box; background-clip: text; /* Global values */ background-clip: inherit; background-clip: initial; background-clip: unset; 如果没有设置背景颜色或图片,那么这个属性只有在边框(border)设置为透明或半透明时才能看到视觉效果(查看border-style或border-image),不然的话,这个属性造成的样式变化会被边框覆盖住。 初始值 border-box 适用元素 all elements. It also applies to ::first-letter and ::first-line. 是否是继承属性 no 适用媒体 visual 计算值 as specif...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Red5直播服务器,属于Java语言的直播服务器
- Docker容器配置,解决镜像无法拉取问题
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2全家桶,快速入门学习开发网站教程
- Linux系统CentOS6、CentOS7手动修改IP地址
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Docker使用Oracle官方镜像安装(12C,18C,19C)

微信收款码
支付宝收款码