CSS Sprites 是 CSS 图像合成技术,有的称 CSS 精灵有的称雪碧图,他的作用就是把网页上很多小图标放到一张图片里面,然后通过 CSS 里面的 background-position 来控制每个图片的坐标,再利用 CSS 的“background-image”,“background- repeat”的组合来精确的定位出背景图片的位置。
使用场景
- 1.静态图片,不随用户信息的变化而变化
- 2.小图片,图片容量比较小
- 3.加载量比较大
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>*{margin:0;padding: 0}ul,li{list-style: none;}.nav{width:736px;height: 78px;margin:50px auto;}.nav li{float: left;margin-right: 2px;}.nav li:nth-child(1){width:98px;}.nav li:nth-child(2){width:122px;}.nav li:nth-child(3){width:144px;}.nav li:nth-child(4){width:118px;}.nav li:nth-child(5),.nav li:nth-child(6){width:122px;}.nav li:nth-child(6){margin-right: 0;}.nav li a{display: block;height: 78px;background: url(img/nav.png) no-repeat;}.nav li:nth-child(2) a{background-position: -100px 0;}.nav li:nth-child(3) a{background-position: -224px 0;}.nav li:nth-child(4) a{background-position: -370px 0;}.nav li:nth-child(5) a{background-position: -490px 0;}.nav li:nth-child(6) a{background-position: -614px 0;}.nav li a:hover{background: url(img/nav-on.png) no-repeat;}.nav li:nth-child(2) a:hover{background-position: -100px 0;}.nav li:nth-child(3) a:hover{background-position: -224px 0;}.nav li:nth-child(4) a:hover{background-position: -370px 0;}.nav li:nth-child(5) a:hover{background-position: -490px 0;}.nav li:nth-child(6) a:hover{background-position: -614px 0;}</style></head><body><ul class="nav"><li><a href="javascript:;"></a></li><li><a href="javascript:;"></a></li><li><a href="javascript:;"></a></li><li><a href="javascript:;"></a></li><li><a href="javascript:;"></a></li><li><a href="javascript:;"></a></li></ul></body></html>
