background-attachment: fixed

本文介绍了CSS中background-attachment属性的用法,包括如何设置背景图像固定或随页面滚动,探讨了scroll、fixed和local三个值的具体效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSS background-attachment 属性

background-attachment设置背景图像是否固定或者随着页面的其余部分滚动。

标签定义及使用说明
默认值: scroll
继承: no
版本: CSS1
JavaScript 语法: object object.style.backgroundAttachment=“fixed”

属性值
scroll 背景图片随着页面的滚动而滚动,这是默认的。
fixed 背景图片不会随着页面的滚动而滚动。
local 背景图片会随着元素内容的滚动而滚动。
initial 设置该属性的默认值。 阅读关于 initial 内容
inherit 指定 background-attachment 的设置应该从父元素继承。
在这里插入图片描述

background-attachment: fixed

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>学习天地</title> {% load static %} <!-- 加载静态文件标签 --> <style> /* 设置背景图片 */ body { min-height: 10vh; /* 设置最小高度为视口高度 */ margin: 0; /* 移除默认边距 */ padding: 0; /* 移除默认内边距 */ font-family: Arial, sans-serif; background-image: url("{% static 'images/学习天地.jpg' %}"); <!-- 使用静态文件标签加载图片 --> background-size: cover; /* 宽度 300px,高度自动调整 */ background-position: center; <!-- 图片居中显示 --> background-attachment: fixed; background-repeat: no-repeat; <!-- 防止图片重复 --> color: white; <!-- 设置文字颜色为白色,确保在背景上清晰可见 --> } /* 其他样式保持不变 */ .course-list { list-style-type: none; padding: 0; } .course-item { background-color: rgba(249, 249, 249, 0.8); <!-- 添加透明度,使背景图片可见 --> margin: 10px 0; padding: 15px; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } .course-item a { color: #007BFF; text-decoration: none; } .course-item a:hover { text-decoration: underline; } .pagination { margin-top: 20px; text-align: center; } .pagination a { margin: 0 5px; color: #007BFF; text-decoration: none; } .pagination a:hover { text-decoration: underline; } </style> </head> <body> <h1 style="font-size: 100px; color:rgb(14, 84, 204);">学习天地</h1> <a href="{% url 'home' %}" style="font-size: 50px; color:rgb(183, 118, 5);">点击进入主页</a> </body> </html>代码运行后,浏览器只能显示背景图片的一部分
最新发布
03-19
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值