A while back, I wrote an article demonstrating how the popular “accordion” effect (as in the image above) could be replicated with nothing more than CSS. There was one caveat, however: the technique didn’t work in Internet Explorer 6 due to the browser’s extremely limited support of the :hover pseudo-element.
Now, IE6’s market share is decreasing day by day – and we’re all undoubtedly thankful. But the browser will continue to maintain a healthy market share for quite some time to come. To that end, I’d like to revisit the CSS accordion technique and make a modification or two that will let it work with IE6.
I’m going to admit up front: these modifications require JavaScript. So what’s the point of having a “CSS” accordion technique that uses JavaScript? Well, all those other accordion techniques out there require JavaScript, and almost all require an entire JavaScript framework. This technique is merely improved by JavaScript – all modern browsers will handle this technique just fine even without JS enabled.
The Original Markup
We’re going to have to make a couple of teensy modifications to the CSS later on, but I wanted to show the original markup for consistency’s sake. Here’s the XHTML we originally used:
<div id="accordion"> <div id="part1"> <p>This text is in part 1.</p> </div> <div id="part2"> <p>This text is in part 2.</p> </div> <div id="part3"> <p>This text is in part 3.</p> </div> <div id="part4"> <p>This text is in part 4.</p> </div> </div>
And here is the CSS:
#accordion {
width: 500px;
margin: 100px auto; }
#accordion div {
float: left;
width:25%;
height: 300px;
overflow: hidden;}
#accordion:hover div {
width: 20px; }
#accordion:hover div:hover {
width: 440px;
overflow: auto; }If you’d like to better understand how all of this works, please refer to the original article.
Better Living Through JavaScript
The idea on how to improve this script came to me one day while I was going through the CSS Newbie archives (yes, I have to refer to my own archives to remember how to do things some days!). I was rereading my article on Easy CSS Dropdown Menus and I realized that the same JavaScript that allows my dropdown menus to be hover-able (that is, the Suckerfish technique) would work just as well on my CSS accordion. Here’s that code modified to effect the accordion:
accHover = function() {
var accContainer = document.getElementById("accordion");
accContainer.onmouseover=function() {
this.className+=" hover";
}
accContainer.onmouseout=function() {
this.className=this.className.replace(new RegExp(" hover\b"), "");
}
var accDivs = accContainer.getElementsByTagName("div");
for (var i=0; i<accDivs.length; i++) {
accDivs[i].onmouseover=function() {
this.className+=" hover";
}
accDivs[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" hover\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", accHover);Basically, this code loops through all the divs within the accordion, as well as the containing accordion div itself, and applies a mouseover and mouseout function to each. Then when the user mouses over the div, it gains a class of “hover”. When the user moves the mouse away from the div, the hover class is removed.
Now all we need to do is edit our CSS to look for either the hover pseudo-class or our new hover class, which is made easy with sequential selectors:
#accordion {
width: 500px;
margin: 100px auto; }
#accordion div {
float: left;
width:25%;
height: 300px;
overflow: hidden;}
#accordion:hover div, #accordion.hover div {
width: 20px; }
#accordion:hover div:hover, #accordion.hover div.hover {
width: 440px;
overflow: auto; }Even Easier with jQuery
Those of you who have been reading for a while know that I’m a big fan of the jQuery JavaScript framework. Thus, when I started to rewrite this script to work for our accordion, I decided to rewrite it in jQuery as well. For those of you who aren’t currently using jQuery, this script might be a bit overkill, as it’d require your users to download a lot more code in the long run (our tiny script plus the jQuery framework). But if you’re already using jQuery, this cuts our heavy lifting down by quite a bit:
$(document).ready(function() {
$("#accordion, #accordion div").mouseover(function() {
$(this).addClass("hover");
}).mouseout(function() {
$(this).removeClass("hover");
});
});This script does the same thing as our longer JavaScript above, but with far less code.
So that’s all there is to it! You can see a demo of this in action here. The accordion should function just fine in all major browsers with our without JavaScript enabled, and will function in IE6 with JavaScript. Enjoy!



On December 17, 2008
9:52PM
Max said:
I may be crazy, but I don’t see a demo link. :)
On December 17, 2008
10:18PM
imacrazy2 said:
I canna see the blasted demo link, neither!
On December 17, 2008
10:28PM
Rob Glazebrook said:
Crud! My bad, gentlepersons. The link has been added now. Sorry for the delay.
On December 19, 2008
4:39AM
elvisparsley said:
The first my reactio to the demo is that it needs a smooth animation touch so that it will be more attractive and elegant. Then I remember that this site is about CSS. But I found you are using jquery. So why not using it.
Thanks.
On December 19, 2008
1:17PM
Chris said:
Honestly, that sucks. It’s a good proof of concept but it looks horrendous and shouldn’t be used on any sort of site. It’s awkward to go from one box to the other.
On December 19, 2008
1:37PM
Rob Glazebrook said:
Honestly, “that sucks” is terrible feedback. The rest I can appreciate more.
I’m not a designer — as I reiterate roughly every third article here. :) And I’m generally not trying to produce stuff for people to copy and paste verbatim. I’m trying to produce stuff from which people can learn.
So if it’s a good proof of concept, that’s good… because that’s all I was going for!
On December 19, 2008
5:57PM
rohnn said:
Hey Rob.
I followed from your post from cssglobe.
Why you call it “Advanced CSS Accordion Effect” since it is JQuery based ???
Can it run without JQuery ?
And why not use some smooth sliding ? Wont hurt for sure.:)
Furthermore, I dont feel it is really usable…Would be much more on click than hover.
On December 20, 2008
3:05AM
Hassan said:
I hate to use JS! The hover problem in ie6 can be fixed with Whatever:hover htc file easily. This behavior enables the :hover pseudo-class in ie. I think it’s good, cause, it’s light-wight, other browsers won’t load this file (without any comment hack), and it doesn’t require user to be javascript-enabled.
Thanks for your great post!
On February 16, 2009
8:02PM
Mike From Cincinnati Ohio said:
I like the effect, I would really like to see a more expanded version, one that has some options for the built in jquery animation. Superfish animation as well as suckerfish animation is nice. I am an avid follower of this site and I like the stuff you do. Keep up the good work.
On July 04, 2009
2:56AM
Silver Firefly said:
Guys, he tried to teach you the CONCEPT of a CSS-based accordion. It’s up to you guys if you want a pretty sliding animation to implement it yourselves.
On July 06, 2009
5:09PM
Mara Alexander said:
The demo is *not* working in IE6 (at least for me), and in Firefox 3.5 this looks horrid. So bad I don’t see how you can offer it as a replacement of JS-driven accordions. It doesn’t come close.
On December 02, 2009
10:57AM
Kalli said:
I really like your menu, but i can ‘t work out how the menu with JS but without jQuery works. Is there a demo online? The panels doesn’t seem to close…