# details & summary

> The `<details>` and `<summary>` elements give you a native, keyboard-accessible disclosure widget with no JavaScript. The browser handles open/close toggling, focus management, and the toggle event.

Subject: HTML | Lesson 40 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/details-and-summary
Previous: [Loading CSS, JS & Resource Hints](https://codewithanimation.in/html/loading-css-js-and-hints.md) | Next: [Native Overlays: dialog & popover](https://codewithanimation.in/html/dialog-and-popover.md)

## What details and summary do

The `<details>` element is a disclosure widget: it has two states, collapsed and expanded, and toggles between them when the user interacts with its `<summary>` child. In the collapsed state only the summary is visible. In the expanded state the full content appears. The `open` boolean attribute controls which state the element is in -- when present, it is expanded. You can set it in HTML (`<details open>`) to start a widget already open. JavaScript can read and set `el.open` as a boolean property.

The `<summary>` must be the first child of `<details>`. It is the visible label that acts as the toggle button -- the browser gives it `role="button"` automatically, so it receives keyboard focus, can be activated with Space or Enter, and is announced correctly by screen readers. If you omit `<summary>`, browsers generate a fallback label that typically reads "Details". The triangle marker before the summary is a CSS `::marker` pseudo-element and can be replaced or hidden.

## Why native beats a custom accordion

A hand-rolled accordion built from `<div>` elements requires you to manage: a button with correct `aria-expanded` and `aria-controls`, a panel with `id` and correct `hidden` state, keyboard event handlers for Space and Enter, focus trapping or not, and open/closed state in JavaScript. The `<details>` element handles all of this for free. The browser manages `aria-expanded`, the toggle event, keyboard activation, and focus order. Content inside an open `<details>` is automatically in the tab order; content inside a closed one is not.

The `name` attribute (supported in modern browsers from 2024) creates an exclusive accordion: multiple `<details>` elements with the same `name` value behave like a radio group -- opening one closes the others. This replaces an entire class of JavaScript that developers have been writing for decades.

## How to use details and summary

The minimal pattern is `<details><summary>Question</summary><p>Answer</p></details>`. To start it open, add the `open` attribute: `<details open>`. To listen for state changes, add an event listener for the `toggle` event: `el.addEventListener('toggle', () => { if (el.open) { ... } })`. The event fires after the state has changed.

For an exclusive accordion where only one panel can be open at a time, give all `<details>` elements the same `name` attribute: `<details name="faq">`. The browser enforces mutual exclusion. For an animated version, listen for the `toggle` event and use GSAP (or a CSS `max-height` transition) to animate the content region.

To customise the marker, target `summary::marker { content: '' }` or `summary { list-style: none }` (for webkit compatibility) and add your own icon. You can also use `summary::-webkit-details-marker { display: none }` for older Safari.

## Common mistake: Putting the summary after the content

`<summary>` must be the first child of `<details>`. If you put it anywhere else, the browser ignores it and generates the default "Details" label, or the element does not work as expected. The content comes after the summary, not before it.

## Tip: Listen to the toggle event for animations

The `toggle` event fires after the `open` state changes, which means you know whether the element just opened or closed. Use it to GSAP-animate the height of the content: on open, animate from 0 to the auto height; on close, animate back to 0 and then remove `open` programmatically. This gives you a smooth disclosure animation while keeping the native keyboard and AT behaviour.

## Try it: Try it

Click summaries to open and close disclosure panels, and toggle exclusive mode to see the `name` attribute enforce a single-open accordion.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/details-and-summary

## Check yourself

### Question 1

Where must the <summary> element be placed inside <details>?

- A. As the first child of <details>
- B. Anywhere inside <details>
- C. After all the content, as the last child

Correct answer: A

`<summary>` must be the first child of `<details>`. The browser only recognises the first `<summary>` child as the toggle label.

- Why not B: <summary> must be the first child. If it is placed elsewhere, browsers ignore it and generate a default label.
- Why not C: <summary> must come first. Placing content before the summary breaks the element's expected behaviour.

### Question 2

Multiple <details name='faq'> elements are on the page. What happens when you open one?

- A. The browser closes all other <details> with the same name
- B. Nothing special; name is just metadata and has no behavioural effect
- C. The name attribute is invalid on <details> and is ignored

Correct answer: A

The `name` attribute on `<details>` creates an exclusive group. Only one `<details>` with a given name can be open at a time. The browser enforces this natively without any JavaScript.

- Why not B: When multiple <details> elements share the same name attribute, the browser treats them as an exclusive group. Opening one automatically closes the others, creating a native accordion with no JavaScript.
- Why not C: The name attribute was added to <details> in HTML5.2 and is supported in modern browsers from 2024. It creates an exclusive accordion group.

### Question 3

When does the toggle event fire on a <details> element?

- A. After the open state has changed
- B. Before the open state changes, allowing it to be cancelled
- C. Only when JavaScript calls el.open = true

Correct answer: A

The `toggle` event fires after the `open` attribute has changed. Check `el.open` inside the handler to know whether the element just opened or closed.

- Why not B: The toggle event is not cancellable and fires after the state change. To animate the transition, listen for toggle and respond to the new state.
- Why not C: The toggle event fires on any open state change: user click, keyboard activation, or programmatic change via el.open.

## Remember this

- `<summary>` must be the first child of `<details>`. Content comes after it.
- The `open` attribute controls state. Add it to HTML for an initially open widget. Read/set `el.open` in JS.
- `name` creates an exclusive accordion group: only one `<details>` with that name can be open at a time.
- The `toggle` event fires after state changes -- use it to trigger GSAP animations.
