Have you heard about details
?
It's a pretty useful element that handles accordion / collapsed text natively!
So how does it work?
You wrap a details
element around any block of HTML content. The browser will collapse that block of text until a user opens the details
block.
Once a user opens a details
block, they'll be able to read all that hidden content!
If you want the details block open by default, include the open
attribute on the opening tag:
<details open>...</details>
But how do I set a custom title?
That's pretty manageable too! Use the summary
element.
Put a summary
at the beginning of your details element and Boom! - you've got a custom title for your details
block.
No worries if you don't add a summary
. The browser will put the word "Details" in there for you. (After all, users need something to click!)
That's cool, but what about styles?
Yes, you're covered there too! Style the details
element however you like. Give it a border, some padding, whatever.
The summary
element is where the ▸
marker lives. If you want to get rid of that, there is a prefixed pseudo-element selector ::-webkit-details-marker
: set that to display: none
for WebKit browsers. In Firefox, it's much simpler: set the summary
to display: block
or flex
(anything but the native display: list-item
) and you'll get rid of the ▸
for you.
Ok, ok, but what about styling based on state?
Once again, details
has got your back! When a details
block is open, it has the open
attribute that I mentioned earlier. To style it (or its children) based on its state, use details[open] { }
.
Note: there's no closed
attribute: styles you apply by "default" will be used on the closed state.
But this requires JavaScript, right?
Open the JS panel on this pen. Clean as a whistle! This is handled totally by the browser.
What about accessibility? Is that the catch?
Sorry to disappoint you. Since these are native HTML elements, they provide useful semantic information to screen readers.
Screen readers will typically read the summary
for a collapsed details
block (and communicate that it's collapsed). They'll also provide an interactive hook for users to open the details
block. If the details
is already expanded, they'll read the whole content.
I don't rely on assistive tech to read the web, so I'm probably not aware of some limitations or drawbacks to using details
and summary
, but I suspect their AX is at least as good as (if not better than) most JavaScript-dependent accordion solutions.
Excellent! More information, please!
You bet! Here are some great resources on details
& summary
:
But what about cross-browser compatibility?
Yeah, sorry. Here's some bad news. IE, Edge, and Opera Mini don't currently support details
/summary
with native open/close behavior (check out caniuse data for details
).
These browsers will show all your details
elements expanded. That's not the worst though: it's a bit of progressive enhancement: if the browser doesn't support the native UI behavior, the content will still be visible to users.
It's unlikey IE11 will be getting any updates on this front, but there's hope for Edge! If this is important to you, please cast a vote a vote for Edge to support details
/summary
. (Or just wait for Edge to use Chromium, I guess. 😕)
If you do need to have open/close behavior in IE11 (or any other non-supporting browser), you'll probably need a polyfill. This Smashing Magazine details
polyfill tutorial looks like a good place to start.