background-size的用法:

本文详细介绍了CSS中background-size属性的四种设置方式:通过长度、百分比、cover及content方式调整背景图片的尺寸,以便更好地适应不同的网页设计需求。

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

background-size有四个取值:

               1、length:设置背景的高度跟宽度(第一个值设置宽度,第二个值设置高度,如果只设置一个值,第二个值会设为"auto")

2、percentage:以父元素的百分比来设置背景图像的宽度跟高度(第一个值设置宽度,第二个值设置高度,如果只设置一个值,第二个值会设为"auto")

3、cover:把背景图像扩展至足够大,以使背景图像完全覆盖背景区域,背景图像的某些部分也许无法显示在北京定位区域中

4、content:把图像扩展至最大尺寸,以使起宽度和高度完全适应内容区域。


<!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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值