<?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>Learning the World &#187; image swapping</title>
	<atom:link href="http://learningtheworld.eu/tag/image-swapping/feed/" rel="self" type="application/rss+xml" />
	<link>http://learningtheworld.eu</link>
	<description></description>
	<lastBuildDate>Tue, 06 Nov 2012 00:17:33 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Foreground Sprites</title>
		<link>http://learningtheworld.eu/2007/foreground-sprites/</link>
		<comments>http://learningtheworld.eu/2007/foreground-sprites/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 13:00:55 +0000</pubDate>
		<dc:creator><![CDATA[Martin Kliehm]]></dc:creator>
				<category><![CDATA[accessibility]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[DOM scripting]]></category>
		<category><![CDATA[foreground sprites]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[image swapping]]></category>
		<category><![CDATA[img element]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mouseover]]></category>
		<category><![CDATA[rollover]]></category>

		<guid isPermaLink="false">http://learningtheworld.eu/2007/foreground-sprites/</guid>
		<description><![CDATA[Most rollovers have become obsolete because they can be performed on background images with <strong><acronym title="Cascading Style Sheets">CSS</acronym> sprites</strong>. However, there are those rare cases when there is just an icon without text, like a &#8220;play&#8221; or &#8220;pause&#8221; button. This article discusses how to apply <acronym>CSS</acronym> sprites for foreground images.&#160;[&#8230;]]]></description>
				<content:encoded><![CDATA[<p class="alert"><ins datetime="20080626T200000">Please consider a better solution with <a href="/2008/better-foreground-sprites/">Better Foreground Sprites</a>.</ins></p>

<p>Most rollovers have become obsolete because they can be performed on background images with <strong><a href="http://www.alistapart.com/articles/sprites/"><acronym title="Cascading Style Sheets">CSS</acronym> sprites</a></strong>. If there are hover effects today, they usually come as text on a button background image, or as a text link with an icon next to it.</p>

<p>However, there are those rare cases when there is <strong>just an icon without text</strong>, like a &ldquo;play&rdquo; or &ldquo;pause&rdquo; button, or a magnification glass on an image to signal it can be enlarged. If you want to swap images when the element receives focus or on mouseover, traditionally you are stuck with two options:</p>

<ol>
<li>JavaScript to change the image source, or</li>
<li><acronym>CSS</acronym> sprites to change the position of a background image behind a transparent <code>gif</code> image.</li>
</ol>

<p>I like neither. Using JavaScript and two images with pre-loading seems like a waste of resources when it can be done with <acronym>CSS</acronym> and one image only. Transparent gifs are <em>so</em> 1998, besides the background usually doesn&rsquo;t get printed. So what we need on these occasions are <strong><acronym>CSS</acronym> sprites for foreground images</strong>.</p>

<p>Until recently I thought that can&rsquo;t be done, but then I stumbled upon some code for <a href="http://download.dojotoolkit.org/release-0.9.0beta/dojo-0.9.0beta/dijit/tests/form/test_Checkbox.html">checkbox widgets in Dojo</a>. Naturally they use a lot of JavaScript for the task. Anyway I like my default checkboxes and don&rsquo;t have any urge to make them prettier (I have no doubt our design overlords would enforce <em>aqua</em> icons everywhere), but that made me think. Here&rsquo;s a suggestion:</p>

<p>First a simple sprite which is 50px wide: <img src="/examples/img/icon-enlarge.gif" width="50" height="15" alt="Two icons of a magnification glass" class="example sprite" /></p>

<p>The <acronym title="Hypertext Markup Language">HTML</acronym> code uses the dimensions of a <em>single</em> icon for width and height to allow fast page rendering without reflow:</p>

<ol class="code">
<li><code>&lt;a href=&quot;/big/&quot; class="icon"&gt;</code></li>
<li class="indent"><code>&lt;img src=&quot;/img/icon-enlarge.gif&quot; id=&quot;magnifier&quot; <strong>width=&quot;19&quot; height=&quot;15&quot;</strong> alt=&quot;enlarge image&quot; /&gt;</code></li>
<li><code>&lt;/a&gt;</code></li>
</ol>

<p>The corresponding <acronym>CSS</acronym> sets the link to <code>display: block</code>, uses the single icon dimensions for the container, prevents overflow, and resets the image dimensions to <code>auto</code> to prevent distortion. The actual image swapping is achieved by a negative left margin on hover.</p>

<ol class="code">
<li><code>a.icon {</code></li>
<li class="indent"><code><strong>display: block;</strong></code></li>
<li class="indent"><code><strong>width: 19px;</strong></code></li>
<li class="indent"><code><strong>height: 15px;</strong></code></li>
<li class="indent"><code><strong>overflow: hidden;</strong></code></li>
<li><code>}</code></li>
<li><code>a.icon img {</code></li>
<li class="indent"><code><strong>width: auto;</strong></code></li>
<li class="indent"><code><strong>height: auto;</strong></code></li>
<li class="indent"><code>border: none;</code></li>
<li><code>}</code></li>
<li><code>a.icon:hover img, a.icon:focus img, a.icon img.hover {</code></li>
<li class="indent"><code><strong>margin-left: &minus;31px;</strong></code></li>
<li><code>}</code></li>
</ol>

<p>For the sake of simplicity I ignored how the link would be floated or positioned. Also borders, a background color, or padding to increase the clickable area can be added to the parent element.</p>

<p>For simplicity I chose a link in the example, but the same can be done with a <code>div</code> surrounding an <code>&lt;input type=&quot;image&quot;&nbsp;/&gt;</code>. Then of course you need to define <code>cursor: pointer</code>, and for anything below <acronym title="Internet Explorer">IE</acronym>7 bind the <acronym title="Document Object Model">DOM</acronym> event to the hovering element (<a href="/examples/js/foreground-sprites-ie6.js" type="text/javascript" title="JavaScript source code to fix Internet Explorer">get the source</a>):</p>

<ol class="code">
<li><code>&lt;!--[if lt IE 7]&gt;</code></li>
<li><code>&lt;script type=&quot;text/javascript&quot;&gt;</code></li>
<li><code>oIcon = {</code></li>
<li class="indent"><code>setHoverClass : function() {</code></li>
<li class="indent"><code><span class="indent"><strong>this.className = &#x27;hover&#x27;;</strong></span></code></li>
<li class="indent"><code>},</code></li>
<li class="indent"><code>resetHoverClass : function() {</code></li>
<li class="indent"><code><span class="indent"><strong>this.className = &#x27;&#x27;;</strong></span></code></li>
<li class="indent"><code>}</code></li>
<li><code>};</code></li>
<li><code>var elm = document.getElementById(&#x27;magnifier&#x27;);</code></li>
<li><code>if (elm) {</code></li>
<li class="indent"><code>elm.onmouseover = elm.onfocus = oIcon.setHoverClass;</code></li>
<li class="indent"><code>elm.onmouseout = elm.onblur = oIcon.resetHoverClass;</code></li>
<li><code>}</code></li>
<li><code>&lt;/script&gt;</code></li>
<li><code>&lt;![endif]--&gt;</code></li>
</ol>

<p><strong>Here is a working example</strong>. You know how to adjust the code if you need more than one class on the image.</p>

<p><a href="#" class="icon"><img src="/examples/img/icon-enlarge.gif" id="magnifier" width="19" height="15" alt="enlarge image" /></a></p>

<p>That was nice and easy. It&rsquo;s tested in Firefox&nbsp;2, Opera&nbsp;9, Internet Explorer 5+, and Safari&nbsp;2. Screen readers should be okay with the <code>alt</code> text.</p>

<h3>Discussion</h3>

<p>The trouble begins when people <strong>start turning off browser features</strong>.</p>

<p>People with <strong>disabled images</strong> won&rsquo;t be able to read the alternative text because the visible area is limited by the parent container. That can be detected by comparing the known sprite size with the <code>alt</code> text size. Then the class is set to anything but <code>icon</code> so that the parent container expands.</p>

<p>More severe is when people <strong>disable style sheets</strong> because then they will see a squeezed image with multiple icons. If you are hyper-correct, you could replace the crushed multi-icon image with the correct single icon (you could use the <code>class</code> or <code>rel</code> attribute to define the icon type), but nobody wants to slice images. As a compromise I would suggest to replace the icon with its <code>alt</code> text.</p>

<p>See the <a href="/examples/js/foreground-sprites-access.js" type="text/javascript" title="JavaScript source code for accessibility">script</a> to detect disabled images or <acronym>CSS</acronym>. (Note: it isn&rsquo;t implemented here because I couldn&rsquo;t test it properly on <acronym>IE</acronym>6 with stuff turned off.)</p>

<p>If JavaScript is unavailable, the user is stranded with what we gave him. Also <strong>icons without text are inherently evil</strong> because they can&rsquo;t be enlarged by zoom readers. Did I forget any politically correct disclaimer for obscure use scenarios?</p>

<p>So we ended up with a <acronym>CSS</acronym> only version, but still some JavaScript is needed for the extra effort to provide the same experience to users of <acronym>IE</acronym> 5-6, and some more for certain user scenarios. Wouldn&rsquo;t it be easier to slice and swap foreground images the old way? But then again slicing is a pain, and people already start using background images. <strong>Do people actually turn off <acronym>CSS</acronym> and images?</strong> Or is that another remnant of <acronym title="Web Content Accessibility Guidelines">WCAG</acronym> 1.0 from 1999?</p>
]]></content:encoded>
			<wfw:commentRss>http://learningtheworld.eu/2007/foreground-sprites/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
