什么是精灵图?
精灵图是 CSS Sprites 图片整合技术,又称为CSS精灵/雪碧图,它就是把多张小图合成一张大图,通过css中的background-position属性,显示精灵图中某一个小图标。
精灵图的优点
- 采用了精灵图这一技术可以缓解加载时间过长从而影响用户体验的这个问题。
- 在一定程度上减少了页面的加载速度,也一定程度上缓解了服务器的压力。
- 使用精灵图可以有效减少服务器接收和发送请求的次数,从而达到提高页面的加载性能。
关键代码
a{
min-width: 103px;
height: 32px;
background: aqua url(./CSS\ Sprites.png);
margin: 10px;
}
a:nth-of-type(1){
background-position:unset;
}
a:nth-of-type(2){
background-position: -104px 0;
}
a:nth-of-type(3){
background-position: 0 -32px;
}
- 示例
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<style>
footer{
position: absolute;
width: 100%;
bottom: 20px;
display: flex;
justify-content: center;
}
a{
min-width: 103px;
height: 32px;
background: aqua url(./CSS\ Sprites.png);
margin: 10px;
}
a:nth-of-type(1){
background-position:unset;
}
a:nth-of-type(2){
background-position: -104px 0;
}
a:nth-of-type(3){
background-position: 0 -32px;
}
a:nth-of-type(4){
background-position: -104px -32px;
}
a:nth-of-type(5){
background-position: 0 -66px;
}
a:nth-of-type(6){
background-position: -104px -66px;
}
a:nth-of-type(7){
background-position: 0 -99px;
}
a:nth-of-type(8){
background-position: -104px -99px;
min-width: 69px;
}
a:nth-of-type(9){
background-position: -104px -132px;
min-width: 87px;
}
</style>
</head>
<body>
<footer>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
<a href="#"></a>
</footer>
</body>
</html>