An accordion effect can be achieved using CSS3’s :target pseudo-class, without requiring JavaScript. Using the proprietary -webkit-transition property this accordion can also be animated.
Result
CSS3 Accordion
Works in browsers that support the :target pseudo-class, see the Quirks Mode compatibility tables. Animation works in recent WebKit based browsers.
How To
Each part of the accordion has an ID, heading and content region. The header includes a link that matches the section’s ID, whilst the content is wrapped in a container which will control its display.
<div class="accordion"> <h2>Accordion Demo</h2> <div id="one" class="section"> <h3> <a href="#one">Heading 1</a> </h3> <div> <p>Content</p> </div> </div> <div id="two" class="section"> <h3> <a href="#two">Heading 2</a> </h3> <div> <p>Content</p> </div> </div> </div>
The CSS then relies on the :target pseudo-class to apply different styles to the chosen section — increasing the height and, in large content cases, altering the overflow behaviour to allow scrolling. To animate the opening and closing of sections the -webkit-transition property is needed (documentation), in this case acting on the height attribute for a duration of 0.3 seconds using the ease-in timing function.
Stripping out the styling, the CSS boils down to:
.accordion h3 + div {
height: 0;
overflow: hidden;
-webkit-transition: height 0.3s ease-in;
-moz-transition: height 0.3s ease-in;
-o-transition: height 0.3s ease-in;
-ms-transition: height 0.3s ease-in;
transition: height 0.3s ease-in;
}
.accordion :target h3 + div {
height: 100px;
}
.accordion .section.large:target h3 + div {
overflow: auto;
}
Critique
Obviously this approach has its limitations. Multiple open accordions on one page wouldn’t be possible — restricted by a URI’s one fragment identifier limit; as one accordion opens the other would lose the target and automatically close. Similarly, pages that use a fragment identifier for everyday use will notice oddities — take for instance when using top links to return to the top of the page, any accordion would, in this case, reset. Other uses include accessibility links and simulated page histories when using Ajax.
17 Comments
Check http://aranedaalejandro.wordpress.com for another CSS only accordion menu (last version with slidding effect with SMIL for IE and CSS3 transitions for the rest)
Love it, thank you for this trick. Your effort is appreciated.
this is great, thanks!
Great Article … thnks
I have been trying to integrate this accordian effect into a mobile website for a client without much avail. But your solution works perfectly in both iOS and android devices. Thank you!
Hi,
I have tried using it on my site and it does work great, but I would like to switch the Heading 1 text with an image. As image is inserted into the place of text trough tags, the area remains clickable, but doesn’t expand any longer. I tried playing around with it, but I sadly can’t come up with any kind of solution.
Could you please offer me your insight on the matter how to make it work.
Thank you and thanks again for posting!
Another pure css3 accordion here using hovers: http://www.aplweb.co.uk/blog/css/pure-css3-accordion/
Really cool! but sadly it doesn’t work in IE

I personally use Opera, but site must work in all browsers…
@bastek: it works in IE9. As the author said, “Works in browsers that support the :target pseudo-class”. IE before IE9 does not support it.
Excellent, I was looking for this, thank you.
IE<9 needs conditional stylesheet.
Integrate also :hover and :focus?
B
http://www.kcl.ac.uk/ip/brentcunningham
Paul, Thanks for posting this — nice solution for mobile web!
Maybe I’m a goof but when I try to have the height of each “fold” just be determined by the interior content by using auto or removing height the animation doesn’t work anymore. I’m sorry if this is a strange question, I’m so used to using js for some of these things.
In my opinion the biggest downside is the fact that the navigation is made through links and this makes the page to scroll in order to put the referred link on top of the page. I dont think there is an easy way to solve this problem unfortunately.
Está genial, me encanta!
It’s great, I love it!
Cool!
Animation would work on IE, Firefox, and Opera if you add the following lines under –webkit-transition attrib.:
–o-transition: height 0.3s ease-in;
–moz-transition: height 0.3s ease-in;
–ms-transition: height 0.3s ease-in;
Nick
Great css script
i did a change to avoid scroll bars
.accordion h3 + div {
height: 0;
overflow: hidden;
–webkit-transition: height 0.3s ease-in;
}
.accordion .section:target div {
height: 100%;
position: relative;
float: left;
width: 100%;
padding-bottom: 10px;
}
@Al:
It does work in IE9+ and FF8+ but this demo is missing the –moz, –o, and –ms flavors for the transition css3 attribute.
It would be nice if Paul updated his CSS across the site to include the browser-specific implementations of these CSS3 attributes. Better yet, the browser makers should stop using these extensions and just use the actual attribute, TRANSITION.