<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AddyOsmani.com &#124; Where User Interface Ideas Grows &#187; css sprite examples</title>
	<atom:link href="http://addyosmani.com/blog/tag/css-sprite-examples/feed/" rel="self" type="application/rss+xml" />
	<link>http://addyosmani.com/blog</link>
	<description>This is the home of Addy Osmani (Web Developer, Designer &#38; Author). Here you can find some great tips and tutorials on everything to do with web development and even a few useful code samples!</description>
	<lastBuildDate>Fri, 03 Sep 2010 21:35:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CSS Sprites in Under 10 Minutes</title>
		<link>http://addyosmani.com/blog/css-sprites-in-under-10-minutes/</link>
		<comments>http://addyosmani.com/blog/css-sprites-in-under-10-minutes/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 04:20:22 +0000</pubDate>
		<dc:creator>Addy</dc:creator>
				<category><![CDATA[Under 10 Minutes]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[animation examples]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css examples]]></category>
		<category><![CDATA[css sprite examples]]></category>
		<category><![CDATA[css sprites]]></category>

		<guid isPermaLink="false">http://addyosmani.com/blog/css-sprites-in-under-10-minutes/</guid>
		<description><![CDATA[In this post I&#8217;m going to teach you everything you need to know about CSS Sprites in under 10 minutes &#8211; you&#8217;ll learn a little the basics of what they can do..and more importantly, how you can implement them in your projects. So, lets start!. So..what is a Sprite? Back in 2004, a guy called [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;m going to teach you everything you need to know about CSS Sprites in under 10 minutes &#8211; you&#8217;ll learn a little the basics of what they can do..and more importantly, how you can implement them in your projects. So, lets start!.</p>
<p> <span id="more-38"></span></div>
<p> 
<p><b>So..what <i>is</i> a Sprite?</b></p>
<p>Back in 2004, a guy called Dave Shea came up with a simple CSS-based approach to using multiple-images across a website and combining them into a single <i>master image</i>. His concept was quite straight-forward -&#160; to show a single image from the master image you would use the background-position property in CSS to define the exact position of the image to display. It was versatile enough that you could use hover, active or focus effects using the same background-position property for any element displayed.&#160; </p>
<p>&#160;</p>
<p><b>Why should I use them? It&#8217;s easier to just slice my images separately, right?</b></p>
<p>CSS Sprites have a lot of different advantages. The first is that your users only need to download a single image file for the graphics on your page (and thus it can complete loading much quicker). Secondly, if you need to update any of the graphics on your page, you only need to release a single image file live to have the chances come into effect. Need to optimize all your images? A master image just requires one round of optimization so you effectively cut down your work exponentially.</p>
<p>&#160;</p>
<p><b>Before I learn how to use these Sprite things..who else is using them?</b></p>
<p>Everybody. Apple.com, Amazon, YouTube, CNN and even Digg.com use CSS Sprites for their graphics. There&#8217;s even a <a href="http://spritegen.website-performance.org/">website </a>that helps you generate your own Sprites by uploading your image to their server.</p>
<p>&#160;</p>
<p><a href="http://media1.smashingmagazine.com/images/css-sprites/apple.jpg"><img height="108" src="http://addyosmani.googlepages.com/apple.jpg" width="420" border="0" /></a></p>
<p><b></b></p>
<p><b>Okay..so how are they done?</b></p>
<p>Lets take the example of a horizontal navigation menu. Normally you would do something like this to set each of the background images for your menu items:</p>
<p>&#160;</p>
<p><font color="#ff0000">#nav li a {background:none no-repeat left center} </font></p>
<p><font color="#ff0000">#nav li a.item1 {background-image:url(&#8216;../img/image1.gif&#8217;)} </font></p>
<p><font color="#ff0000">#nav li a:hover.item1 {background-image:url(&#8216;../img/image1_over.gif&#8217;)} </font></p>
<p><font color="#ff0000">#nav li a.item2 {background-image:url(&#8216;../img/image2.gif&#8217;)} </font></p>
<p><font color="#ff0000">#nav li a:hover.item2 {background-image:url(&#8216;../img/image2_over.gif&#8217;)}</font></p>
<p>&#160;</p>
<p>For 10 different images that&#8217;s a whole 10 different HTTP requests that need to be made. We&#8217;re talking about what.. 20-40kb in total that your page needs to load up (just for the graphics?)&#160; We can do better than that..</p>
<p>If instead you use a &quot;master&quot; image to store all your menu backgrounds, it&#8217;s as easy as pie to use background-positioning to access the same images:</p>
<p>&#160;</p>
<p><font color="#ff0000">#nav li a {background-image:url(&#8216;../img/image_nav.gif&#8217;)} </font></p>
<p><font color="#ff0000">#nav li a.item1 {background-position:0px 0px} </font></p>
<p><font color="#ff0000">#nav li a:hover.item1 {background-position:0px -72px} </font></p>
<p><font color="#ff0000">#nav li a.item2 {background-position:0px -143px;} </font></p>
<p><font color="#ff0000">#nav li a:hover.item2 {background-position:0px -215px;}</font></p>
<p>&#160;</p>
<p>That means your page just needs to perform 1 HTTP request to grab all your image and it only takes up 15-18kb as you&#8217;re not using multiple files with their own image headers.</p>
<p>&#160;</p>
<p>Here&#8217;s another quick and dirty example:</p>
<p><a href="http://www.ignacioricci.com/tutorials/sprites/images/guideImgs/background-position.png"><img height="177" src="http://www.ignacioricci.com/tutorials/sprites/images/guideImgs/background-position.png" width="420" border="0" /></a></p>
<p>Here&#8217;s a master image which contains some pretty cool vector characters. I would like to use the third one, so using PhotoShop, Fireworks or my favourite image editor, I find out how far away the image is from the left and how far it is from the top. As you can see this is -196x and -2px. The image is 115px in width. Accessing the image is thenas simple as doing the following:</p>
<p>&#160;</p>
<p><font color="#ff0000">.sprite {background:url(sprite.png);}&#160; </font></p>
<p><font color="#ff0000">.thirdimage {width:115px; background-position:-196px -2px;}</font></p>
<p>&#160;</p>
<p>And that&#8217;s it!. If I&#8217;ve successfully convinced you that CSS Sprites are the way forward, </p>
<p>click <a href="http://www.itnewb.com/v/Optimize-Your-Web-Site-by-Using-CSS-Image-Sprites">here </a>to learn how to get the positions of the images in your &quot;master&quot; so you can start </p>
<p>making your own or check out the CSS Sprite generator <a href="http://spritegen.website-performance.org/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://addyosmani.com/blog/css-sprites-in-under-10-minutes/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
