前端基础知识|【前端基础知识】精灵图/雪碧图的实现以及优缺点


精灵图/雪碧图的实现以及优缺点

  • 是什么?
  • 为什么?(优点)
  • 如何使用?

是什么? 前端基础知识|【前端基础知识】精灵图/雪碧图的实现以及优缺点
文章图片

精灵图也称雪碧图,就是很多张图片放在一张图片上,用户只需请求一张图片就可以拿到所有需要的图片。开发人员利用背景图的位置去显示不同位置的图片,这是前端图片优化的一种方式。
为什么?(优点) 1、可以减小图片的总大小。
2、获取全部的图片只需一次请求,可以减少http请求的次数。
缺点:
1、降低开发效率
2、维护难度加大
如何使用?
精灵图 - 锐客网 > li { width: 30px; height: 30px; background-image: url(./精灵图.gif); background-repeat: no-repeat; list-style-type: none; float: left; margin: 10px; }/* 清楚浮动 */ .ul1::after { content: ''; display: block; clear: both; }.ul2::after { content: ''; display: block; clear: both; }.ul1 li:nth-child(1) { background-position: 0 0; }.ul1 li:nth-child(2) { background-position: -42px 0; }.ul1 li:nth-child(3) { background-position: -84px 0; }.ul1 li:nth-child(4) { background-position: -126px 0; }.ul1 li:nth-child(5) { background-position: -168px 0; }.ul1 li:nth-child(6) { background-position: -210px 0; }.ul2 li:nth-child(1) { background-position: 0 -40px; }.ul2 li:nth-child(2) { background-position: -42px -40px; }.ul2 li:nth-child(3) { background-position: -84px -40px; }.ul2 li:nth-child(4) { background-position: -126px -40px; }.ul2 li:nth-child(5) { background-position: -168px -40px; }.ul2 li:nth-child(6) { background-position: -210px -40px; }

前端基础知识|【前端基础知识】精灵图/雪碧图的实现以及优缺点
文章图片

【前端基础知识|【前端基础知识】精灵图/雪碧图的实现以及优缺点】以上就是精灵图/雪碧图的内容。
我会将自己平时项目中常见的问题以及笔试面试的知识在CSDN与大家分享,一起进步,加油。

    推荐阅读