Details on <details>

Have you heard about details?

It's a pretty useful element that handles showing and hiding content natively!

Usage over time details usage
Why should I use it?
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.

If you want the details block open by default, include the open attribute:

details element
How do I set a custom title?

Use the summary as the first child of your details element.

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!)

What about styling?

Style the details element just like any other CSS element.

Styling <summary>

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, set the summary to display: block or flex (anything but the native display: list-item) to get rid of the ▸

Or just use GitHub's CSS helper: reset utility

What about styling based on state?

When a details block is open, it has the open attribute. To style it (or its children) based on its state, use

details[open] { }
.

There's no closed attribute: styles you apply by default will be used on the closed state.

Are there events?

You can listen for the toggle event to detect when the widget changes state:

Details events
What about accessibility?

Since these are native HTML elements, they provide useful semantic information to screen readers out of the box!

Tabable and press enter / space to toggle.

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.

What are some use cases?
Accordions

This page!

Popovers Popover example

GitHub Emoji Reactions (Late 2017)

GitHub emoji reactions
Dropdowns

Old Way

old dropdown

New Way

new dropdown

Examples

GitHub dropdown menu

GitHub Build Status

GitHub build status dropdown menu

GitHub's CSS helper: overlay utility

Dialogs Modal example GitHub dialog

GitHub's <details> dependent Custom Elements:

How's the browser support? support

Edge doesn't currently support details/summary with native open/close behavior, but it will be using Chromium soon.

Edge will show all your details elements expanded, so at least the content will still be visible to users.

Edge

Accessible polyfill left arrow

More information