My Latest Take on Image Replacement

Published 7 years 5 months ago on October 23, 2006 — 4 min read

As you can see, the new layout still sports image based headings for much of the reoccurring content around the site. I took my usual approach of marking up and styling in that I began with bare XHTML 1.0 Strict and then added the CSS after that was totally complete. As I got to the first image-based heading, I took some time to think about a better image replacement technique.

The Problems with Image Replacement

As you may know, image replacement can be a touchy subject. Your aim should be to develop something that is equally usable by each and every person that finds their way to a site you designed and/or developed. Including extra markup that isn’t semantic didn’t seem like a path I wanted to travel, so many of the “accepted” image replacement techniques were put aside. The remaining choices left little to be desired if your reader happened to be viewing a site with images disabled and CSS enabled.

“Who does that?” you may ask. If I were to take a guess, it could be someone who is fairly Web-savvy and has the ability to use their browser in that particular fashion because they’re at a relatives house and stuck on a choking dial-up connection. That’s just one of the possibilities, and I’m sure if we all put a few minutes into thinking about it, we could think of another situation in which we wanted to simply disable images only and get straight to the content.

An Idea was Born

As I thought about it, I went in a direction that others have gone before, in that the actual text wasn’t removed from display all together, it was merely camouflaged with the background. I stuck with that technique because removing the words from display as step one doesn’t let you get much further with images disabled.

The trouble with camouflaging the text is that most of the time, disabling images simply leaves the ‘invisible’ text on screen. The text proves to be useless to a reader unless they disable CSS as well. There had to be a better way.

And That’s When it Came to Me

It was at that point I began testing with a structured block element by giving a heading a specific height and width. I gave that element a background-color that contrasted with the color in order to make the text visible again. Once that was complete, everything else seemed to fall into place.

Since I had a block element with a height and width, I was able to cut out my desired image for replacement, and simply include it as the headings background-image. A crucial step, however, is to leave yourself enough real estate to work with when cutting out the image. What I mean by that is cutting out an extra couple dozen pixels at the top of the image, because that is where your text will reside. The next step is to give the heading a color that matches the background color of the site (in order for the text to camouflage). The last step is simply shrinking the font-size of the heading so that our content resides in the real estate we set aside.

To provide a visual example, here’s what results:

Image of mouse highlighting camouflaged text

You can recreate just that on any of the image based headings around the site. If you disable images, the black background appears and the text is now visible, albeit small. Go ahead and bump the font size up a couple notches, the technique survives it for a bit. I don’t think that many readers would feel the need to bump font size three or four levels, do you?

Obvious Limitations to the Technique

As with almost everything, there are certain limitations to using a technique such as this. First and foremost, it won’t really work with anything but a solid color background. Secondly, if a user were to increase their font size a few jumps, your text will begin to overlap the images you created and look like quite a big mess. Lastly, first time (and some repeat) readers will see that stark background color you gave your heading and it may take them by surprise. Once your CSS and images are cached the problem shouldn’t continue, but some people browse with their cache completely disabled. Something like this might prove to be annoying to readers such as those.

Another thing that could be considered a limitation is the extra space you have to leave at the top of every heading. Again, this was something that just fell into place with my design, I wanted the extra margin there as it was. This method of image replacement won’t be the end all for other techniques; it just seemed to fit my goal set quite nicely.

How do you Feel About the Technique?

So far, I’ve had some mixed reactions to the technique. Some people think it’s semantic and innovative while others don’t like the fact that the background color appears. I can definitely see both sides, but I feel the semantic benefit of not including empty <span>s outweighs it in a way.

Find this useful? Digg it.

There's a conversation brewing

  1. Very clever, I must say Jon. I think it definitely has some potential pitfalls but it seems to be a great idea to bring to the table.

  2. @P.J.: You’re sure right about that — the applicable situations are limited, I just wanted to see if it were possible to create something that was both valid and semantic to solve the problem. I didn’t want to resort to empty spans which seems like the technique of choice, it’s valid code, but the lack of semantics gets me.

  3. I like it a lot. I’d used this technique a bit before, but never thought to have the image’s bitmapped background colour override the element’s CSS background colour, solving the CSS on/images off problem. As you say, it’s not the answer to everything, but for the right projects, it will prove very useful. Thanks!

  4. Very creative, I definitely like the idea and will use it in designs that allow for it. (solid background colors, etc).

    The best compromise of semantics and accessibility I have come across.

  5. Kudos for thinking up a new technique Jon. While this wouldn’t work for me (as I rarely use a solid background colour for anything), I can see the benefit.

    I’m not overly keen on leaving all the extra pixels around the text on the images as I prefer to use CSS to position exactly how I want things. Not that this eliminates that all together, but you would have to take into account the white space (in your example) when you’re adjusting your margins.

  6. @Matt: Yeah the limitation to having to use a solid background is substantial.

    I should have taken more care with the example image, it was more of a ‘screenshot’ style image as opposed to the actual image I’m using for the background (mine are closer to what you describe) — I’m going to update that now because I’m sure you aren’t the only person who will see it that way. Thanks for stopping by!

  7. I always use the negative text-indent technique. Never noticed any problems with it at all, does anyone know if there are any?

  8. @Fredrik: A lot of people stick with that very same method — I would have adapted that technique, but nothing is visible should images be disabled and CSS be enabled. The image disappears, but the style is still applied which hides the text.

  9. I may be wrong, but shouldn’t “alt” attribute of image be used instead?
    When browser can’t show/handle images it shows alt text instead.

  10. A nice idea you’ve got here, Jon. It’s been a while since someone has come up with a new approach to accessible image replacement with CSS, and it’s nice to know improvements are still possible.

  11. @ahwayakchih: The images aren’t included in the HTML, they’re included using CSS, so an alt attribute wouldn’t be included.

    @Steve Tucker: Thanks a lot. When I first started thinking over possibilities I wasn’t too convinced I would come up with anything as there have been many others before me who put very intelligent thought into ways around this issue. While this solution isn’t globally applicable, it can work well given the required circumstances.

  12. Coloring the text to the same as the background/-image? I tried this technique a few years ago – and gave it up after resizing the text, thus overlapping the image!
    As much as it seems the current fad to cater for the sight-deficient, I too have returned to using negative margins.
    On a sideways tangent, Jon, I have built a folio using
    div#thumb li { float:left; }.
    (See link in signature for result.)
    Now I have a slight problem: It works perfectly in Mozilla – but IE7 doesn’t produce the same result. Can you know the coughhackscough I need to make this work? Your help is appreciated in advance.

  13. Sorry, left out the forward slashes on website link. Lets see if this works…

  14. 6.00am in the morning – problem fixed. Tested and tried in both IE and MF.

By all means, contribute

Leave a comment

Powered by Fusion

This article is so meta

Published October 23rd, 2006

Random article

css.php