有几篇关于CSS Sprites的文章,基本上把其原理和机制说明得很清楚。 What Are CSS Sprites? How to create CSS sprites Creating Rollover Effects with CSS Sprites Building a Dynamic Banner with CSS Sprites High Performance Web Sites中关于CSS Sprites的内容3.2. CSS Sprites
CSS Sprite的例子
1. 图片限制(Image Slicing)[1] 典型如文本编辑器,小图标特别多,打开时一张张跑出来,给用户的感觉很不好。如果能用一张图解决,则不会有这个问题,比如百度空间、163博客、Gmail都是这么做的。 Image Slicing’s Kiss of Death http://www.alistapart.com/articles/sprites 2. 单图转滚(Single-image Rollovers)[1] 触发切换图片的需求,传统方案得重新请求新图片,因为网络问题经常造成停留或等待。如果能把多种状态合并成一张图,就能完美解决,然后再使用背景图技术模拟动态效果。 ColorScheme Ratings http://demo.rexsong.com/200608/colorscheme_ratings/ 3. 延长背景(Extend Background Image)[1] 如果图片的某边可以背景平铺无限延长,则不需要每个角、每条边单独搞出来,图片能少一个就少一个。其实,这个理论还可以扩展到四角容器里,好处是能大大简化HTML Structure。 Extend Background Image http://demo.rexsong.com/200705/extend_background_image/ 综合案例 Google Korea(1和2技巧) http://demo.rexsong.com/200705/google_korea/ CSS Menus(2和3技巧) http://demo.rexsong.com/200705/css_background_menus/
CSS Sprites的问题
由于IE6存在的background的flicker问题IE6/Win, background image on , cache=‘check every visit’: flicker!,有人针对此问题提出了解决方案Fast Rollovers Without Preload 关于IE6的flicker问题,fivesevensix.com上有一篇很不错的研究文章Minimize Flickering CSS Background Images in IE6 另外:brunildo.org的CSS tests and experiments是关于css各种功能不错的参考手册和测试工具。