<?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>Anne Dorko &#187; css sprites</title>
	<atom:link href="http://www.annedorko.com/blog/tag/css-sprites/feed" rel="self" type="application/rss+xml" />
	<link>http://www.annedorko.com</link>
	<description>Help for small business</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:36:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS Tutorial: How to create CSS image rollovers with a single background image</title>
		<link>http://www.annedorko.com/blog/css-tutorial-how-to-create-css-image-rollovers-with-a-single-background-image</link>
		<comments>http://www.annedorko.com/blog/css-tutorial-how-to-create-css-image-rollovers-with-a-single-background-image#comments</comments>
		<pubDate>Wed, 13 Jun 2007 07:26:53 +0000</pubDate>
		<dc:creator>Anne</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.annedorko.com/?p=44</guid>
		<description><![CDATA[<p>Everybody is looking for lightweight code with a maximum effect. JavaScript rollovers just don’t cut it anymore- they take too long to load, they’re kinky the first time you rollover, etc. etc. and CSS using multiple images can be kinky if you aren’t using an image pre-load. The solution? CSS image rollovers using only a single background image.</p>
<p>If you don’t have a solid understanding of <a title="CSS tutorial: using lists for navigation" href="../blog/css-tutorial-how-to-use-lists-for-navigation">using lists for navigation</a>, then you may want to review that concept before tackling the single-background-image rollover.</p>
<p><a href="http://www.annedorko.com/site/wp-content/uploads/2007/06/cssSprites.zip">Download the example to follow along.</a></p>
The concept
<p>You have, for instance, a navigation bar (vertical or horizontal, it doesn’t matter). Very simple. There are only four buttons but you can obviously change and edit this concept to fit your own site.</p>
How is it done?
<p>Well this is what the HTML in the example ... <a href="http://www.annedorko.com/blog/css-tutorial-how-to-create-css-image-rollovers-with-a-single-background-image">Read More &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Everybody is looking for lightweight code with a maximum effect. JavaScript rollovers just don’t cut it anymore- they take too long to load, they’re kinky the first time you rollover, etc. etc. and CSS using multiple images can be kinky if you aren’t using an image pre-load. The solution? CSS image rollovers using only a single background image.</p>
<p>If you don’t have a solid understanding of <a title="CSS tutorial: using lists for navigation" href="../blog/css-tutorial-how-to-use-lists-for-navigation">using lists for navigation</a>, then you may want to review that concept before tackling the single-background-image rollover.</p>
<p><a href="http://www.annedorko.com/site/wp-content/uploads/2007/06/cssSprites.zip">Download the example to follow along.</a></p>
<h3>The concept</h3>
<p>You have, for instance, a navigation bar (vertical or horizontal, it doesn’t matter). Very simple. There are only four buttons but you can obviously change and edit this concept to fit your own site.</p>
<h3>How is it done?</h3>
<p>Well this is what the HTML in the example looks like (using lists, of course):</p>
<pre>&lt;ul&gt;
&lt;li id="home"&gt;&lt;a href="index.htm"&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li id="about"&gt;&lt;a href="pages/about.html"&gt;About &lt;/a&gt;&lt;/li&gt;
&lt;li id="portfolio"&gt;&lt;a href="pages/portfolio.html"&gt;Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li id="contact"&gt;&lt;a href="pages/contact.html"&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre>
<p>What? Impossible? Not so much.</p>
<p>When I first learned this technique, I was not aware that you can position the background image by location and pixels. It’s all very controllable through CSS. So, moving right along…</p>
<h3>The beauty of CSS</h3>
<p>So what does it take to make this work? Well this is what the CSS looks like in the example to make this turn out right:</p>
<pre>* {
 margin: 0;
 padding: 0;
 }
 body {
 background: #1B78E2;
 }
 ul {
 background: url(nav-sprite.jpg) no-repeat;
 width: 600px;
 height: 50px;
 list-style: none;
 }
 li a {
 display: block;
 width: 150px;
 height: 50px;
 text-indent: -999px;
 float: left;
 }
 li#home a:hover, li#home a:active {
 background: url(nav-sprite.jpg) 0 -50px;
 }
 li#about a:hover, li#about a:active {
 background: url(nav-sprite.jpg) -150px -50px;
 }
 li#portfolio a:hover, li#portfolio a:active {
 background: url(nav-sprite.jpg) -300px -50px;
 }
 li#contact a:hover, li#contact a:active {
 background: url(nav-sprite.jpg) -450px -50px;
 }</pre>
<p>First of all, I’ve set the padding and margin to 0 by using the wildcard * command. That way it will be more likely to look correct in every browser.</p>
<p>Then I set the width and height of the entire navigation by setting the ul.</p>
<p>Next, I set the commonalities of every li: display block (allows you to get rid of the text later as well as the width and height), set width and height, as well as using text-indent to move the ugly text off the page so we can see the pretty typography of the navigation images without losing SEO friendliness.</p>
<p>The rest of the CSS simply sets the background and position of the same background images. When setting a background position, keep in mind that the first number represents the way it moves horizontally (negative numbers will move it left, positive numbers will move it right), and the second number sets the vertical position (negative moves it up, positive moves down).</p>
<pre>li#home a:hover {
 background: url(nav-sprite.jpg) 0 -50px;
 }</pre>
<p>Be sure that you are selecting the correct link and link/hover/active/visited state for full support in all browsers.</p>
<h3>Go forth and write navigation</h3>
<p>This simple trick will help you develop quick loading sites that don’t jump or hiccup when using rollovers. No need for JavaScript or slicing and saving 25 individual rollover images! Saves production time AND creates better usability for your sites.</p>
<p>Good luck coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.annedorko.com/blog/css-tutorial-how-to-create-css-image-rollovers-with-a-single-background-image/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

