---
title: "解密CSS Sprites：技巧、工具和教程（译言）"
author: "chungle"
date: "2009-06-17T18:24:14Z"
tags: []
slug: "css-sprites"
draft: false
lastmod: "2009-06-17T18:24:14Z"
id: 410165
url: "https://chungle.iteye.com/blog/410165"
---

[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites并没有一个确定的中文翻译，通常被意译为“CSS图像拼合”或“CSS贴图定位”。CSS Sprites并不是一门新技术，目前它已经在*网页*开发中发展得较为成熟。CSS Sprites并不是什么金科玉律，但在很多情况下，它有着一定的优势，最重要的是它可以减轻服务器的负载，提高网页加载速度。

本文系统的介绍了[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites的概念、用途和用法，相信本文会解决你在使用CSS Sprites中遇到的大部分问题。

### 什么是[](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites？

“Sprite”(精灵)这个词在计算机图形学中有它独特的定义，由于游戏、视频等画质越来越高，必须有一种技术可以智能的处理材质和贴图，并且要 同时保持画面流畅。“Sprite”就是这样一种技术，它将许多图片组合到一个网格上，然后通过程序将每个网格的内容定位到画面上。

Sprite被定位到一副静态图片上，并且通过简单的程序或硬件即可正确定位到画面上，一幅幅图片就像是被“变”出来的，他们并没有单独占用内存，所以被取名为“Sprite精灵”。

 

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152344114-0.gif)](http://img45.imageshack.us/img45/4259/buildingssheet7dv.png)

上图是口袋妖怪的组合图片，可以[点这里欣赏更多](http://gmc.yoyogames.com/lofiversion/index.php/t307986.html)。不是加载每个但以图片

时间进行到2000年，Web[*设计*](http://www.yeeyan.com/articles/tag/%E8%AE%BE%E8%AE%A1)向着精致、巧妙的方向发展。设计师们开始考虑使用非Javascript的方 式制作鼠标滑过、悬停菜单的效果，这时[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprite应运而生，它基于同上文提到的游戏Sprite同样的原理，并且使用CSS更容易控制，很快的流行开来。

2004年，Dave Shea 提出了一种使用[*CSS*](http://www.yeeyan.com/articles/tag/CSS)控制组合图片的[方案](http://www.alistapart.com/articles/sprites) ，将许过小的图片组合在一起，使用css定义背景属性，来控制图片的显示位置和方式。

当页面加载时，不是加载每个单独图片，而是一次加载整个组合图片。这是一个了不起的改进，它大大减少了HTTP请求的次数，减轻服务器压力，同时缩短了悬停加载图片所需要的时间延迟，使效果更流畅，不会停顿。

### [](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites用在哪里？

[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites可以用在很多场合，大型网站可以将许多单独的图片，以有机的方式组合起来，从而使其便于维护和更新。图片之间通常会留出较大的空白，使 得图片不会影响*网页*的内容。但同时CSS Sprite大多使用于较固定的像素定位中，它的弹性较差，收到定位等因素的制约。所以，你需要在可维护性vs降低负载之间权衡利弊，选择最适合你的项目 的方式。

下面是一些[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites的使用范例：

[Xing](http://www.xing.de/)

这个网站将一些按钮、图标以及LOGO做成了[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites：

![Screenshot](http://oncoding.net/uploads/allimg/090429/2152343M6-1.png)

[Amazon](http://www.amazon.com/)  
亚马逊使用的大幅、整齐巧妙的[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234Nb-2.png)](http://www.flickr.com/photos/mezzoblue/3217540317/)

[Apple](http://www.apple.com/)  
苹果网站使用[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites来制作导航菜单的鼠标悬停效果：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152342464-3.jpg)](http://images.apple.com/global/nav/images/globalnavbg.png)

[YouTube](http://www.youtube.com/)  
YouTube使用了一个2008像素高的[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/21523442D-4.gif)](http://s.ytimg.com/yt/img/master-vfl89407.png)

[CNN](http://www.cnn.com/)  
CNN使用了非常简单谨慎的方案：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152342G8-5.gif)](http://i.cdn.turner.com/cnn/.element/img/2.0/global/icons/share_sprite.gif)

[Digg](http://www.digg.com/)  
Digg的方案比较复杂：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152346092-6.gif)](http://digg.com//img/menu-current.gif)

[Yahoo](http://www.yahoo.com/)  
Yahoo将他们漂亮的图标等距离排布起来：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234CX-7.gif)](http://l.yimg.com/a/i/ca/sp/purple/fp_spt_icons_0.0.2.png)

[Google](http://www.google.com/)  
Google使用了极其简化的方案：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152344H9-8.png)](http://www.google.ca/images/nav_logo3.png)

[Dragon Interactive](http://dragoninteractive.com/)  
一个丰富多彩的[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites方案：

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152344X0-9.jpg)](http://dragoninteractive.com/lib/i/navigation/sprite.jpg)

[TV1.rtp.pt](http://tv1.rtp.pt/noticias/index.php)  
一个很大很酷的[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites方案

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152342464-10.jpg)](http://img.rtp.pt/noticias/images/ImgNoticias_n_repeat.jpg)

[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites常用来合并频繁使用的图形元素，如导航、LOGO、分割线、RSS图标、按钮等。通常它们不会作为*网页*内容出现，因为涉及内容的图片并不是每个页面都一样。

### 关于[](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites的文章

[CSS Sprites: Image Slicing’s Kiss of Death](http://www.alistapart.com/articles/sprites)

中文版 http://www.liquidroc.name/post/my\_view\_on\_css\_[*sprites*](http://www.yeeyan.com/articles/tag/sprites).html

最权威的[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites介绍文章之一

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152344017-11.gif)](http://www.alistapart.com/articles/sprites)

[CSS Sprites: What They Are, Why They’re Cool And How To Use Them](http://css-tricks.com/css-sprites/ "What They Are, Why They’re Cool, and How To Use Them")  
一片图文并茂的介绍文章

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152341628-12.png)](http://css-tricks.com/css-sprites/)

[How Yahoo.com and AOL.com Improve Web Performance With CSS Sprites](http://www.websiteoptimization.com/speed/tweak/css-sprites/)  
介绍Yahoo、AOL等网站使用[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites降低服务器压力的案例。

[What Are CSS Sprites?](http://www.peachpit.com/articles/article.aspx?p=447210)  
又一片介绍文章

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234K05-13.jpg)](http://www.peachpit.com/articles/article.aspx?p=447210)

[Sprite Optimization](http://mezzoblue.com/archives/2009/01/27/sprite_optim/ "Permanent link for this article")  
Dave Shea的思考：是不是真的有必要简历复杂的大型[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites？答案是不！不要搞得太复杂，找到一个折中的方案才是正道。

[![CSS Sprites](http://oncoding.net/uploads/allimg/090429/21523434G-14.gif)](http://mezzoblue.com/archives/2009/01/27/sprite_optim/ "Permanent link for this article")

[Creating Easy and Useful CSS Sprites](http://cssglobe.com/post/3028/creating-easy-and-useful-css-sprites)

一篇教程，其源文件可下载学习

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152341045-15.png)](http://cssglobe.com/post/3028/creating-easy-and-useful-css-sprites)

[Fast Rollovers Without Preload](http://wellstyled.com/css-nopreload-rollovers.html)

一个快速翻转效果的例子

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152343X6-16.gif)](http://wellstyled.com/css-nopreload-rollovers.html)

[CSS Sprites + Rounded corners](http://cssglobe.com/post/3714/css-sprites-rounded-corners/)

另一个例子：使用[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites实现背景圆角

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152344T1-17.gif)](http://cssglobe.com/post/3714/css-sprites-rounded-corners/)

[CSS Image Sprites](http://websitetips.com/articles/css/sprites/)

一篇教程

[Optimize Your Website Using CSS Image Sprites](http://www.itnewb.com/archive/Optimize-Your-Web-Site-by-Using-CSS-Image-Sprites)

非常详细的教程，介绍了[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites的原理和应用方法。

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234J33-18.gif)](http://www.itnewb.com/archive/Optimize-Your-Web-Site-by-Using-CSS-Image-Sprites)

[Animated GIF For CSS Sprites](http://adamducker.com/blog/7/animated-gif-for-css-sprites/ "Animated GIF For CSS Sprites")

一个比较特别的应用

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152343634-19.gif)](http://adamducker.com/blog/7/animated-gif-for-css-sprites/)

[Image Sprite Navigation With CSS](http://stylemeltdown.com/2007/10/22/image-sprite-navigation-with-css/)

怎样制作简单的悬停菜单效果

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152341022-20.jpg)](http://stylemeltdown.com/2007/10/22/image-sprite-navigation-with-css/)

[Advanced CSS Menu](http://www.webdesignerwall.com/tutorials/advanced-css-menu/)

还是悬停效果

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234L45-21.gif)](http://www.webdesignerwall.com/tutorials/advanced-css-menu/)

[Creating and Using CSS Sprites](http://davidwalsh.name/css-sprites)

一个非常基本的教程

[![CSS Sprites Screenshot](http://oncoding.net/uploads/allimg/090429/2152341333-22.gif)](http://davidwalsh.name/css-sprites)

### [](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites视频教程

[How to Use CSS Sprites](http://www.from-the-couch.com/post.cfm/title/how-to-use-css-sprites)  
David Perel解释了[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites的基本概念，并演示了如何在*网页*中使用它，长度：10分钟

[Creating Rounded Buttons With CSS Sprites](http://www.from-the-couch.com/post.cfm/title/creating-rounded-buttons-with-css-sprites)  
继续上面的教程，展示了如何制作滑动按钮

[Exactly How to Use CSS Sprites](http://nettuts.com/videos/screencasts/exactly-how-to-use-css-sprites/)  
Andres Fernandez  展示了 [*CSS*](http://www.yeeyan.com/articles/tag/CSS) sprites如何加快加载时间和减少请求次数

[How To Use CSS Sprites](http://css-tricks.com/video-screencasts/43-how-to-use-css-sprites/)  
Chris Coyier 展示了一个 [*CSS*](http://www.yeeyan.com/articles/tag/CSS) sprites的范例，他将8幅图片组合在了一起，并且使用jQuery制作了一个小程序。

[Faster Page Loads With Image Concatenation](http://borkweb.com/story/faster-page-loads-with-image-concatenation)  
对于复杂的网站，减少图片请求数量可以减轻服务器负担，这是许多站长所希望看到的。

[CSS Image Sprites In 10 Minutes](http://www.csslessons.com/)  
另一个关于导航菜单的教程

[CSS: Using Percentages in Background-Image](http://www.sitepoint.com/blogs/2007/07/05/css-using-percentages-in-background-image/)  
介绍背景图片定位的方法

 

### 利用[](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites制作图像映射(image maps)

使用[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites，你可以对一个对象的一小部分加载翻转效果，使用负值的背景图片位置(`background-position` )，你可以创建基于CSS的图像映射，下面的文章讲述了这一技术：

[CSS Image Maps Using Sprites](http://lab.fatagnus.com/css-sprites/)  
一个基于[*CSS*](http://www.yeeyan.com/articles/tag/CSS)的图像映射的简单例子。你可以同[传统的方式](http://www.cssplay.co.uk/menu/imap.html)对比一下优劣。

[![Screenshot](http://oncoding.net/uploads/allimg/090429/21523452X-23.jpg)](http://lab.fatagnus.com/css-sprites/)

[City Guide Map Using Sprites](http://www.alistapart.com/d/sprites/ala-image3.html)  
另一个横向定位的例子

[![Screenshot](http://oncoding.net/uploads/allimg/090429/2152343511-24.jpg)](http://www.alistapart.com/d/sprites/ala-image3.html)

[Advanced Map Using Sprites](http://www.frankmanno.com/ideas/css-imagemap/)  
一个更高级的技术。

[![Screenshot](http://oncoding.net/uploads/allimg/090429/215234KC-25.jpg)](http://www.frankmanno.com/ideas/css-imagemap/)

### [](http://www.yeeyan.com/articles/tag/CSS)[*CSS*](http://www.yeeyan.com/articles/tag/CSS) Sprites技术