open
Toggle visibility of details disclosure widgets and dialog elements. A boolean attribute that controls whether expandable or overlay content is currently shown.
Overview
The open attribute is a boolean attribute that controls the visibility of expandable content. It applies to two elements:
<details>— controls whether the disclosure widget is expanded or collapsed<dialog>— controls whether the dialog is visible (non-modal)
When open is present, the content is shown. When absent, it is hidden.
Details Element
On <details>, the open attribute determines whether the content below the <summary> is visible. Users can toggle it by clicking the summary. Add open in markup to have the section expanded on page load.
Exclusive Accordions with name
The name attribute on <details> creates an exclusive accordion group. When multiple <details> elements share the same name, opening one automatically closes the others — no JavaScript required.
Dialog Element
On <dialog>, the open attribute makes the dialog visible as a non-modal overlay. However, for modal dialogs you should use showModal() instead, which adds the open attribute automatically and also provides backdrop, focus trapping, and Escape-to-close behavior.
| Approach | Backdrop | Focus Trap | Escape to Close | Top Layer |
|---|---|---|---|---|
open attribute | No | No | No | No |
showModal() | Yes | Yes | Yes | Yes |
show() | No | No | No | No |
Styling Open State
Use the [open] attribute selector to style expanded details or visible dialogs.
JavaScript Events
The toggle event fires on <details> when its open state changes, whether by user interaction or script. The open property reflects the current state.
Accessibility
- Screen readers announce
<details>as a disclosure widget with its expanded/collapsed state. - The
<summary>element is focusable and activatable via keyboard (Enter and Space). - For
<dialog>, always prefershowModal()over theopenattribute — it ensures proper focus management and keyboard behavior.
Limitations
- The
nameattribute for exclusive accordion groups is relatively new. Older browsers will treat each<details>independently. - There is no built-in animation for
<details>open/close transitions. CSS transitions on height require workarounds. - Setting
opendirectly on a<dialog>in markup creates a non-modal dialog with no backdrop or focus trapping, which is rarely what you want. - The
toggleevent is not cancellable — you cannot prevent the state change from within the handler.