批改状态:合格
老师批语:恭喜, 掌握了
. 常用背景控制属性
| 描述 | 代码 |
|---|---|
| 背景颜色 | background-color: |
| 裁切 | background-clip: content-box; |
| 渐变 | background: linear-gradient() |
| 背景图片 | background-image: url(‘图片名称’) |
| 规定背景图像是否固定或者随着页面的其余部分滚动 | background-attachment: fixed; |
| 背景定位 | background-position: |
| 背景尺寸 | background-size: |
| 简写 | 1.background: url(timg.jpg)2.background: violet; |
| 补发光 | box-shadow: |
| 圆角 | box-shadow: |
.背景控制示例
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>背景</title><style>.box {width: 300px;height:300px;/* border: 3px solid black; */border-radius: 150px;/* 背景色 */background-color: blueviolet;/* 因为内边距是透明的,只能设置宽度不能设置样式,因此,背景色默认可以从内边距透出来 *//* padding: 20px; *//* 控制背景的覆盖范围限制在内容区,裁切 *//* background-clip: border-box; *//* background-clip: content-box; *//*渐变 */background: linear-gradient(blue,red);background: linear-gradient(to right,blue,red,white);/* 背景图片 */background-image: url('timg.jpg');/* 同一个框不重复出现图片 */background-repeat: no-repeat;/* background-repeat: repeat-y; *//* background-attachment: fixed; *//* 背景定位 *//* background-position: left; *//* 只写一个,第二个默认就是center *//* background-position: right center; *//* 只写一个,第二个默认就是50% *//* background-position: 50% 20%; *//* 背景尺寸 */background-size: contain;background-size: cover;/* 简写 */background: violet;background: url('timg.jpg') no-repeat center;position: relative;top: 30px;left: 30px;/* 外发光 */box-shadow: 0px 0px 6px #8888cursor: pointer;}</style></head><body><div class="box"></div></body></html>
.效果如下:

CSS Sprites叫 CSS精灵或者雪碧图,是一种网页图片应用处理方式
CSS Sprites其实就是把网页中一些背景图片整合到一张图片文件中。
再利用CSS的”background-image”,”background-repeat”,”background-position”的组合进行背景定位,background-position可以用数字精确的定位出背景图片的位置。
在网页访问中,客户端每需要访问一张图片都会向服务器发送请求,所以,访问的图片数量越多,请求次数也就越多,造成延迟的可能性也就越大。
所以,CSS Sprites技术加速的关键,并不是降低质量,而是减少个数,当然随之而来的增加内存消耗,CSS Sprites图片繁琐的合成等缺点在网站性能的提升前,也就不足为道了。
减少图片的字节减少了网页的http请求,从而大大的提高了页面的性能减少命名难的问题
在图片合并的时候,你要把多张图片有序的合理的合并成一张图片,还要留好足够的空间,防止板块内出现不必要的背景。在宽屏,高分辨率的屏幕下的自适应页面,图片如果不够宽,很容易出现背景断裂。CSS Sprites在维护的时候比较麻烦,如果页面背景有少许改动,一般就要改这张合并的图片,无需改的地方最好不要动,这样避免改动更多的CSS,如果在原来的地方放不下,又只能(最好)往下加图片,这样图片的字节就增加了,还要改动CSS。
只有真正的了解background-position的特性才能在需要的场景更好的运用它。
background-position 属性设置背景图像的起始位置。
这个属性设置背景原图像(由 background-image 定义)的位置,背景图像如果要重复,将从这一点开始。
提示:background-position属性设置背景原图像(由 background-image 定义)的位置,意味着使用这个属性的前提是必须设置背景原图像background-image。
了解了background-position属性的用法,使用雪碧图之前,我们需要知道雪碧图中各个图标的位置。
.精灵图示例代码
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>背景实战: 精灵图/雪碧图</title><style>.box1{width: 500px;height: 400px;border: 1px solid#000;background-image: url("1.png");background-repeat: no-repeat;background-position: 0px 0px;}.box2{width: 110px;height: 110px;background-image: url("1.png");background-repeat: no-repeat;background-position: -330px -110px;}.box3{width: 110px;height: 110px;background-image: url("1.png");background-repeat: no-repeat;background-position: -330px 0;}</style></head><body><div class="box1"></div><div class="box2"></div><div class="box3"></div></body></html>
.效果如下:
1.进入站点
https://www.iconfont.cn/
2.通过GITHUB账号登入
3.“图标库”或搜索 寻找需要的图标
4.将需要的图标加入购物车
5.将图标通过项目的方式下载到本地站点目录并解压
6.font-class 引用:
使用步骤如下:
第一步:引入项目下面生成的 fontclass 代码:
<link rel="stylesheet" href="./iconfont.css">
第二步:挑选相应图标并获取类名,应用于页面:
<span class="iconfont icon-xxx"></span>
“ iconfont” 是你项目下的 font-family。可以通过编辑项目查看,默认是 “iconfont”。
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="font/iconfont.css"><title>阿里图标使用方法-font-class 引用</title><style>.icon{font-size: 70px;color: chocolate;}.time{font-size: 80px;color: darkgreen;}</style></head><body><span class="iconfont icon-icon-test12 icon"></span><span class="iconfont icon-icon-test5 time"></span></body></html>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号