<?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>Tutorial9 &#187; Alexis Brille</title>
	<atom:link href="http://www.tutorial9.net/author/alexis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tutorial9.net</link>
	<description>Tutorial Bliss.</description>
	<lastBuildDate>Thu, 09 Feb 2012 14:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Building Faster Websites with CSS Sprites</title>
		<link>http://www.tutorial9.net/tutorials/web-tutorials/building-faster-websites-with-css-sprites/</link>
		<comments>http://www.tutorial9.net/tutorials/web-tutorials/building-faster-websites-with-css-sprites/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 07:00:58 +0000</pubDate>
		<dc:creator>Alexis Brille</dc:creator>
				<category><![CDATA[Web Development Tutorials]]></category>

		<guid isPermaLink="false">http://www.tutorial9.net/?p=4303</guid>
		<description><![CDATA[Using CSS sprites allows you to greatly increase your websites speed by using single image files that contain multiple graphics. This tutorial will show you how to use CSS sprites of your own!]]></description>
			<content:encoded><![CDATA[<p class="important">Using CSS sprites allows you to greatly increase your websites speed by using single image files that contain multiple graphics. In other words, when you have many images to be used, instead of having them as different individual files, we combine them into one. Therefore, the client computer only downloads one image for all the different graphics to be displayed.</p>
<div class="toc" id="toc">
<p><span class="label">Quick Nav:</span></p>
<ul>
<li><a href="#1">Tutorial</a></li>
<li><a href="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/css_sprites_demo_home.html">View Live Demo</a></li>
<li><a href="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/DemoSourceFiles.zip">Download the Source Files</a></li>
</ul>
</div>
<div class="tip">
<h4>Benefits of CSS sprites</h4>
<p>Using CSS sprite images on your website is the best way to <b>quicken up the loading time of your images</b> and is also the best way to keep up with <b>today&#8217;s best web design practices</b>.</p>
<p>CSS sprites decrease the loading time of images on your web pages, and can significantly lower the number of requests that your website makes which can drastically lower total page load time.</p>
<p>If you need more reason to use sprites, consider that most of the top sites out there are using them: Apple, Amazon, Yahoo&hellip; Tutorial9 :P</p>
</div>
<h3>Materials Needed:</h3>
<ul>
<li><a href="http://vandelaydesign.com/blog/galleries/inspirationalnavigation-menus/" target="_blank">A navigation menu (with multiple states other than the normal state, you may download the sample).</a></li>
<li><a href="http://www.creativeapplications.net/2008/11/24/rulers-mac-windows/" target="_blank">Measuring application (err, you can try with a real-life ruler but Photoshop, Slammer or XScope would be better).</a></li>
<li><a href="http://www.smashingmagazine.com/2008/05/07/35-useful-source-code-editors-reviewed/" target="_blank">A code editor (preferably, Notepad++ on Windows or TextWrangler on Mac, both free. I used both TextWrangler and Coda).</a></li>
</ul>
<h3>Image &#8211; Step 1: Set up each individual navigation menu image states</h3>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/1neonBlackNavBarStatesNormal.png" width="549" height="100" /><br />
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/2neonBlackNavBarStatesHover.png" width="549" height="100" /><br />
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/3neonBlackNavBarStatesClicked.png" width="549" height="100" /></div>
<p>Since this tutorial is not focused on designing beautiful navigation bars, we&#8217;ll use pre-made ones. Don&#8217;t worry, there are plenty of <a href="http://www.tutorial9.net/category/photoshop/">Photoshop tutorials</a> on Tutorial9 to teach you that, browse around!</p>
<div class="tip">
<h4>Need a pretty navigation bar?</h4>
<p>Download the source files <a href="#2">here</a>.</p>
</div>
<p>In the source files, there are 3 separate files for the navigation bar&#8217;s normal state, hover/rollover state and clicked state.</p>
<p>We&#8217;ll teach you how to combine them yourself in the next step. But you can always use the one combined image of all 3 states provided in the <a href="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/DemoSourceFiles.zip">source files</a>.</p>
<h3>Step 2: Image &#8211; Combine all 3 navigation bar image states into 1 image</h3>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/neonBlackNavBar.png" width="489" height="156" /></div>
<p>Right now, we have 3 separate PNG files for the 3 different states. It&#8217;s time to copy-paste them together into 1 image just as shown above.</p>
<div class="tip">
<h4>A personal tip.</h4>
<p>Usually, what I like to do in this step is (in Photoshop) make the background invisible so that all you have left are the buttons themselves like this:</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/neonBlackGreenNoBg.png" width="506" height="65" /></p>
<p>Then, I go to Image > Trim:</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/trim.png" width="240" height="437" /></p>
<p>Select Transparent Pixels and check all the sides to trim away:</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/trimTransparent.png" width="399" height="294" /></p>
<p>So, now, you have a concise image of the buttons, nothing else. (Note that I made the background visible here):</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/1neonBlackNavBarNormal.png" width="489" height="52" /></p>
</div>
<p>Now that we&#8217;ve trimmed away correctly all the 3 states, let&#8217;s merge them into 1 image.</p>
<p>There is no standard way of doing this, you may be used to your own way of copy pasting but for the sake of example, start by opening up the normal state then press CTRL or CMD + a (select all) and CTRL or CMD + a (copy)just like below.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/selectAll.png" width="513" height="72" />
</div>
<p>Create new document (CTRL or CMD + n). </p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/newDoc.png" width="600" height="370" />
</div>
<div class="tip">
<h4>Copy pasting tip</h4>
<p>We copied the navigation bar before making the new document so that Photoshop will automatically know the width and height of the navigation bar.</p>
</div>
<p>You can see from the new document panel that the height of one navigation bar is 52. Since we&#8217;d like to put 3 states in this document, that&#8217;s 52 * 3 = 156. So let&#8217;s change the height of this new document to 156.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/changeHeight.png" width="342" height="32" />
</div>
<p>Then, paste the normal state of the navigation bar into the new document.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/pasteNormalState.png" width="510" height="369" />
</div>
<p>Move it to the very top of the document.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/moveNormalState.png" width="518" height="363" />
</div>
<p>Repeat this process 2 more times or according to the number navigation bar states you have.</p>
<p>Paste and align the hover state of the navigation bar right below the normal state.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/pasteHoverState.png" width="505" height="390" />
</div>
<p>Lastly, paste and align the clicked state of the navigation bar right below the hover state.</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/pasteClickedState.png" width="510" height="394" />
</div>
<p>
If you have aligned all 3 states correctly, they will fit into the new 489 x 52 document perfectly just like so:
</p>
<div class="image-container full">
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/neonBlackNavBar.png" width="489" height="156" />
</div>
<h3>Step 3: Image &#8211; Measure the height of each buttons</h3>
<p><strong>Important:</strong> Get something to write on, whether a new text file on screen or a piece of paper for this next step &#8217;cause we&#8217;ll need to <strong>write down the width and height for each button</strong>.</p>
<p><strong>To easily determine</strong> the height of the buttons, we can <strong>look</strong> back on the <strong>height of each navigation bar</strong> for obvious reasons.</p>
<div class="tip">
<h4>Tip for measuring in Photoshop</h4>
<p>Use the Ruler Tool in Photoshop to easily measure things in your document.</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/rulerTool.png" width="176" height="104" /></p>
<p>In the screenshot below, the Ruler Tool is used to measure the height of the Home button.</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/rulerToolMeasure.png" width="42" height="339" /></p>
<p>Also, move the navigation bar below the navigation bar you want to measure one pixel down to see its edge.</p>
<p><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/onePixelDown.png" width="151" height="74" /></p>
</div>
<p>After measuring <strong>the height of the buttons</strong>, we now know the height of each button is <strong>52px</strong>.</p>
<p>And since all of the buttons have the same height, all of the buttons will all be 52px.</p>
<h3>Step 4: Image &#8211; Measure the width of each buttons</h3>
<p>
Now, to measure <strong>the width of each buttons</strong> is a bit more complicated.
</p>
<p>
As mentioned above, the <strong>horizontal gap between each buttons should preferably be even</strong> so that when you divide the gap width into 2, one side will not be wider than the other. <em>This is optional, though.</em>
</p>
<p>
Here the width of the gap between each button is 16px.
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/gapWidth.png" width="90" height="57" /></div>
<p>
When divided into 2 sides, each side of the gap will be 8px.
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/gapWidthDivided.png" width="60" height="49" /></div>
<div class="tip">
<h4>Tip to keep measurements in Photoshop</h4>
<p>
Make sure you have Rulers turned on (cmd + r) or go to View > Rulers.
</p>
<p>
Then, click on the vertical ruler and drag out a guide to the point where you would want your button to end.
</p>
<p>
<img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/homeGuide.png" width="121" height="51" />
</p>
</div>
<p>
Now that you know how wide your Home button will be, let&#8217;s measure it.
</p>
<p>
Use the Ruler tool again and measure from the left edge of the Home button to the guide we made as shown in the last tip, like this:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/rulerHomeButton.png" width="171" height="66" /></div>
<p>
You can see the width in the Ruler tool panel:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/widthHomeRulerPanel.png" width="244" height="30" /></div>
<p>
Repeat the same process for all the other 2 buttons.
</p>
<h3>Step 5: Image &#8211; Width and height of each buttons</h3>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/neonBlackNavBarPx.png" width="507" height="104" /></div>
<h3>Step 6: CSS &#8211; setting up the navigation bar</h3>
<p>
Here comes the coding! I hope you already understand a bit of CSS and HTML. If not, then you can just go &#8216;head and copy-paste the codes below, :P.
</p>
<p>
First let&#8217;s create a DIV for our navigation bar in our HTML document and name it &#8220;navBarContainer&#8221;.
</p>
<pre name="code" class="html" style="overflow: auto;">
&lt;div id="navBarContainer"&gt;
&lt;/div&gt;
</pre>
<p>
Now, let&#8217;s style this DIV in an external CSS stylesheet.
</p>
<div class="tip">
<h4>Linking an external CSS stylesheet to an HTML document</h4>
<p>
Put this line of code into the &lt;head&gt; tag of your document:
</p>
<pre name="code" class="html" style="overflow: auto;">
    &lt;link rel="stylesheet" type="text/css" media="screen" href="stylesheet_name.css" /&gt;
</pre>
<p>
Like this:
</p>
<pre name="code" class="html" style="overflow: auto;">
&lt;html&gt;
    &lt;head&gt;
        &lt;link rel="stylesheet" type="text/css" media="screen" href="stylesheet_name.css" /&gt;
    &lt;/head&gt;
&lt;/html&gt;
</pre>
</div>
<p>
Continuing on with styling the &#8220;navBarContainer&#8221; DIV.
</p>
<p>
In the external CSS, let&#8217;s write down the width, height and margin of the DIV that contains this navigation bar. It&#8217;s very easy to do so since we&#8217;ve already determined the width and height before.
</p>
<pre name="code" class="html" style="overflow: auto;">
#navBarContainer{
    width: 489px;
    height: 52px;
    margin: 0 auto;
}
</pre>
<p>
Note: the height for the navigation bar is 52 pixels tall, not 156 pixels. This is because we&#8217;d like to show only one navigation bar at a time.
</p>
<p>
Next, we&#8217;ll show how to use HTML lists to display links. This is a much more structured way for links.
</p>
<h3>Step 8: CSS &#8211; Using HTML lists (ul tags) for the navigation bar links</h3>
<p>
Let&#8217;s continue to code our HTML document.
</p>
<p>
We&#8217;ll now add the lists for each link:
</p>
<pre name="code" class="html" style="overflow: auto;">
&lt;div id="navBarContainer"&gt;
	&lt;ul id="navBar"&gt;
		&lt;li id="navBarHome"&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;
		&lt;li id="navBarServices"&gt;&lt;a href="#"&gt;Services&lt;/a&gt;&lt;/li&gt;
		&lt;li id="navBarAboutus"&gt;&lt;a href="#"&gt;About us&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>
By default, HTML lists are structured on display as vertical bullets.
</p>
<p>
To make them flow horizontally, we&#8217;ll use &#8220;none&#8221; for the &#8220;list-style&#8221; attribute. We&#8217;ll also have to use &#8220;inline&#8221; for the &#8220;display&#8221; attribute.
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar{
	width: 489px;
	height: 52px;
	margin: 0 auto;
	list-style: none;
}

ul#navBar li{
	display: inline;
}
</pre>
<p>
So, instead of this:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/linksVertical.png" width="88" height="59" /></div>
<p>
We have this:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/linksHorizontal.png" width="177" height="28" /></div>
<h3>Step 9: CSS &#8211; adding images for each links</h3>
<p>
This first step is useful to still display the links as text whenever CSS has been turned off in browsers and the images aren&#8217;t available.
</p>
<p>
Add this code to do so:
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li a{
	height: 52px;
	float: left;
	text-indent: -9999px;
}
</pre>
<p>
The &#8220;-9999px&#8221; was not a typo, this is too keep the text links off the screen if CSS is turned on. If not then the text links would overlap the navigation bar images.
</p>
<p>
Now to display some image for the home link!
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarHome a{
	width: 126px;
	background: url(neonBlackNavBar.png) no-repeat 0px 0px;
}
</pre>
<p>
Ta-da! Our first link is set up:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/firstLink.png" width="171" height="59" /></div>
<p>
By the way, we <strong>changed our body background to #131313</strong>.
</p>
<p>
For the other 2 buttons, we apply the same codes but we change their background positions and widths.
</p>
<p>
Here&#8217;s the code for the &#8220;Services&#8221; button:
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarServices a{
	width: 179px;
	background: url(neonBlackNavBar.png) no-repeat -126px 0px;
}
</pre>
<p>
<strong>&#8220;WAIT!</strong> How&#8217;d you get that &#8220;-126px&#8221;?! <strong>Good question</strong>.
</p>
<p>
Since we used a sprite image, we need to &#8216;cut&#8217; the images <strong>inside</strong> the sprite image.
</p>
<p>
<strong>We do this by specifying the background position.<br />
<br />
Since the &#8220;Home&#8221; button x-axis was at 0px, the &#8220;Services&#8221; button (obviously, come on! :D) must begin where the &#8220;Home&#8221; button ended. And that is 126 pixels.</strong>
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/1neonBlackNavBarPxNormal2.png" width="489" height="91" /></div>
<p>
Now, here&#8217;s the code for the &#8220;About us&#8221; button:
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarAboutus a{
	width: 184px;
	background: url(neonBlackNavBar.png) no-repeat -179px 0px;
}
</pre>
<p>
And we&#8217;re done!
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/normalWrong.png" width="547" height="82" /></div>
<p>
<strong>Actually, no we&#8217;re not done, the image above is a pitfall.</strong>
</p>
<p>
The &#8220;About us&#8221; button does not start at -179px just because the &#8220;Services&#8221; button ended there.
</p>
<p>
The correct calculation is (width of “Home” button) + (width of “Services” button). Therefore, the &#8220;About us&#8221; button begins at -305px.
</p>
<p>
Now, we&#8217;re <strong>truly</strong> done, I promise.
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/normalRight.png" width="545" height="75" /></div>
<h3>Step 10: CSS &#8211; adding hover states to the navigation bar</h3>
<p>
Adding hover states owns the same principle as adding the other 2 links. We just move their background positions.
</p>
<p>
For the &#8220;Home&#8221; button, we simply move its y-axis background position to where the normal state navigation bar ended, like so:
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarHome a:hover{
	background-position: 0 -52px;
}
</pre>
<p>
Now, when you hover over the &#8220;Home&#8221; button, you&#8217;ll see its hover state.
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/homeHover.png" width="516" height="64" /></div>
<p>
Let&#8217;s apply the same principles to the &#8220;Services&#8221; button.
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarServices a:hover{
	background: url(neonBlackNavBar.png) no-repeat -126px -52px;
}
</pre>
<p>
We&#8217;ll get this:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/servicesHover.png" width="545" height="68" /></div>
<p>
Lastly, to the &#8220;About us&#8221; button.
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarAboutus a:hover{
	background: url(neonBlackNavBar.png) no-repeat -305px -52px;
}
</pre>
<p>
Preview:
</p>
<div class="image-container full"><img src="http://tutorial9.s3.amazonaws.com/uploads/2009/08/css-sprites/aboutUsHover.png" width="529" height="67" /></div>
<h3>Step 11: CSS &#8211; adding clicked/active states to the navigation bar</h3>
<p>
Given the keyword &#8220;active&#8221; for clicked link states, I really, <strong>REALLY</strong> hope you can already do this part yourself.
</p>
<p>
Here are the codes for the hover states for all 3 links:
</p>
<pre name="code" class="html" style="overflow: auto;">
ul#navBar li#navBarHome a:active{
	background-position: 0 -104px;
}

ul#navBar li#navBarServices a:active{
	background: url(neonBlackNavBar.png) no-repeat -126px -104px;
}

ul#navBar li#navBarAboutus a:active{
	background: url(neonBlackNavBar.png) no-repeat -305px -104px;
}
</pre>
<h3>More Ways to Speed Up Your Website</h3>
<p>If you&#8217;re a speed junkie, then you&#8217;ll love these great articles that explore even more ways to reduce load time of your sites:</p>
<ul>
<li><a href="http://www.uxbooth.com/blog/how-to-minimize-load-time-for-fast-user-experiences/">How To Minimize Load Time for Faster User Experiences</a></li>
<li><a href="http://www.uxbooth.com/blog/speed-up-your-website-with-better-image-optimization-in-photoshop/">Speed Up Your Website with Better Image Optimization in Photoshop</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tutorial9.net/tutorials/web-tutorials/building-faster-websites-with-css-sprites/feed/</wfw:commentRss>
		<slash:comments>132</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk
Database Caching using disk
Object Caching 168/202 objects using disk
Content Delivery Network via Amazon Web Services: S3: tutorial9.s3.amazonaws.com

Served from: www.tutorial9.net @ 2012-02-11 14:10:42 -->
