<?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>buyco.ca &#187; sprites</title>
	<atom:link href="http://www.buyco.ca/tag/sprites/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.buyco.ca</link>
	<description>because someone stole my name</description>
	<lastBuildDate>Fri, 24 Jun 2011 01:23:42 +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>Optimizing with Image Sprites and CSS for Beginners (Part 2)</title>
		<link>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/</link>
		<comments>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 09:34:02 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=118</guid>
		<description><![CDATA[I know what a sprite is. Now what do I do?!]]></description>
			<content:encoded><![CDATA[<h4>I know what a sprite is, now what?</h4>
<p>In Part 1 of <a title="Optimizing with Image Sprites and CSS for Beginners" href="http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/">Optimizing with Image Sprites and CSS for Beginners</a> I went through an explanation of what sprites are, how they work, and why to even bother. This time around we&#8217;re going to jump into some implementation of these sprites. I am still calling this Beginner stuff (only so that I can hit everyone with some hardcore code in the future), but if you happen to get confused follow me on <a title="Follow Jonathan Buyco on Twitter" href="http://twitter.com/jonathanbuyco" target="_blank">Twitter</a> and ask away!</p>
<h4>What am I going to use the sprite for?</h4>
<p>First let&#8217;s figure out what we&#8217;re even going to use the sprite for. When you&#8217;re ready to start slicing up your design, use your intuition and think of the graphical elements which will be used and reused on your site; this usually includes your navigation and maybe some buttons. In this example I&#8217;m going to use a sprite to create a &#8220;close&#8221; button and a textual navigation bar (SEO FTW). Here&#8217;s what I&#8217;ll be working with:</p>
<p><img src="/images/sprites.gif" alt="sprites" /></p>
<h4>Let&#8217;s open with a close</h4>
<p>This is the close button which I want sprite-ized:</p>
<p><a id="close" title="Close this badboy" href="#">Close</a></p>
<p>Your processes may be different, but I always start off with the HTML markup. The reason is because I want it as clean as possible and easily accessible for our search crawler friends. With a few quick keystrokes and a little touch of magic, this is what my markup for the close button looks like:</p>
<pre class="brush: xml;">&lt;a href="/close" title="Close this badboy" id="close"&gt;Close&lt;/a&gt;</pre>
<p>That was easy! Now let&#8217;s fancy this up with a bit of CSS magic and add the graphical close icon:</p>
<pre class="brush: css;">a#close {
	display: block;
	height: 14px;
	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;
	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;
}
a#close:hover {
	background-position: -32px -114px;
}
a#close:active {
	background-position: -32px -128px;
}</pre>
<h4>Wait.. what?</h4>
<p>I skipped about 20 steps there, so let&#8217;s go through this line by line:</p>
<pre class="brush: css; first-line: 2">	display: block;</pre>
<p>We need to give our link element layout so the graphic doesn&#8217;t get cropped. Display as <em>block</em>, kablammo problem solved! Almost..</p>
<pre class="brush: css; first-line: 3">	height: 14px;</pre>
<p>The close button consists of a 14&#215;14 area on our sprite map. We need to set the <em>height</em> so we don&#8217;t get any cropping.</p>
<pre class="brush: css;first-line: 4">	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;</pre>
<p>This part is a bit arbitrary. I set the <em>line-height</em> just so the text is vertically positioned evenly, and the font is a tribute to <a title="Ikea says Goodbye to Futurea" href="http://www.idsgn.org/posts/ikea-says-goodbye-to-futura/" target="_blank">Ikea</a> (don&#8217;t hurt me <img src='http://www.buyco.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<pre class="brush: css;first-line: 6">	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;</pre>
<p>The fun part! The <em>padding</em> is there so that the text for &#8220;close&#8221; is shifted to the right and not disturbing the space for the graphic. But where the hell did I get the -32px -100px from? It&#8217;s the sprite&#8217;s position! 32 pixels from the left, and 100 pixels from the top. An easy way of finding your graphic positions in your sprite map is to open up the file in Photoshop, use the rectangular marquee tool (M), then start a selection from the top left of the document and end at the topmost and leftmost of the graphic. Check your Info window (F8) and take note of the width and height of your selection&#8211; and there are your positioning coordinates!</p>
<pre class="brush: css;first-line: 9">a#close:hover {
	background-position: -32px -114px;
}
a#close:active {
	background-position: -32px -128px;
}</pre>
<p>These modifiers are just added to give a hover and click effect to the button. The only style changing here is the position of the background which then points to the coordinates of the hover and active states. Put this all together and you have yourself a close button!</p>
<p>I obviously geared the markup for this single example, not taking into account that you may want to reuse the code. With just a small change to select the class of the elements instead of styling based on ID, you can have multiple instances of this button:</p>
<pre class="brush: css;">a.close {
	display: block;
	height: 14px;
	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;
	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;
}
a.close:hover {
	background-position: -32px -114px;
}
a.close:active {
	background-position: -32px -128px;
}</pre>
<p>For you newbies: it&#8217;s the dot.</p>
<h4>Navigation Sprite&#8217;ing</h4>
<div id="navigation">
<ul>
<li><a href="#1"><span id="nav-first"> </span><span>Link 1</span></a></li>
<li class="splitter"></li>
<li><a href="#2"><span>Link 2</span></a></li>
<li class="splitter"></li>
<li><a href="#3"><span>Link 3</span></a></li>
<li class="splitter"></li>
<li><a href="#4"><span>Link 4</span></a></li>
<li class="splitter"></li>
<li><a href="#5"><span id="nav-last"> </span><span>Link 5</span></a></li>
</ul>
</div>
<p><br style="clear: both;" /></p>
<p>This may seem a bit more advanced, but I am using very similar techniques as the example above. Once again, I&#8217;ll start off with the markup:</p>
<pre class="brush: xml;">
&lt;div id="navigation"&gt;
	&lt;ul title="Jon's awesome navigation"&gt;
		&lt;li&gt;&lt;a href="link1" title="link 1"&gt;&lt;span id="nav-first"&gt;&lt;/span&gt;&lt;span&gt;Link 1&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link2" title="link 1"&gt;&lt;span&gt;Link 2&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link3" title="link 1"&gt;&lt;span&gt;Link 3&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link4" title="link 1"&gt;&lt;span&gt;Link 4&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link5" title="link 1"&gt;&lt;span id="nav-last"&gt;&lt;/span&gt;&lt;span&gt;Link 5&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;</pre>
<p>Wow that looks like some dirty code. Forgive me for the empty <em>span</em> tags, it&#8217;s getting late. I&#8217;m assuming everyone&#8217;s navigation markup looks pretty similar to this, so it shouldn&#8217;t be much of a stretch to update your style sheets. Speaking of which, here they are for this example:</p>
<pre class="brush: css;">div#navigation {
	font: 11px 'Verdana','Arial',sans-serif;
}
div#navigation ul {
	margin: 0;
	padding: 0;
	height: 50px;
	background: url(sprites.gif) 0 0 repeat-x;
	float: left;
	list-style-type: none;
}
div#navigation ul li {
	float: left;
	display: block;
	width: 50px;
}
div#navigation ul li.splitter {
	width: 2px;
	height: 50px;
	background: url(sprites.gif) -7px -100px no-repeat;
	float: left;
}
div#navigation ul li a {
	display: block;
	height: 50px;
	line-height: 50px;
	color: #606;
	text-decoration: none;
}
div#navigation ul li a:hover {
	background: url(sprites.gif) 0 -50px repeat-x;
	color: #404
}
div#navigation ul li a span	{
	display: block;
	padding: 0 5px;
}
div#navigation ul li a span#nav-first {
	display: block;
	position: absolute;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) 0 -100px no-repeat;
}
div#navigation ul li a:hover span#nav-first {
	background: url(sprites.gif) -16px -100px
}
div#navigation ul li a span#nav-last {
	display: block;
	position: absolute;
	margin: 0 0 0 44px;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) -10px -100px no-repeat;
}
div#navigation ul li a:hover span#nav-last {
	background: url(sprites.gif) -26px -100px;
}</pre>
<p>Quite a bit of styles. It&#8217;s okay though, you&#8217;ve obviously gone through the first example a million times already, so this should be a breeze.</p>
<p>Now, a lot of the styling is there just for aesthetics so we&#8217;ll skip all of that. Let&#8217;s just jump to the areas where we&#8217;re using sprites:</p>
<pre class="brush: css;first-line: 4">div#navigation ul {
	margin: 0;
	padding: 0;
	height: 50px;
	background: url(sprites.gif) 0 0 repeat-x;
	float: left;
	list-style-type: none;
}</pre>
<p>Here we are actually going to be repeating the sprite as the background in the x direction because I want this navigation bar to expand in the event of new sections. When creating a sprite with the intention of repeating it as a background, keep in mind that they can only be repeated in one direction: x or y. You cannot repeat a slice of a sprite in both directions, otherwise you may have other graphics in the sprite included in the repetition. Looking back at the original sprite, this background can be repeated in the x direction since there are no other slices to the left or right of that area.</p>
<pre class="brush: css;first-line: 30">div#navigation ul li a:hover {
	background: url(sprites.gif) 0 -50px repeat-x;
	color: #404
}</pre>
<p>Skipping all the positioning and font styles, I want to add a hover effect. Once again I&#8217;m utilizing a repeating background in the x-direction. The position is found using my marquee technique above.</p>
<pre class="brush: css;first-line: 17">div#navigation ul li.splitter {
	width: 2px;
	height: 50px;
	background: url(sprites.gif) -7px -100px no-repeat;
	float: left;
}</pre>
<p>I thought the navigation bar would look a little weird without having some kind of splitter between each option, so the styles above should take care of that. Here I am actually specifying the width because I do not want the background to repeat.</p>
<pre class="brush: css;first-line: 38">div#navigation ul li a span#nav-first {
	display: block;
	position: absolute;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) 0 -100px no-repeat;
}
div#navigation ul li a:hover span#nav-first {
	background: url(sprites.gif) -16px -100px
}
div#navigation ul li a span#nav-last {
	display: block;
	position: absolute;
	margin: 0 0 0 44px;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) -10px -100px no-repeat;
}
div#navigation ul li a:hover span#nav-last {
	background: url(sprites.gif) -26px -100px;
}</pre>
<p>Here I&#8217;m just being a little fancy and adding the smooth bits at each end of the navigation bar. Essentially the same techniques used above.</p>
<h4>Holy Crap!</h4>
<p>Yeah, it&#8217;s a lot to take in but I didn&#8217;t lie when I said this was a beginner guide because I assume everyone reading this is an expert with CSS.</p>
<p>The examples above are merely examples. I hope no one copy-and-pastes the markup because it won&#8217;t work for your site (figuratively speaking). I hope you learn from what was presented above, test out your own techniques, and apply it in your daily web development ventures. The best way of learning is to do it yourself, this guide is only here as a guide and also because I have nothing better to do at 2:00AM on a Wednesday morning <img src='http://www.buyco.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Optimizing with Image Sprites and CSS for Beginners (Part 1)</title>
		<link>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/</link>
		<comments>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 00:20:59 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Dev Tips and Tricks]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=70</guid>
		<description><![CDATA[Image Sprites and fancy CSS code-work can save your website visitors' download time.
]]></description>
			<content:encoded><![CDATA[<h4>What the hell is an image sprite? Why do I care? How do I use it?!</h4>
<p>That&#8217;s probably the same thought process I had when I first started reading about image sprites. After a few minutes of investigation via Google, I wanted to try to implement this technique every and anywhere possible on my websites. This isn&#8217;t necessarily a required method of implementation of a web site but it is still something that every web developer should at least have an understanding of.</p>
<p>In this 2-part series I&#8217;ll go over what an image sprite is, when to use them, how to use them, as well as provide a few examples.</p>
<h4>What is an image sprite?</h4>
<p>Back in the olden days (circa 90s), multiple images were merged into a single graphic file to quickly composite 2D games. <a href="http://upload.wikimedia.org/wikipedia/commons/0/03/Sprite-example.gif" target="_blank">Here is an example</a> [source: wikipedia]. Certain parts of this single graphic would be extracted and used to animate objects, characters, etc. We can utilize the same methodology with our websites and optimize load times (as well as provide an easy means of updating graphics when the times come).</p>
<h4>How does an image sprite work in the web-world?</h4>
<p>One of the greatest things to happen to a web developer / designer is the invention of Cascading Style Sheets. We can use CSS to pick certain coordinates within a sprite and display those areas on our websites. It may sound a bit tedious to have to find the pixel by pixel locations of every sprite you want to use, but in doing this you may be able to shave off precious load times of your web page.</p>
<h4>Decreasing load times? Wtf?</h4>
<p>When you want to increase the performance of a website, one of your key objectives is to reduce the amount of time it takes for a particular page (or pages) to load. One way is to keep the number of HTTP requests to a minimum. Any time you insert an image, or include a JavaScript file, or any other file for that matter, it is an additional HTTP request. In the nature of a sprite, you are making a single HTTP request for that graphic, then referencing different parts of it to be displayed on a web page. This works great granted that the size of the single graphic is less than the size of all it&#8217;s containing graphics individually (we&#8217;re talking bits and bytes here, not lengths and widths).</p>
<p>Another potential advantage is browser caching. Take, for example, this navigation bar:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/an-nav.jpg"><img class="alignnone size-full wp-image-71" title="an-nav" src="http://www.buyco.ca/wp-content/uploads/2009/06/an-nav.jpg" alt="an-nav" width="610" height="33" /></a></p>
<p>In this case we assume that this navigation bar is split up into 8 images: 4 for the &#8220;default state&#8221;, and 4 for the &#8220;hover state&#8221;. When the user hovers their mouse over a particular image, the &#8220;hover state&#8221; image is loaded on the fly. That&#8217;s a single HTTP request for that extra image. If a user does not hover over all the graphics, not all of them will load, so basically this page will only load the additional images if it *really* needs to. But still not the best situation&#8230;</p>
<p>Each graphic is around 1.097Kb &#8211; 0.997Kb totaling 8.11Kb. In actuality, I used a single sprite totaling 3.292Kb. By loading even only the &#8220;default state&#8221; images, we are still reaching a size larger than the single sprite.</p>
<h4>Applications</h4>
<p>That is only one situation where you can really utilize the benefits of using a sprite. Using them for navigation bars with hover states is probably the most common use, but some sites have taken it a step further.</p>
<p>Take Google for example. Shaving off a single millisecond of load time of their site can translate into some amazing results (probably because they get millions of hits a day). You may not see this type of traffic, but in either case you still don&#8217;t want to have a slow website.</p>
<p>Back to my point, lets have a look at a screenshot of my iGoogle homepage:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/igoogle.gif"><img class="alignnone size-full wp-image-72" title="igoogle" src="http://www.buyco.ca/wp-content/uploads/2009/06/igoogle.gif" alt="igoogle" width="621" height="135" /></a></p>
<p>Looks like a simple logo right? In actuality, the logo is a part of an image sprite:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/ico_sprite_classic.gif"><img class="alignnone size-full wp-image-73" title="ico_sprite_classic" src="http://www.buyco.ca/wp-content/uploads/2009/06/ico_sprite_classic.gif" alt="ico_sprite_classic" width="167" height="68" /></a></p>
<p>All those smaller icons are used as elements within the page. With some fancy CSS code-work, only a single image is used on this page! (That&#8217;s actually a lie since I have multiple gadgets running with external image sources).</p>
<p>Other high-volume sites like Yahoo and eBay also use image sprites so why don&#8217;t you!?</p>
<p>Tune in next time as I go through a few implementation examples: Optimizing with Image Sprites and CSS for Beginners (Part 2)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

