CSS image switcher (done the right way)

Chris Coyier has done it again. Compelled me to stay up all night, that is (it's 7am as I type this). In Chris's latest screencast, CSS Image Switcher, he demonstrates how to create an "image switcher" using CSS. The problem, though, is that his process is wrong.

Incorrect process

  1. What effect or experience do I want to create?
  2. How can I achieve this using CSS (and JavaScript if necessary)?
  3. What can my markup do to help me?

Correct process

  1. What effect or experience do I want to create?
  2. What is the most correct and meaningful way to describe the content?
  3. How can I achieve the desired effect or experience (or something close to it) without altering my markup?

Chris simply isn't in the right mindset. He's thinking about how he's going to present the content on the page, but he's forgetting to consider the content itself. This is a crucial error. If one views Chris's CSS image switcher demo with styles disabled, one sees something most unhelpful:

Chris Coyier's CSS image switcher demo displays nothing but four unhelpful links when styles are disabled
CSS image switcher demo as seen at CSS-Tricks (with styles disabled; note the lack of images)

Well, at least one could click on these links to view the images, right? Wrong! The hrefs contain nothing but the hash sign. Chris does mention in the screencast that these links could go somewhere, but it's as if to say hey, here're some links if you need 'em rather than yikes! this page'll be worthless when styles are disabled, you'd sure as hell better link to the images as backup.

I assume that Chris opted to use anchor elements to allow him to employ the :hover pseudo-class and have it work in IE6. It's well and truly time, however, to stop bending over backwards to accommodate IE6's shortcomings, and Google agrees: YouTube to drop support for IE6.

Why meaningful markup matters

Meaningful HTML is not just important to markup geeks who like to use the word "semantic". It's vitally important to search engines. While in most cases discussion of the relationship between structured content and search engines centres on whether Google favours sites with clean, descriptive markup, in the case of Chris's CSS image switcher it's a matter of whether the content is indexable at all! Consider the consequences of a site such as Flickr adopting this approach: any new photo uploaded to the site would be invisible to web crawlers.

One should not rely on either CSS or JavaScript to deliver content – HTML exists to contain and describe content. This is a pragmatic consideration as well as an idealogical one: HTML is the one component of the HTML+CSS+JavaScript stack to which all users have access (certain devices, for example, eschew CSS support in favour of faster page loads).

Meaningful markup for image–caption pairs

As I state in Captions over images, my response to an earlier CSS-Tricks screencast, the definition list is the most appropriate tool in our toolbox.

Definition list markup
The screenshot above is marked up within a dt and this caption is a dd (how meta!)

Each term in a definition list should have at least one definition. This enables more than one description — a title and a caption, for instance — to be tied to an image:

<dl>
    <dt><img src="images/ds81.jpg" alt="Vancouver architecture" /></dt>
    <dd>This is not like that</dd>
    <dd>Contrasting ideas engage the mind. Make a photo today that tells
    a story with contrasting elements. — <i>Daily Shoot 81</i></dd>
    ...
</dl>

Beautiful, isn't it? No pointless (literally) anchors, no meaningless ids, no class names, just content wrapped in descriptive HyperText Markup Language. This ensures that the page's content is accessible to all: those viewing the page on mobile phones; those using screen readers or feed readers; and, of course, web crawlers.

Meaningful markup degrades gracefully
The page's content is accessible even with styles disabled

Demo

Check out the CSS image switcher demo to see what's possible with straightforward, structured markup (and some rather less straightforward CSS). The demo works in the latest versions of Firefox, Internet Explorer, Opera, and Camino. Unfortunately the core feature does not work in Safari or Google Chrome, as these WebKit-based browsers fail to handle the dt+dd:hover~dt selector.

Comments

While I like your approach, it's not done the right way. It's done in what YOU believe is the right way (and mostly what I do to, for that matter).

However, to me, not supporting webkit, is not the right way.

That being said, It's great in firefox. Well done. Now if only webkit worked with that selector.

Jason

I agree with your point of process but didn't we now choose to exclude two browsers on point of principle? I mean now Safari and Chrome cannot use it, it didn't fall back to a non-CSS version automatically. But, the mark-up is correct.

I didn't know about the multiple dd for a dt - thats good to know.

And I agree with dropping support for IE6. I do it where ever I can get away with it. Sadly, a good chunk of the city of L.A. uses a few of the sites I manage and the city still has too many PCs on IE6 for me to discount it. Looking forward to better days…

Mike

Chris's screencast had an idealistic motive: to achieve with CSS something generally done with JavaScript. I, too, set out to find a CSS-based solution, but unlike Chris I did not allow myself to be thrown off course by pragmatic considerations.

Let me make it clear that I'm not advocating the use of this approach at this point in time, given that the combination of hover and general sibling selector fails in WebKit. While JavaScript could supplement the CSS to make the "switcher" work in Safari and Chrome, this would undermine the goal of avoiding dependence on JavaScript.

For now, I recommend a JavaScript-based solution. It is nice to know that a CSS-based approach will be feasible in the future, although how far in the future remains to be seen given that two years have passed since the WebKit bug was reported.

While I applaud you for diving deeper into the standards argument, I must say losing cross browser support is absolutely WRONG. IE6, while Google has chosen not to support it, is still to widely used. Not to mention this only works in webkit browsers. While Chris's method may not meet standards, any good site out there that wants to accomplish pixel perfect cohesion between all browsers must break some rules. Knowing how to break the standards rules only when it's required and being fully aware of the consequences separates good front end designers from the bad ones.

That said, I wish all browsers did follow standards and breaking the rules wasn't necessary.

Nick

I set out to discover what is currently possible using nothing more than a layer of CSS on top of well-structured content. Were it not for the WebKit bug mentioned, this CSS-only approach would work in all modern browsers. As I've stated, I do not consider the approach fit for consumption given that it does not work in Safari or Chrome.

While I understand your desire, Nick, to provide a rich experience for all users (not just those with modern browsers), I maintain that deformed markup is an unacceptable — and unnecessary — price to pay. Instead, JavaScript could be used — with or without the help of a library such as jQuery or MooTools — to provide a single solution for (virtually) all desktop browsers.

Maybe at http://www.w3schools.com/browsers/browsers_explorer.asp you can see that IE6 is 6.7%, and think it's not much… Have GoogleAnalytic install and review YOUR multiple website stats

My most view website (over 20000/y) REAL people do use IE6 for 20% of the viewer audience. That's A LOT of people. If your a geek that have a PC (sorry for you) or a mac (happy for you) in the last year, great, you will see all those html5/css3/flashy website the "correct" way, all the other, maybe not....

And when your are in a big company, with software install remotely and control by administrator, you live with the stuff you have, and maybe you will run w2k with IE6.

So for people who want to design for a large audience, get IE6 work correctly, and don't think everyone have chrome of FF, and for those who design for the elite geek, go play with your iPhone and live in your bubble !

Thanks for sharing… here another ideas! http://www.raghibsuleman.com/css-image-switcher

This is great indepth Show of CSS. Kindly provide the code for it if you like sharing.

Sam

Respond