The <details>
element in HTML creates a widget that contains additional information that is only visible when the widget is in the “open” state. There are no restrictions on the content that can be included inside the <details>
element.
By default, the widget created by the <details>
element is in the “closed” state. You can click on the widget to toggle between the “open” and “closed” states to reveal or hide the additional information enclosed in the tag.
To use the <details>
element, you need to include the <summary>
element to specify a visible heading for the additional details, as shown below:
<details>
<summary>Heading</summary>
</details>
The <details>
element supports the open
attribute that specifies whether the content should be visible to the user. The contents are hidden by default. The <details>
element also includes global attributes.
The <details>
element supports all the usual events that are supported by HTML elements. In addition, <details>
also supports the toggle
event, which is triggered whenever the <details>
element’s state changes between “open” and “closed”.
The code below shows how the <details>
element works:
The <summary>
element in line 9 creates a visible heading. When you click on the small triangle to the left of this heading, the open
attribute of the <details>
element changes, and the contents of the <details>
element becomes visible. Similarly, clicking on the triangle again changes the state of the open
attribute, and the contents of the <details>
element become invisible again.
If you want the contents of the <details>
tag to be visible by default, then the opening <details>
tag in line 8 would need to be adjusted as shown below:
<details open>
Since the open
attribute is now present, the <details>
element is rendered with the content being visible by default.
Note: The style of the widget created by the
<details>
element can be changed using CSS. You may check the following link to see how the appearance of the<details>
element can be customized.
Free Resources