TIL the details and summary elements give you an accordion with zero JavaScript
I've been writing little toggle scripts for FAQ sections for years. Turns out <details> with a <summary> inside does it natively: click to open, click to close, keyboard accessible, and you can style the marker with CSS.
You can even add the open attribute to have it expanded by default.
What other built-in HTML features did you discover way too late?
Comments · 4
Nice thing about the summary marker is that you can swap it for your own icon and rotate it with a CSS selector on [open]. The accessibility is also better than most hand-rolled accordions.
Browser support is good now. You can style the marker and animate the open state with a bit of CSS, though animating the height smoothly takes extra tricks.
Can you make several details elements behave like an exclusive accordion where opening one closes the others?
Yes, add the same
nameattribute to each details element. Browsers then keep only one open at a time. It's a newer feature, so check support for your audience.