Example
That was a great click! 💪
How to make an accordion element with CSS only and no JavaScript?
A accordion element is great for organizing content in a collapsible and expandable manner, allowing users to quickly find the information they need without being overwhelmed by too much content at once.
This page shows two code examples, one simple non-animated accordion and one that is animated. Both can be made without JavaScript.
Example
That was a great click! 💪
The easiest way to build an accordion is by using the details
and summary
element. This allows us to build a very simple and functional accordion instantly without any CSS or JavaScript.
Control over it’s styling and behaviour is limited though. By using input
and label
instead we can build an accordion over which we have much more control as shown below.
To make the accordion animated we need add a transform, but there’s a problem. Transition does not work on the height property when we go from 0 to auto. So what can we do?
We start by making the container a grid element and defining grid-template-rows
as 0fr. Then we define grid-template-rows
as 1fr on checked and transition this instead. We move over the overflow: hidden to the child element. Finally we add the transition property to the collapsable element and icon, that’s it.
Now you should have all the skills to build your own accordion element and style it to your liking. To make it look exactly like the example above replace the text caret with a svg image and then swap the two lines, placing the icon before the text. Add a bit of margin-right
to the svg icon and it’s done.
Further Links: