Rethinking CSS Image Replacement

Tags:
When I say CSS image replacement, I mean taking a page element that isn't normally an image and turning it into an image. This is a very common trick and popular because of it's semantic meaningfulness and SEO benefits. A common place to use this is with a header tag.

The OLD Way


<h1 class="main-logo">
   CSS-Tricks
</h1>


h1.main-logo {
	width: 350px; height: 75px;
	background: url(images/header-image.jpg);
	text-indent: -9999px;
}


PROBLEM: If you turn CSS off, this will just degrade into text. Not a bad thing, but just because someone has their CSS turned off doesn't necessarily mean they want their images turned off too.

The NEW Way


<h1 class="main-logo">
   <a href="#">
       <img src="images/header-image.jpg" alt="CSS-Tricks" />
   </a>
</h1>


WHY THIS IS BETTER:

With CSS turned off you can still display an image. With CSS and images turned off, you will get the ALT text from the image. You can even use a different image inside than you used for your CSS image, if there is a good contextual reason to do so.

REMAINING PROBLEMS:

Neither of these techniques are perfect yet. While the latter solves one piece of the puzzle, there is still one missing piece. That is CSS ON, images OFF. In this scenario you will get blank space instead of either text or an image. Not great. The other issue is turning these elements into links. You can wrap the header tag in an anchor element, but wrapping a block element in an inline element is bad form. Make sure you change your anchor link to block level if you do this. The other option is a javascript onClick event.

NOTE:

I'm sure this has been thought of and used by people before, so "old" and "new" are kind of subjective here.

[EXAMPLE PAGE]

Thanks to Volkan Görgülü for the idea!
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

TETRIS replied on Fri, 2008/03/14 - 10:39pm

What happens to the SEO value now that you took out your actual copy of "CSS-Tricks" and replaced it with an image?

Does the ALT tag have the same value as the actual copy within the H1?

 

 

Chris Coyier replied on Sat, 2008/03/15 - 10:24am

I'm afraid I just don't know. I would hope that search engines are smart enough to understand and give the same value to the ALT text as they would regular text. I think good content is the best SEO anyway...

Chris Coyier replied on Sun, 2008/03/16 - 8:45pm in response to:

@John: That's pretty smart I've never seen that technique before. I like the idea of keeping the alt text empty so you don't get the repeat text with css-off / images-off. With this technique, in the css-off / images-on scenario, you will get both the image and the preceding text, which is a little awkward. My example above handles the css-off / images-on pretty well I think, but it fails with css-on / images-off (it just leaves a white blank). Your technique is the first I've seen that handles that scenario nicely. The relative positioning on the h1 tag might need some work though. In an article with multiple h1 tags, it looks like they would stack all on top of each other at the top of the page. Not a problem if you are just using it for a logo or something though.

Ryan Mathis replied on Wed, 2008/03/19 - 10:30pm

Well the whole point in using image replacement is to seperate content from style. Including the image in the heading is pointless because it's not adding anything meaningful for the user. If someone has css turned off it's probably because all they want to view is the content. And if they are using a mobile device; it's really annoying to have to wait for an image to load just to see what the page is about.

Anyway, your users will appreciate you not forcing your personal tastes upon them.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.