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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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:
1 2 3 | #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:
1 2 3 4 5 | #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:
1 | #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:
1 2 3 | #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:
1 2 3 4 5 6 7 8 9 10 11 12 | #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!

I’ve said that least 716238 times. The problem this like that is they are just too compilcated for the average bird, if you know what I mean
Katy Perry happens to be at # 4 located on the United states Billboard Chart. In a case where. She actually reaches number one it will be her 5th numberone single coming from the same album, California Dreams
Nice to be visiting your weblog again, it has been months for me. Properly this write-up that i’ve been waited for so long. I want this post to total my assignment within the college, and it has exact same topic with your write-up. Thanks, excellent share.
I am often blogging and i really appreciate your content. The article has really peaked my interest. I am going to bookmark your site and keep checking for new information.
Good site! I really love how it is easy on my eyes and the data are well written. I’m wondering how I could be notified whenever a new post has been made. I’ve subscribed to your RSS feed which must do the trick! Have a great day!
Great blog! I really love how it’s easy on my eyes as well as the information are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which ought to do the trick! Have a nice day!
Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work…
Sorry for the huge review, but I’m really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it’s the right choice for you.
I like what you guys are up also. Such clever work and reporting! Carry on the excellent works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my site :). “As is a tale, so is life not how long it is, but how good it is, is what matters.” by Lucius Annaeus Seneca.
why i often face with mistakes when try to maket.
Pingback: 30种时尚的CSS网站导航条 | My ideal.
Pingback: 30 эксклюзивных техник СSS навигации | Интернет журнал Web 2.0 - Всё о Бизнесе в Сети
Pingback: a Collection of CSS Tutorials
The CSS-Only Accordion Effect I was suggested this website by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks! your article about The CSS-Only Accordion Effect Best Regards Lisa Agata
Pingback: 【精华资源】精选30个优秀的CSS技术和实例 - 【enjoyWP】乐享WP
Thanks so much for the tutorial. I used it to make a really cool informational section on one of me sites.
Dr Dre Beats Red Sox look iconique Studio Beats casque dr dre avec une édition spéciale,Red Sox thème La conception du pilote de pointe pour la clarté précise,audio Drr Dre Beats casque beats Red Sox extra haut-parleurs large pour Super Bass,profonde Haute amplificateur alimenté numérique joue hip hop, rock, R + B et plus fort, sans distorsion .
Pingback: 惊奇CSS:25 个高级CSS技巧 | 奔向远方
I and also my buddies were examining the nice suggestions on your web page then then developed a horrible suspicion I never expressed respect to the website owner for those tips. All the men had been consequently joyful to study them and already have in reality been using them. I appreciate you for really being so thoughtful as well as for making a decision on this form of helpful things most people are really desirous to be aware of. My personal sincere apologies for not saying thanks to you earlier.
nice stuff, but im looking for a accordian wich looks like a button and if u click on in pull down. if someone knows good likes, would be helpful.
Pingback: The CSS-Only Accordion Effect » Web Design
awesome!! thanks
why i often face with mistakes when try to maket.
Pingback: 30 Exceptional CSS Navigation Techniques « Web Development informations
Thank you for another magnificent post. Where else may
just anyone get that kind of information in such a perfect manner
of writing? I have a presentation next week, and I am on the search for
such info.
nale harthal
Pingback: 精选30个优秀的CSS技术和实例 | EvilCode 邪恶代码
Pingback: 30种时尚的CSS网站导航条 – 90互联网数据中心
Pingback: 30个优秀的CSS技术和实例 | 闹心 Naoxx 资源
nice post , ty !
Pingback: 30种时尚的CSS网站导航条 « Welcome to Agui's Space
Pingback: Uhostingall.com | free webhosting provider | webtemplates | email hosting| and many more – 30+ Best CSS Tutorials
Yes! Finally someone writes about casque
audio monster beats.
I am really impressed with your writing skills and also with the layout on your weblog.
Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it is
rare to see a nice blog like this one nowadays.
Pingback: สอน css สอน css เบื้องต้น เว็บ สอน css ภาษา css สอน ทำ css สอน css layout สอน html css » บทเรียน css – 25 Advanced CSS Techniques
Hi everybody, I am sure you will be enjoying here
by watching these hilarious videos.
Really simple code but hard if you dont have the knowledge! Thanks a lot Rob.
Cloud computing is the use of computing resources (hardware and software) that are delivered as a service over a network (typically the Internet). For example, email. The name comes from the common use of a cloud-shaped symbol as an abstraction for the complex infrastructure it contains in system diagrams. Cloud computing entrusts remote services with a user’s data, software and computation.*
Have a look at our new internet site as well
<http://www.foodsupplementdigest.com
The code is indeed really simple and this is a great tutorial for CSS, thanks Rob, it was really clear!
Great tips and really simple to use. Thanks Rob, you got great descriptive abilities! :)