Update: I’ve created an advanced version of this technique that works in IE6! Read more here.
The Accordion Effect is fast becoming one of the most commonly used (and perhaps abused?) effects of the Web 2.0 world. Most JavaScript frameworks make such an effect fairly easy to pull off – heck, MooTools even built their reputation on it in the early days. So what makes this accordion effect special? It doesn’t require a single line of JavaScript.
First off, what is an “accordion” effect? Generally speaking, the accordion effect takes several larger elements and then compresses them into a small space so that only a small portion (like a heading) of most or all of the elements is visible. Then, when the user interacts with that element — either by clicking on it or maybe only mousing over — the accordion expands so that the element of interest is visible, and the other elements shrink down automatically. When it’s in use, it looks a bit like an accordion expanding and contracting: hence the name.
Our accordion will work exactly the same way: all of the elements will be partially visible when the user loads the page. And then when they mouse over a particular section, it will instantly expand – and the other elements will contract – to make reading more easy.

So how do we accomplish this trick? First, we start with our XHTML, which just consists of a couple of divs with some IDs applied:
<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>
My first div defines where my accordion starts and ends. The divs nested inside are just parts of my accordion – they don’t even technically need IDs unless I want to style them differently. That’s all the XHTML it takes!
So now, let’s start building our accordion. We start by defining the physical limitations of our space:
#accordion {
width: 500px;
margin: 100px auto; }
All I’ve done is give my accordion a specific width and centered it in the page for a nice visual effect. Now, I have to create the default state for the divs inside of my accordion:
#accordion div {
float: left;
width:25%;
height: 300px;
overflow: hidden;}
This snippet floats all of my divs to the left and gives them a specific width and height. I’ve chosen a width of 25% because I have four elements in my accordion, so they all split that width up evenly by default. If I’d had five elements, I would have used 20%, and so on. My height of 300 pixels also becomes the height of my accordion div.
The overflow: hidden part here is also crucially important. This is what prevents my divs from either resizing to fit their content or spilling their content out of the containing div and onto our page. It also creates a nice visual effect… people will wonder what content they’re missing, and will mouse over the area to investigate.

So now that we’ve created our default state, we need to create our hover or “active” state. This requires two CSS rules. The first looks like this:
#accordion:hover div { width: 20px; }
We’re using the :hover pseudo-class here in a pretty creative way – we’re telling all of the divs inside of the div we’re hovering over to change. Specifically, we’re shrinking them all down to 20 pixels to make room for our expanded accordion section. So now we just need to make sure it expands:
#accordion:hover div:hover {
width: 440px;
overflow: auto; }
The :hover pseudo-class comes into play here again. Now, we’re applying styles to an element we’re hovering over, inside of an element we’re hovering over. We’re styling this element differently than our others by giving it a specific width – 440 pixels, i.e. 500 pixels minus the 20 pixels taken up by our other three divs – and setting its overflow to “auto.” These two classes cause our active div to expand, and then give it a scroll bar if the content is too long.
And that’s all there is to creating a CSS-only accordion box! If you’d like to see it in action, click here. The only change I’ve made to the full version is I’ve added a few background styles and some interior padding to each of the accordion sections to make them stand out and look a little more attractive.
This example creates a horizontal accordion box. But it’s just as easy to create a vertically oriented accordion. All we really need to do is eliminate the floats on our nested divs and turn most of our width tags into height tags. Here is the markup:
#accordion {
width: 500px;
height: 400px;
margin: 20% auto; }
#accordion div {
height:25%;
overflow: hidden;}
#accordion:hover div {
height: 20px; }
#accordion:hover div:hover {
height: 340px;
overflow: auto; }
And you can see it in action here.
As with any trick this cool, there are some caveats. Most significantly: this technique does not work in IE6, (update: now works in IE6! Read more here) because IE6 doesn’t support hover states on anything other than anchors. Therefore, you can make it work if you’re willing to make a sacrifice: if you wrap all of your divs in anchor tags, and then apply the hover state to those anchors instead of your divs, the technique should work the same (I haven’t tried it, though). However, that wouldn’t be particularly semantic or valid, so I’m not showing it here.
Also, if you apply any padding or borders to your #accordion div, that can cause some problems. The border and padding are part of your div, and therefore part of your :hover class. However, if you’re hovering over the containing div’s padding, you aren’t hovering over one of the sections of your accordion – meaning all of your interior divs will shrink down to their smallest size, and none will grow to fill the space. It took me half an hour of debugging to figure this one out. :)
So there you have it. Use it, share it, love it. And let me know in the comments if you come up with a particularly interesting or attractive implementation – mine is obviously pretty simple!


Someone necessarily lend a hand to make critically articles I would state.
That is the first time I frequented your web page and up
to now? I amazed with the research you made to create this particular
publish extraordinary. Magnificent process!
Oh my goodness! Amazing article dude! Thank you, However I am having issues with your RSS.
I don’t know the reason why I am unable to join it.
Is there anybody getting identical RSS issues?
Anybody who knows the answer can you kindly respond? Thanks!!
Everyone loves what you guys are up too. Such clever work aand reporting!
Keep up the awesome works guys I’ve added you guys to blogroll.
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added
I get several e-mails with the same comment.
Is there any way you can remove people from that service?
Many thanks!
Pingback: 30个CSS导航技术 | Joozo.net Web Plus
I’m really loving the theme/design of your site. Do you ever run into any internet browser compatibility
problems? A few of my blog readers have complained about my website not operating correctly in Explorer but looks great in Opera.
Do you have any recommendations to help fix this problem?
Great post with excellent information.
Thank you very much for sharing with us.
If you desire to grow your familiarity simply keep visiting
this site and be updated with the newest news posted here.
Can you get this to work on an iPad? I am trying to write a simple eBook using simple CSS – to switch from a physics question to a physics answer. each text has an image in it.
Aw, this was an extremely nice post. Taking a few minutes and actual effort to create a good article… but what
can I say… I put things off a whole lot and don’t seem to get anything done.
I really like reading an article that can make men and women think.
Also, thank youu forr allowing for me to comment!
This is the right webpage for anyone who really wants to find out about this topic.
You know so much its almost tough to argue with you (not
that I actually would want to…HaHa). You definitely put a brand new spin on a topic that has been written about for years.
Great stuff, just wonderful!
If some one desires expert view regarding blogging and site-building afterward i suggest him/her to pay a
quick visit this blog, Keep up the nice job.
You could definitely see your expertise in the article you
write. The arena hopes for more passionate writers like you who aren’t afraid to mention how they believe.
At all times go after your heart.
Término extraídо del lenguaje ecuestre, ԛue significa
montar a caballo ѕin silla.
my site primer anal doloroso
Hello, i believe that i saw you visited my website thus i got here to return the desire?.I am attempting
to to find things to improve my web site!I guess its ok
to use a few of your ideas!!
The Notifier attributes comparable functionality with no getting the added capability of running apps on the watch itself.
Lo vamos a hacer igual que antes, mechón a mechón, y aplicando fluido de queratina en cada mechón, para resguardarlo, ya antes de ponerle la plancha.
What’s up, every time i used to check web site posts here early in the dawn, for the reason that i love
to learn more and more.
Right now it seems like Drupal is the preferred blogging platform available right now.
(from what I’ve read) Is that what you are using on your blog?
I do consider all of the ideas you have introduced
to your post. They are very convincing and can definitely work.
Still, the posts are too short for beginners. May just
you please prolong them a bit from subsequent time? Thanks
for the post.
I’m now not certain the place you’re getting
your information, but great topic. I needs to spend a while learning much
more or working out more. Thanks for wonderful information I used to be on the lookout for this info for my
mission.
Ӏ visited several websites but tҺɦe audio feture for audio songs current at this
website is іn fact fabulous.
It’s really a great and useful piece of information.
I’m satisfied that you just shared this helpful info with us.
Please stay us up to date like this. Thanks for sharing.
This piece of writing gives clear idea for the new users of blogging,
that genuinely how to do running a blog.
Can I simply say what a relief to discover someone that actually understands what they are talking about over the internet.
You definitely understand how to bring a problem to light and make it important.
More and more people need to look at this and understand this side of your story.
I was surprised that you aren’t more popular because you most
certainly have the gift.
Its not my first time to go to see this site, i am visiting this site dailly and obtain nice
information from here all the time.
Oh my goodness! Impressive article dude! Thank you,
However I am experiencing problems with your RSS.
I don’t understand why I am unable to join it. Is there anyone
else having similar RSS problems? Anyone that knows the solution will you kindly respond?
Thanks!!
I believe this is one of the so much significant information for me.
And i’m happy studying your article. However want to statement on some basic things, The web site taste is
wonderful, the articles is in point of fact great : D.
Just right task, cheers
I savor, cause I discovered exactly what I used to be having
a look for. You have ended my four day lengthy hunt!
God Bless you man. Have a great day. Bye
El plazo de inscripción para apuntarse a la octava edición del ALLSTARTUP, que
va a tener lugar los días dos, tres y 4 de octubre de 2015, está abierto a través de la página web de Demium Startups y de En la edición anterior, festejada
en el primer mes del verano, más de ciento
sesenta emprendedores se apuntaron para participar en el acontecimiento.
J’ai donc eu l’idee d’ecrire un article avec quelques conseils sur comment choisir son guide de voyage.
It’s hard to come by educated people on this subject, however, you seem like you know what you’re talking about!
Thanks
Pingback: ピュアCSSアコーディオン | Acenumber Technical Issue
Remarkable things here. I’m very happy to see your article.
Thanks a lot and I am looking forward to contact you.
Will you kindly drop me a mail?
Decouvrez tous nos conseils pour acheter une cocotte minute
pas cher et les meilleurs appareils du marche!
What’s up friends, its great article concerning teachingand fully explained,
keep it up all the time.
Yesterdɑy, wһile I was att աork, mʏ cousin stole my iphone andd tested tо seee iif іt саn survive a thhirty foot drop, ϳust ѕⲟ
sҺᥱ ccan ƅᥱ a youtube
sensation. Ⅿy apple ipad іѕ noѡ broken andd ѕҺᥱ Һаѕ 83
views. Ι кnoѡ tһіs iѕ entirely օff topic Ьut ӏ ɦad tο share іt with someone!
mу weblog – toko sepatu wanita termurah
Why people still use to read news papers when in this technological world
everything is available on net?
Hello, all the time i used to check weblog posts
here early in the dawn, for the reason that i
love to learn more and more.
Sehr scɦöner Beіtгаǥ!
Ⅰϲһ ɡսсқe mіг sеɦг gеrne Ꮩіdеоѕ іm ԜеЬ an. Ѵߋгallᥱm maց іϲh Νetflіх.
I think what you typed made a bunch of sense. But, consider this, what if you added
a little content? I mean, I don’t wish to tell you how
to run your blog, however suppose you added a headline
to maybe grab a person’s attention? I mean The CSS-Only Accordion Effect is a little vanilla.
You should look at Yahoo’s home page and see
how they write news headlines to grab people to open the links.
You might add a related video or a picture or two to grab readers interested about
everything’ve written. In my opinion, it might bring your posts a little bit more
interesting.
I have read so many content concerning the blogger lovers however this article is truly
a good paragraph, keep it up.
I’ve been browsing online more than 3 hours nowadays, yet I
by no means discovered any fascinating article like yours. It
is lovely price sufficient for me. Personally, if
all site owners and bloggers made excellent content as you did, the web will be a
lot more helpful than ever before.