<details>
Definition
The <details>
element in HTML allows users to view and hide additional information on a webpage. It's often used in conjunction with the <summary>
element, which serves as the label or title for the collapsible content.
Examples
Here's an example of how the <details>
and <summary>
elements can be used:
<details>
<summary>Click here to see more details</summary>
<p>Additional information goes here.</p>
</details>
When a user clicks on the <summary>
element, the additional information inside the <details>
element will be revealed. Clicking on the <summary>
element again will hide the information.
One unique feature of the <details>
element is that it has a open
attribute, which can be used to specify whether the additional information should be visible by default or not.
For example, the <details>
element in the following code will be open by default:
<details open>
<summary>Click here to see more details</summary>
<p>Additional information goes here.</p>
</details>
Attributes
Attribute | Description | Deprecated |
---|---|---|
open | Specifies whether the additional information associated with the element should be visible by default or not | No |
Best Practices
- Use the
<summary>
element as the label or title for the collapsible content inside the<details>
element. This will make it clear to the user what the additional information is about. - Use the
open
attribute to specify whether the additional information should be visible by default or not. This will help provide a better user experience, especially if the page has multiple<details>
elements. - Keep the additional information inside the
<details>
element concise and relevant. Avoid using long blocks of text or irrelevant information, as this can make the page difficult to read and navigate. - Use the
<details>
element in a way that enhances the overall design and layout of the page. For example, you can use it to create an accordion-style navigation menu or to hide and reveal important information in a user-friendly way. - Avoid using the
<details>
element for critical information that the user needs to see. Because the additional information inside the<details>
element is hidden by default, important information may be missed by the user. It's best to use the<details>
element for supplementary or optional information.
Accessibility Considerations
The HTML <details>
element is used to create a widget that the user can open and close to show or hide additional content. When used correctly, the <details>
element can make your website more accessible, because it allows users to choose whether or not they want to see the additional content.
However, there are some accessibility considerations that you should keep in mind when using the <details>
element. For example, the <details>
element should always have a <summary>
element as its first child, which provides a brief summary of the content that will be shown when the widget is expanded. This allows users who are using screen readers to quickly understand what the widget is for and what will be revealed when it is opened.
Additionally, you should avoid using the <details>
element as the only way to access important content on your website. This is because some users, such as those with motor impairments, may not be able to use the widget to open and close it. In these cases, it is important to provide an alternative way for users to access the content, such as a link or button that they can use to reveal the content.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |