CSS background-attachment属性实例讲解
background-attachment 属性用于指定背景图像是固定的还是随浏览器窗口中页面的其余部分滚动。
此属性具有滚动、固定和本地三个值。它的默认值是滚动,这会导致元素不随其内容滚动。此属性的本地值导致元素随内容滚动。如果我们将该值设置为fixed,则在浏览器中滚动时背景图像不会移动。
此 CSS 属性可以支持多个背景图像。我们可以为每个 background-image 指定不同的 background-attachment 属性值,用逗号分隔。每个图像都将与此属性的相应值匹配。
用法
background-attachment:scroll | fixed | local | initial | inherit;
该属性的值定义如下。
属性值
scroll:它是防止元素随内容滚动但随页面滚动的默认值。
fixed:使用此值,背景图像不会随元素移动,即使元素具有滚动机制。它会导致图像被锁定在一个地方,甚至文档的其余部分也会滚动。
local:使用这个值,如果元素有滚动机制,背景图片会随着元素的内容滚动。
initial:它将属性设置为其默认值。
inherit:它从其父元素继承属性。
让我们通过一些插图来理解这个属性。
示例
在此示例中,我们使用 background-attachment 属性的滚动值,这是默认行为。所以当页面滚动时,背景也会随之滚动。
<!DOCTYPE html>
<html>
<head>
<title>
background-attachment property
</title>
<style>
#example {
background-image: url("lion.png");
font-size:35px;
border:4px solid red;
color:white;
background-position:center;
background-color:green;
background-repeat:no-repeat;
background-attachment:scroll;
}
</style>
</head>
<body>
<h1> background-attachment:scroll;</h1>
<p> If there is no scrollbar on your screen, then try to resize the browser's window to see the effect. </p>
<div id="example">
<p>
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
</div>
</body>
</html>
输出
示例 - 使用固定值
在此示例中,我们使用 background-attachment 属性的固定值。该值固定了背景图像,即使文档的其余部分滚动,图像也不会移动。
<!DOCTYPE html>
<html>
<head>
<title>
background-attachment property
</title>
<style>
#example {
background-image: url("lion.png");
font-size:35px;
border:4px solid red;
color:white;
background-position:center;
background-color:green;
background-repeat:no-repeat;
background-attachment:fixed;
}
</style>
</head>
<body>
<h1> background-attachment:fixed;</h1>
<p> If there is no scrollbar on your screen, then try to resize the browser's window to see the effect. </p>
<div id="example">
<p>
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
</div>
</body>
</html>
输出
示例 - 使用本地值
在此示例中,我们使用 background-attachment 属性的本地值。在这里, background-image 将随着元素内容的滚动而滚动。
<!DOCTYPE html>
<html>
<head>
<title>
background-attachment property
</title>
<style>
#example {
background-image: url("lion.png");
font-size:35px;
border:4px solid red;
color:white;
background-position:center;
background-color:green;
background-repeat:no-repeat;
background-attachment:local;
}
</style>
</head>
<body>
<h1> background-attachment:local;</h1>
<p> If there is no scrollbar on your screen, then try to resize the browser's window to see the effect. </p>
<div id="example">
<p>
Hi, Welcome to the javaTpoint.com. This site is developed so that students may learn computer science related technologies easily. The javaTpoint.com is always providing an easy and in-depth tutorial on various technologies. No one is perfect in this world, and nothing is eternally best. But we can try to be better.
</p>
</div>
</body>
</html>
输出
现在,让我们看另一个示例,其中我们为元素使用多个背景图像。
示例
在这里,有两个背景图像我们正在应用 background-attachment 属性。第一张图片的附件设置为固定,而第二张图片的附件设置为滚动。
<!DOCTYPE html>
<html>
<head>
<title>
background-attachment property
</title>
<style>
#example {
background-image: url("jtp.png"), url("forest.jpg");
height:500px;
border:4px solid red;
background-position:center;
background-repeat:no-repeat;
background-attachment:fixed, scroll;
}
</style>
</head>
<body>
<h1> background-attachment:scroll;</h1>
<p> If there is no scrollbar on your screen, then try to resize the browser's window to see the effect. </p>
<div id="example">
</div>
</body>
</html>
输出
- HTML 教程
- HTML 简介
- html div 标签介绍
- html span 标签介绍
- html a 超链接标签
- HTML Br换行标签介绍
- HTML P段落标签介绍
- HTML br与p标签区别
- Html H 标题标签
- html px em pt长度单位
- HTML form 标签
- HTML radio 单选框
- HTML B 加粗标签
- HTML strong加粗粗体标签
- HTML em 强调标签
- HTML i 斜体标签
- HTML u下划线标签
- HTML s 删除线标签
- Html img 图片标签
- Html上标注sup与下标注sub标签
- HTML nobr 禁止换行标签
- HTML hr 水平线标签
- HTML label 标签
- HTML input 标签
- HTML textarea 标签
- HTML select下拉列表标签
- HTML checkbox 多选框
- HTML font color 标签
- HTML iframe 框架标签
- HTML Table 表格
- HTML dl dt dd 标签
- HTML ol li有序列表标签
- HTML ul li 无序列表标签
- HTML 注释
- CSS 教程
- CSS 简介
- CSS 语法
- CSS Id 和 Class选择器
- CSS 样式的创建
- CSS background 背景介绍
- CSS 文本样式
- CSS font 字体
- CSS A 链接
- CSS ul ol列表样式
- CSS TABLE 样式
- CSS 框模型
- CSS border 边框
- CSS Outlines 轮廓
- CSS 外边距 Margin
- CSS Padding 内边距
- CSS 分组和嵌套选择器
- CSS 尺寸 (Dimension)
- CSS Display 属性
- CSS Position 定位
- CSS Float 浮动
- CSS 水平对齐(Horizontal Align)
- CSS 组合选择符
- CSS 伪类
- CSS 伪元素
- CSS 导航栏
- CSS 下拉菜单
- CSS 图片廊
- CSS 图像透明/不透明
- CSS sprite 图像拼合技术
- CSS 媒体类型
- CSS 属性选择器
- CSS 实例
- Meteva笔记:加载GRIB 2要素场
- crontab 指令笔记
- Shell 脚本实现串口操作
- 【CSS】714- 你所不知道的 CSS 负值技巧与细节
- SRC挖掘利器—溯光开源插件化渗透测试框架
- 【React】717- 从零实现 React-Redux
- 【Vue.js】Vue.js介绍及MVVM模式
- 【Vue.js】Vue.js基本语法
- 【JS】723- 前端如何优雅的处理类数组对象?
- GitLabCI作业中如何使用私有仓库镜像?
- 三分钟学会自定义spring-boot-starter
- 分析Netflix公司产品Spinnaker的微服务实践
- 教你用策略模式解决多重if-else
- 模板模式以及实战应用
- 大四如何快速找到实习岗位