The CSS-Only Accordion Effect

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.

example of accordion effect before and after the user mouses over it

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.

extra text is hidden from the user, inspiring curiosity.

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!

17 Comments

  1. On April 09, 2008
    10:12AM

    Jennifer said:

    This would be interesting to see implemented in collapsed navigation in the secondary content area of the page (sidebar). Suppose you are using uls for your nav, apply the height and overflow styles to their corresponding ul and/or li.

    The main gripe I have about this accordion method, however, is that it proves inaccessible to users without a mouse, which happens more often that we care to admit. iPhone/iPod Touch users are incapacitated, and I know in my conference room the wireless mouse works just as often as it doesn’t and I’m usually relegated to keyboard control.

    In a navigation setting, however, you could combat this problem with anchors around the nav section headings (which are semantic), coupled with :active and :focus styles in addition to your hover styles.

    Altogether, pretty cool! This along with a Javascript accordion promises users a very similar experience whether or not JS is enabled.

  2. On April 09, 2008
    11:26AM

    Rob Glazebrook said:

    Great comments, Jennifer. I’ll admit wholeheartedly that this sort of method isn’t appropriate for all uses. I mostly presented it as a proof-of-concept: you don’t need more than a dozen lines of CSS to create some pretty interesting interactive effects. I like the idea of trying the change to the :active and :focus pseudo-classes, too — I might have to give that a try!

  3. On April 10, 2008
    11:22AM

    Justin said:

    Pretty cool. However, doesn’t have that smooth effect like you get when using javascript. Very impressive!

  4. On April 11, 2008
    3:00AM

    Alex said:

    It’s probably a nice way for a Javascript-powered accordian to degrade (assuming the mouse access issues were handled).

    Like many people, I have ‘No Scripts’ enabled when I visit a site for the first time, and many sites never get a chance to get their JS privileges because they fail badly on first load.

    This gives you basic site functionality without any JS needed.

  5. On April 11, 2008
    3:45AM

    Adam said:

    Nice, but it doesn’t work in ie6; that reminds me suckerfish

  6. On April 11, 2008
    6:37AM

    leonard said:

    Doesn’t seem to work properly in firefox3b5, when accordion is horizontal, and when you move from left to right, the last pane doesn’t flip open but instead all pane are collapsed and will reset to default if you leave the the accordian area

  7. On April 11, 2008
    7:42AM

    tony petruzzi said:

    might be considered cheating, but you can get the :hover effect in ie6 to work with a little javascript:

    http://lawrence.ecorp.net/inet/samples/css-ie-hover1.shtml

  8. On April 25, 2008
    11:01AM

    ctraos said:

    exlente post, muchas gracias !!

  9. On May 01, 2008
    4:32AM

    Justin said:

    This is pretty cool. I have something similar working on my site only it’s more of a vertical navigation for some quick misc. links. It’s a little tricky but I’m still working with it to get it to work better.

    Justin

  10. On May 06, 2008
    12:12AM

    www.minicity.in said:

    Thanks for your posting !
    But it’s not work on IE6 !

    ———^_^——–
    Thanks and best regard,

  11. On June 14, 2008
    2:59AM

    Alex said:

    Nice realization.
    But I have some troubles with MSIE.

  12. On July 26, 2008
    10:02AM

    California Home Equity Loan said:

    I happened upon this site while following the links from another site. Your site is wonderful and i bookmarked it. Thank your for the hard work you must have put in to create this wonderful facility. Keep up the excellent work

  13. Raza Ali said:

    It’s really awesome i must have to try…

  14. On April 23, 2009
    11:43AM

    Doug said:

    Hi, I was wondering if it was possible to set one of the columns to be wide on page load. And when ever the bar is not hovered that one column is always bigger. I hope this is understandable.

  15. Nenad said:

    Honestly, I’m not impressed in any way. First it breaks in Opera and second it doesn’t look good anyway.

  16. Rahul - Web Guru said:

    I just wish there was a horizontal menu for this one. ;)

  17. Flying Dutchman said:

    Amazing !
    I am a css-only fan too, but this is one off the most beautifull trick I saw.

    Can permit me to suggest you to use the CSS3 “transition:;” property (works in firefox 3.6 with -moz- and in Chrome and Safari with -webkit-).
    So you can give animation to your effet :
    It will no more be instantly but the accordion effect will be gradual (I am maybe not very comprehensive, ’cause I am can’t speak very good english…).

    You can see some animation effect on my home page, in the head menu (with Chrome, safari or Firefox 3.6).

    I hope I have help you a bit^^

    Long live css^^”
    And I repeat, I find your effect amazing !

73 Responses Elsewhere

  1. Skylog » Blog Archive » links for 2008-04-10 said:

    [...] The CSS-Only Accordion Effect (tags: css) [...]

  2. Fatih Hayrioğlu’nun not defteri » 11 Nisan 2008 web’den seçme haberler said:

    [...] CSS ile akordeon menü yapımı. Javascriptdeki gibi olmasa da idare eder. Bağlantı [...]

  3. CSS only Accordion Effect « Rif.webmemo said:

    [...] The CSS-Only Accordion Effect – CSSnewbie – 注釈有 [...]

  4. Links of Interest - CSS-Tricks said:

    [...] of great accordian examples last week. Rob shows us a darn clever way to handle it with CSS only. David shows us how to make an accordian with MooTools. I love just how smooth MooTools animations [...]

  5. On April 25, 2008
    12:14AM

    30 Exceptional CSS Techniques and Examples | Six Revisions said:

    [...] 11. CSS-Only Accordion Effect [...]

  6. On April 30, 2008
    11:51AM

    Best Of April 2008 | Best of the Month | Smashing Magazine said:

    [...] The CSS-Only Accordion Effect“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.” [...]

  7. On May 01, 2008
    11:25PM

    Kantongin » Best Of April 2008 said:

    [...] The CSS-Only Accordion Effect“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.” [...]

  8. On May 06, 2008
    12:49PM

    Accordion | milo said:

    [...] on May 5 ⁄⁄ CSS-Only Accordion Effect ⁄⁄ An accordion effect using div’s and :hover; the accordion effect can be vertical or horizontal… Published in Design, Spotlight | Tags: web | Browse the Archives That’s all. Want more info? [...]

  9. On May 06, 2008
    9:44PM

    בלוגה של אמא » links for 2008-05-07 said:

    [...] The CSS-Only Accordion Effect – CSSnewbie (tags: css css-accordion) [...]

  10. On May 09, 2008
    4:06PM

    Best Of April 2008 - Glimpses of the Aleph said:

    [...] The CSS-Only Accordion Effect“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.” [...]

  11. Div+Css页面特效代码30个 said:

    [...] 11. CSS-Only AccordionEffect [...]

  12. New to CSS? New to CSSnewbie? Start Here! - CSSnewbie said:

    [...] The CSS-Only Accordion Effect [...]

  13. AdobeDeveloperFace » 30 Exceptional CSS Techniques and Examples said:

    [...] 11. CSS-Only Accordion Effect [...]

  14. Pure CSS Accordion Menu | ProgTuts said:

    [...] further reading why not check out this article at CSS Newbie where my idea for this tutorial came [...]

  15. CSS techniques and examples List: Part 1 | Net Feast said:

    [...] 11. CSS-Only Accordion Effect [...]

  16. 30 Exceptional CSS Techniques and Examples « Jonsunhee’s Weblog said:

    [...] 11. CSS-Only Accordion Effect [...]

  17. 30 Exceptional CSS Techniques and Examples : NguyenVanChieu said:

    [...] 11. CSS-Only Accordion Effect [...]

  18. CSS-Only Accordion « Yes We Did! said:

    [...] much so that I felt kind of embarassed when I figured it out. Most of the credit should be given to cssnewbie; the modifications I made simply expanded on their article to only show the titles of the accordion [...]

  19. 30 Exceptional CSS Techniques and Examples - Six Revisions | Secondary cortex: online said:

    [...]  CSS-Only Accordion Effect An accordion effect using div’s and :hover; the accordion effect can be vertical or horizontal. [...]

  20. 精选30个优秀的CSS技术和实例at ThinkiP! said:

    [...] 11.纯CSS手风琴效果 [...]

  21. intkids`blog » 资源盛宴:30个优秀CSS技术和实例(上) said:

    [...] 模仿Windows开始菜单的CSS导航菜单。   11.纯CSS手风琴效果 [...]

  22. Advanced CSS Accordion Effect said:

    [...] 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 [...]

  23. rascunho » Blog Archive » links for 2008-12-18 said:

    [...] The CSS-Only Accordion Effect The Accordion Effect is fast becoming one of the most commonly used (and perhaps abused?) effects of the Web 2.0 world. (tags: http://www.cssnewbie.com 2008 mes11 dia18 Accordion CSS article) [...]

  24. Advanced CSS Accordion Effect | Castup said:

    [...] 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 [...]

  25. The Year In Review: 2008 said:

    [...] The CSS-Only Accordion Effect [...]

  26. On March 25, 2009
    10:46AM

    Discover the “Cool” of CSS: 25 Advanced CSS Techniques | Desizn Tech said:

    [...] This post explains how to create Accordion using CSS [...]

  27. 22 Most Amazing Css Tricks « Reeckon said:

    [...] 14. CSS Accordion effect Really cool [...]

  28. 「magazine-basic」途中経過 その2 | ブログで行こう! WordPress なら最高! said:

    [...] コリスさんが紹介していた「[CSS]上級者向け、スタイルシートのテクニック集」内の「アコーディオン」を利用しました。cssだけで実現している機能なので、編集も簡単。javascript や Flash 仕様のアコーディオンが沢山ありますが、それほどのクオリティは必要ないので、これにしました。   この週末でサブコンテンツの方向を決め、4月1日から本格始動です。孫たちが保育園に通い始めますから……。 [...]

  29. 25个CSS教程 - Booto’Blog said:

    [...] create Accordion using CSS [...]

  30. On March 31, 2009
    10:05AM

    我想网 » Blog Archive » 30个非凡的CSS技巧和例子 said:

    [...] 11. CSS-Only Accordion Effect [...]

  31. 克兰印象 » 25个高级CSS技巧教程 said:

    [...] create Accordion using CSS [...]

  32. 30 Exceptional CSS Navigation Techniques said:

    [...] 19. CSS-Only Accordion Effect [...]

  33. On April 17, 2009
    10:50AM

    30种时尚的CSS网站导航条 | 酷壳 said:

    [...] 19. CSS-Only Accordion Effect [...]

  34. On April 19, 2009
    12:42AM

    30个特殊的CSS导航技术 « Burning Fav’s Blog said:

    [...] 19. CSS-Only Accordion Effect [...]

  35. 30 Exceptional CSS Navigation Techniques | Blog YODspica Ltd said:

    [...] 19. CSS-Only Accordion Effect [...]

  36. 30 Exceptional CSS Navigation Techniques « Livefont Interactive said:

    [...] 19. CSS-Only Accordion Effect [...]

  37. On April 22, 2009
    10:35PM

    神奇的CSS-25个样本迷死你 | 鹏博客 said:

    [...] create Accordion using CSS [...]

  38. On April 23, 2009
    11:07AM

    22个不错的CSS技术 | 酷壳 said:

    [...] 14. CSS 折叠式导航 [...]

  39. 30 CSS Navigation Techniques « Webdesignchennai’s Blog said:

    [...] 19. CSS-Only Accordion Effect [...]

  40. 40 Outstanding CSS Techniques And Tutorials | Arbenting said:

    [...] The CSS-Only Accordion Effect – An accordion effect using only pure CSS, no javascript. [...]

  41. On May 03, 2009
    2:12PM

    365必备资讯网 » Blog Archive » 25个高级CSS技巧教程 said:

    [...] create Accordion using CSS [...]

  42. On May 04, 2009
    11:22PM

    Loves sunshine » Blog Archive » 30+css导航技术 said:

    [...] 19. CSS-Only Accordion Effect [...]

  43. On May 06, 2009
    5:06AM

    jsssc - 30个特殊的CSS导航技术 said:

    [...] 19. CSS-Only Accordion Effect [...]

  44. You are now listed on FAQPAL said:

    The CSS-Only Accordion Effect…

    The Accordion Effect is fast becoming one of the most commonly used (and perhaps abused?) effects of the Web 2.0 world….

  45. 10 CSS tutorials that will make your website looks cool said:

    [...] Learn how to create the CSS-only Accordion Effect. [...]

  46. On July 06, 2009
    10:13PM

    Design-Tut+ » 10 CSS tricks that make website looks cool instantly! said:

    [...] Learn how to create the CSS-only Accordion Effect. [...]

  47. Technology Blog / 10 CSS tricks that make website looks cool instantly. said:

    [...] Learn how to create the CSS-only Accordion Effect. [...]

  48. 25个高级CSS技巧教程 | 帕兰映像 said:

    [...] create Accordion using CSS [...]

  49. 10 Amazing CSS Techniques | CSSDOSE said:

    [...] CSS-Only Accordion Effect [...]

  50. 22个不错的CSS网页特效技术 | 无名小站 said:

    [...] 14. CSS 折叠式导航 [...]

  51. 30款时尚酷酷的CSS导航条 | 无名小站 said:

    [...] 19. CSS-Only Accordion Effect [...]

  52. Discover the “Cool” of CSS: 25 Advanced CSS Techniques | Theme Center said:

    [...] This post explains how to create Accordion using CSS [...]

  53. [转]30个优秀的CSS实例 said:

    [...] 11.纯CSS手风琴效果 [...]

  54. 10 CSS tricks that make website looks cool instantly! | Theme Center said:

    [...] Learn how to create the CSS-only Accordion Effect. [...]

  55. 30 Exceptional CSS Techniques and Examples | Theme Center said:

    [...] 11. CSS-Only Accordion Effect [...]

  56. 30 Exceptional CSS Techniques and Examples @ tipBOX.net said:

    [...] 11. CSS-Only Accordion Effect [...]

  57. 30个精选优秀的CSS实例 | 伟景博客 said:

    [...] 11.纯CSS手风琴效果 使用div和:hover伪类的手风琴效果,这个效果可以垂直显示也可以水平显示。 [...]

  58. 享受生活 » 22个不错的CSS技术 said:

    [...] 14. CSS 折叠式导航 [...]

  59. 享受生活 » 30种时尚的CSS网站导航条 said:

    [...] 19. CSS-Only Accordion Effect [...]

  60. 10 Top-Notch CSS Editors « HUE Designer said:

    [...] 19. CSS-Only Accordion Effect [...]

  61. Saint John Web Design | Informative Computer Solutions » Blog Archive » The Best CSS Newbie Articles of 2009 said:

    [...] The CSS-Only Accordion Effect. This article (and its successor, the Advanced CSS Accordion Effect) have received a lot of feedback, both positive and negative. The negative comments generally revolve around my ineptitude in the graphic design department. I’m comfortable with that, I think. [...]

Leave a Comment