{"authorId":"1","title":"Improved Navigation Image Replacement","slug":"improved-navigation-image-replacement","postDate":1177720362,"expiryDate":null,"enabled":"1","fields":{"2":"<p>So my co-worker at work today was slicing a design, and the designer said that the navigation had to be images. Being the good little accessibility people that we were, we were trying to figure out a way to use images, but have text also show up when images are disabled.<\/p>\n\n<p>So my co-worker went with the quick fix (for now), a JavaScript onmouseover solution, and I told him that it was lame. I told him there had to be a way to use CSS to do it. I didn't have time to do it at work, so I played with it, and I have found a solution.<\/p>\n<!--more-->\n<h2 class=\"replaceHeadline\">The Markup<\/h2>\n\n<p>Ok, so everyone knows to mark-up navigation in an unordered list:<\/p>\n\n<div class=\"codeblock\"><code>\n&lt;ul&nbsp;id=\"nav\"&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;\/a&gt;&lt;\/li&gt;<br \/>&lt;\/ul&gt;&nbsp;\n\n<\/code><\/div>\n\n<p>Now, instead of placing an image in the anchor tag and then using JavaScript to change the hover state, I thought, why not just add an extra <code>span<\/code> at the end of the anchor tag. I chose the end, but it also works with it at the beginning. I think it makes more sense to have it at the end though, so that the text within the anchor tag is read first by screen readers and bots. So this is what our markup looks like now:<\/p>\n\n<div class=\"codeblock\"><code>\n&lt;ul&nbsp;id=\"nav\"&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;span&gt;&lt;\/span&gt;&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;span&gt;&lt;\/span&gt;&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;span&gt;&lt;\/span&gt;&lt;\/a&gt;&lt;\/li&gt;<br \/>&nbsp;&lt;li&gt;&lt;a&nbsp;href=\"#\"&gt;Text&nbsp;to&nbsp;Cover&nbsp;Here&lt;span&gt;&lt;\/span&gt;&lt;\/a&gt;&lt;\/li&gt;<br \/>&lt;\/ul&gt;&nbsp;\n\n<\/code><\/div>\n\n<h2 class=\"replaceHeadline\">Let&rsquo;s Style It<\/h2>\n\n<p>So I started off with just some normal styles that you would apply to a navigation: zeroing out margins and paddings, floating it, removing the list-styling, and giving it a width.<\/p>\n\n<p>Next, I floated the list items so that they would be in a line horizontally. This is where it finally gets interesting, I promise. I styled the anchor tag like so:<\/p>\n\n<div class=\"codeblock\"><code>\nul#nav&nbsp;li&nbsp;a&nbsp;&#123;<br \/>&nbsp;background:&nbsp;#FFFF99;<br \/>&nbsp;display:&nbsp;block;<br \/>&nbsp;height:&nbsp;30px;<br \/>&nbsp;padding:&nbsp;0&nbsp;5px;<br \/>&nbsp;width:&nbsp;115px;<br \/>&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>Since this is just an example, I put a random background color on (to make sure it didn&rsquo;t show through in the final example), and I gave them all the same width. Not a likely situation, but I didn&rsquo;t feel like giving each list item an id. That should be self explanatory enough.<\/p>\n\n<p>Now, when I think back to the talk that <a href=\"http:\/\/meyerweb.com\">Eric Meyer<\/a> at <a href=\"http:\/\/aneventapart.com\/events\/boston07\/\">An Event Apart Boston<\/a>, he kept stressing that to a browser, an element is just an element, and you can do anything to it. So, my plan was to just set the anchor tag to be relatively positioned so that it would contain the <code>span<\/code>.<\/p>\n\n<p>The next step is to add the background image to the <code>span<\/code>. Now one thing that needs to be realized is that you cannot use a transparent image. But I don&rsquo;t think that really causes much of a problem in most cases.<\/p>\n\n<div class=\"codeblock\"><code>\nul#nav&nbsp;li&nbsp;a&nbsp;span&nbsp;&#123;<br \/>&nbsp;background:&nbsp;url(\/images\/content\/2008\/01\/nav.gif)&nbsp;no-repeat&nbsp;0&nbsp;-30px;<br \/>&nbsp;cursor:&nbsp;pointer;<br \/>&nbsp;display:&nbsp;block;<br \/>&nbsp;height:&nbsp;30px;<br \/>&nbsp;left:&nbsp;0;<br \/>&nbsp;position:&nbsp;absolute;<br \/>&nbsp;top:&nbsp;0;<br \/>&nbsp;width:&nbsp;125px;<br \/>&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>The only things to note from that is that I combined the normal and hover states into one awesome image (I wasn&rsquo;t worried about how pretty it looked). I also had to add the cursor property for our best friend, IE6.<\/p>\n\n<p>Ok, cool. We are on our way. Now we just need to shift the background image on the hover state:<\/p>\n\n<div class=\"codeblock\"><code>\nul#nav&nbsp;li&nbsp;a:hover&nbsp;span&nbsp;&#123;&nbsp;background:&nbsp;url(\/images\/content\/2008\/01\/nav.gif)&nbsp;no-repeat&nbsp;0&nbsp;0;&nbsp;&#125;&nbsp;\n\n<\/code><\/div>\n\n<p><strong>Voil&agrave;!<\/strong> It works like a charm. <a href=\"http:\/\/trevordavis.net\/play\/imagereplacement\/\">Check out the example<\/a>.<\/p>\n\n<h3 class=\"replaceHeadline\">But&hellip;<\/h3>\n\n<p>Did you check it in IE6? When I checked it in Firefox and IE7, everything worked beautifully. You can turn off the images and you get the text underneath.<\/p>\n\n<p>When you do check it in IE6, you will notice that the hover states stay on. It&rsquo;s very odd.<\/p>\n\n<p>After a lot of tinkering, I finally found something that worked. If you add the following to the anchor when it is in its hovered state, it for some reason fixes it:<\/p>\n\n<div class=\"codeblock\"><code>\nul#nav&nbsp;li&nbsp;a:hover&nbsp;&#123;&nbsp;background:&nbsp;0&nbsp;0;&nbsp;&#125;&nbsp;\n\n<\/code><\/div>\n\n<p>I have no explanation, but it does not seem to have any adverse affects on other browsers.<\/p>\n\n<h3 class=\"replaceHeadline\">Let&rsquo;s Bulletproof It<\/h3>\n<p>We can add a simple property to the anchor tag so that when someone resizes their text, it does not poke out from under the image:<\/p>\n\n<div class=\"codeblock\"><code>\nul#nav&nbsp;li&nbsp;a&nbsp;&#123;<br \/>&nbsp;background:&nbsp;#FFFF99;<br \/>&nbsp;display:&nbsp;block;<br \/>&nbsp;height:&nbsp;30px;<br \/>&nbsp;overflow:&nbsp;hidden;&nbsp;\/*Added&nbsp;for&nbsp;bulletproofing*\/<br \/>&nbsp;padding:&nbsp;0&nbsp;5px;<br \/>&nbsp;position:&nbsp;relative;<br \/>&nbsp;width:&nbsp;115px;<br \/>&#125;&nbsp;\n\n<\/code><\/div>\n\n<p><a href=\"http:\/\/trevordavis.net\/play\/imagereplacement\/final\/\">Check out the final example.<\/a><\/p>\n\n<h3 class=\"replaceHeadline\">More Bulletproofing<\/h3>\n\n<p>If you wanted the text to resize gracefully when images are disabled, I suppose you could set your height in ems. Then you would just need to build some extra blank space into your image, so that it would work when images are enabled.<\/p>\n\n<p>So hopefully that helps out my co-worker. I think it potentially solves a pretty big problem with CSS image navigation with images disabled.<\/p>","14":{"old_id":{}}}}