一.引言
雪碧图被运用在众多使用了很多小图标的网站上。相对于把每张小图标以.png格式文件的形式引用到页面上,使用雪碧图只需要引用一张图片,对内存和带宽更加友好。
二.实现
假设我们通过.toolbtn的类,为应用该类的各元素提供一张背景图片:
<style> .toolbtn {background:url(myfile.png); display:inline-block; height:20px; width:20px } </style>
背景位置,可以通过在background的url()直接定义X,Y轴的值,或者通过background-position属性来添加。例如:
<style> #btn1 {background-position: -20px 0px} #btn2 {background-position: -40px 0px} </style>
id=btn1的元素背景左移20px,id=btn2的元素背景左移40px(假设这两个元素的都添加了toolbtn类,应用了上面样式定义的图片效果)
类似的,你也可以使用下面的方式添加hover的状态:
#btn:hover {background-position: [pixels shifted right]px [pixels shifted down]px;}
三.实例
index.html
<html> <head> <title>CSS Sprite/Slide Button Demo</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <link href="style.css" rel="stylesheet" type="text/css" /> <div id="wrapper"> <p>I've found hundreds of tutorials on the sliding doors button and hundreds on the sprite technique but never a combination of both. This button combines the wonderful sprite technique with the sliding doors technique to make a beautiful button that not only loads in a snap but also looks great and scales to your text! I hope this is something useful for you! Please give me a shout-out if you like and use this code!</p> <hr /> <h3>Button Examples:</h3> <div class="clearbutton"> <a class="GlobalRedButton" href="#"><span>Hey this is so cool!</span></a> </div> <p> <div class="clearbutton"> <a class="GlobalBlueButton" href="#"><span>Hey this is even cooler!</span></a> </div> <div class="clearbutton"> <a class="GlobalBlackButton" href="#"><span>Now I think I'll write a whole sentence in the button!</span></a> </div> <div class="clearbutton"> <a class="GlobalOrangeButton" href="#"><span>So Neat!</span></a> </div> <div class="clearbutton"></div> <hr /> <h3>Copy Paste Code:</h3> </p><p>HTML Code:</p> <div class="code"> <p> <div class="clearbutton"> <a class="GlobalOrangeButton" href="#"><span>So Neat!</span></a> </div> </p> </div> <p>CSS Code:</p> <div class="code"> <p>/* GLOBAL ORANGE BUTTON *****************************/<br /> a.GlobalOrangeButton span {<br /> background: transparent url('images/button_left_orange.png') no-repeat 0 0;<br /> display: block;<br /> line-height: 22px;<br /> padding: 7px 0 5px 18px;<br /> color: #fff;<br /> }</p> <p> a.GlobalOrangeButton {<br /> background: transparent url('images/button_right_orange.png') no-repeat top right;<br /> display: block;<br /> float: left;<br /> height: 34px;<br /> margin-right: 6px;<br /> padding-right: 20px;<br /> text-decoration: none;<br /> font-family: Arial, Helvetica, sans-serif;<br /> font-size:12px;<br /> font-weight:bold;<br /> }</p> <p> a.GlobalOrangeButton:hover span {<br /> background-position: 0 -34px; color: #fff;<br /> }</p> <p>a.GlobalOrangeButton:hover {<br /> background-position: right -34px;<br /> }</p> </div> <h3>The 2 image files:</h3> <p><img src="images/button_left_orange.png" alt="" border="0" /> <img src="images/button_right_orange.png" alt="" border="0" /></p> <hr /> <div class="clearbutton"> <a class="GlobalBlueButton" href="CSS-Sprite-Slide-Button.zip"><span>Download Source Files</span></a> </div> <div class="clearbutton"></div> <p>Includes all code, images, and Photoshop image files.</p> <hr /> <h3>Credits:</h3> <p> CREATED BY: Brandon Setter<br /> WEBSITE: <a href="http://media-sprout.com">http://Media-Sprout.com</a><br /> FOLLOW ME: <a href="http://twitter.com/bsetter">twitter.com/bsetter</a><br /> CREATION DATE: 10/2009<br /> VERSION 1.0 </p> <hr /> </div> </body> </html>
style.css
/* CREATED BY: Brandon Setter WEBSITE: http://Media-Sprout.com DATE: 10/2009 Please give me a shout-out if you like and use this code ;-) */ /* DISREGARD THIS PAGE STYLING *****************************/ p, hr, h1, h2, h3 {margin: 10px 0 10px; } #wrapper {margin: 0 auto; width: 600px;} .code {margin: 0 0 10px 0; padding: 15px; border: 2px solid #999;} /* BUTTON STYLING FROM HERE DOWN *****************************/ div.clearbutton {clear:both; height:0; font-size: 1px; line-height: 1px;} /* GLOBAL RED BUTTON *****************************/ a.GlobalRedButton span {background: transparent url('images/button_left_red.png') no-repeat 0 0; display: block; line-height: 22px; padding: 7px 0 5px 18px; color: #fff;} a.GlobalRedButton {background: transparent url('images/button_right_red.png') no-repeat top right; display: block; float: left; height: 34px; margin-right: 6px; padding-right: 20px; text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;} a.GlobalRedButton:hover span {background-position: 0 -34px; color: #fff;} a.GlobalRedButton:hover {background-position: right -34px;} /* GLOBAL BLUE BUTTON *****************************/ a.GlobalBlueButton span {background: transparent url('images/button_left_blue.png') no-repeat 0 0; display: block; line-height: 22px; padding: 7px 0 5px 18px; color: #fff;} a.GlobalBlueButton {background: transparent url('images/button_right_blue.png') no-repeat top right; display: block; float: left; height: 34px; margin-right: 6px; padding-right: 20px; text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;} a.GlobalBlueButton:hover span {background-position: 0 -34px; color: #fff;} a.GlobalBlueButton:hover {background-position: right -34px;} /* GLOBAL ORANGE BUTTON *****************************/ a.GlobalOrangeButton span {background: transparent url('images/button_left_orange.png') no-repeat 0 0; display: block; line-height: 22px; padding: 7px 0 5px 18px; color: #fff;} a.GlobalOrangeButton {background: transparent url('images/button_right_orange.png') no-repeat top right; display: block; float: left; height: 34px; margin-right: 6px; padding-right: 20px; text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;} a.GlobalOrangeButton:hover span {background-position: 0 -34px; color: #fff;} a.GlobalOrangeButton:hover {background-position: right -34px;} /* GLOBAL BLACK BUTTON *****************************/ a.GlobalBlackButton span {background: transparent url('images/button_left_black.png') no-repeat 0 0; display: block; line-height: 22px; padding: 7px 0 5px 18px; color: #fff;} a.GlobalBlackButton {background: transparent url('images/button_right_black.png') no-repeat top right; display: block; float: left; height: 34px; margin-right: 6px; padding-right: 20px; text-decoration: none; font-family: Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;} a.GlobalBlackButton:hover span {background-position: 0 -34px; color: #fff;} a.GlobalBlackButton:hover {background-position: right -34px;}
运行效果:
文章来源:https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/CSS_Image_Sprites
相关推荐
这个“css雪碧图制作工具”就是为了方便开发者高效地创建和管理雪碧图而设计的。 在网页开发中,每一张单独的图片都需要发送一个HTTP请求,这在大量小图标存在的页面中会显著增加页面加载时间。雪碧图的出现,就是...
**CSS雪碧图技术详解与实战** 在网页设计中,CSS雪碧图(Sprite)是一种优化页面加载速度的技巧,通过合并多个小图到一张大图中,减少HTTP请求次数,从而提高页面性能。本教程将详细介绍CSS雪碧图的工作原理、如何...
雪碧图(Sprite)是一种网页图像处理方式,它将多个小图像合并到一张大图(称为雪碧图或精灵图)中,然后通过CSS样式控制显示其中的部分图像,以此达到减少HTTP请求,优化页面加载速度的效果。在网页设计中,尤其是...
**CSS Sprite技术详解** CSS Sprite,也被称为雪碧图,是一种网页图像处理方式,它将多个小图标合并到一张大图中,...通过使用像"css sprite 雪碧图生成小程序"这样的工具,我们可以更加便捷地管理和应用雪碧图技术。
css sprite雪碧图生成小工具 v4.3 css sprite 雪碧图生成小程序,已在github上开源,地址:https://github.com/iwangx/sprite ,请需要的朋友注意版本更新
CssSprite是一款很好用的小图合并工具,能自动生成CSS代码,导入小图就可以直接用了~
可减轻广大Web程序员或网站网页设计爱好者制作雪碧图时的苦恼、愉快地享用CSS雪碧图网页开发技术。 在网页中使用雪碧图技术,有如下优点: 1.减少加载网页图片时对服务器的请求次数; 2.提高网页的加载速度...
**CssSprite雪碧图详解** 在网页设计中,图片的加载速度往往直接影响到用户体验。为了优化网页性能,提高页面加载速度,一种名为“CssSprite”(雪碧图)的技术应运而生。雪碧图是一种将多个小图标合并到一张大图中...
2. **CSS定位**:通过CSS的`background-image`属性设置雪碧图的URL,再利用`background-position`属性来定位要显示的图标在雪碧图中的位置。 3. **大小调整**:利用CSS的`width`和`height`属性调整元素大小,使之与...
1. **CSS雪碧图原理**:CSS雪碧图是将多个小图标合并到一个大的图像文件中,然后通过CSS的`background-position`属性来控制显示哪一部分。这样只需要加载一次大图,就能显示多个图标,减少了网络请求。 2. **工具...
"雪碧图工具CssSprite.exe"是一个专为生成雪碧图并自动生成对应的CSS代码而设计的软件,使得这一过程变得更加便捷高效。 在网页开发中,每个单独的小图都需要一个HTTP请求来获取,当页面中包含大量小图时,这些请求...
**CssSprite(雪碧图制作工具)** 在前端开发中,优化网页加载速度是至关重要的。其中,一种常见的优化策略就是使用CssSprite技术,也就是我们常说的“雪碧图”。雪碧图是一种将多个小图标合并到一张大图中,然后...
CSS Sprite,也被称为雪碧图,是一种网页图像处理方式,它将多个小图标合并到一张大图中,通过CSS的background-position属性来定位显示所需的小图标,以此达到减少HTTP请求,提高页面加载速度的效果。在Web性能优化...
雪碧图是将多个小图标合并到一张大图片中,然后通过CSS定位显示需要的图标。Java作为一种强大的后端开发语言,可以用于自动生成雪碧图和相应的CSS样式文件,以自动化这个过程。 本项目的核心目标是读取指定目录下的...
css sprite 雪碧图生成小程序,已在github上开源,地址:https://github.com/iwangx/sprite ,请需要的朋友注意版本更新,下载最新版。 博客园地址:http://www.cnblogs.com/wang4517/p/4493917.html
3. **CSS编写**:编写CSS样式,使用`background-image`属性设置雪碧图的URL,然后通过`background-position`来调整背景位置,展示出雪碧图中需要的部分。 4. **HTML应用**:在HTML中应用这些CSS类,将雪碧图中的每...
效果描述: 之前看到一个网站,用户上传自己喜欢的...今天特意整理了一个用纯CSS3实现的滤镜图片效果供给大家参考 使用方法: 1、将head中的CSS引入到你的网页中 2、将body中的代码部分拷贝到你需要的地方即可