summary Tag in HTML
The `<summary>` tag is an HTML5 element that is used in conjunction with the `<details>` tag to create an interactive widget that allows the user to show or hide content on a webpage. When the user clicks on the summary, the details section of the widget will either expand or collapse, revealing or hiding the content within.
In this tutorial, we’ll go over how to use the `<summary>` tag and its related attributes to create this interactive widget.
Basic syntax
The basic syntax for the `<details>` and `<summary>` tags is as follows:
<details>
<summary>Click me to show or hide content</summary>
<p>Content to be shown or hidden</p>
</details>
This will create a widget with a summary element that says “Click me to show or hide content” and a paragraph element that contains the content to be shown or hidden.
Attributes
The `<summary>` tag has a few attributes that can be used to customize its appearance and behavior.
`accesskey`
This attribute specifies a keyboard shortcut to activate the summary element. For example, if you set `accesskey=”s”`, the user can press the “s” key to activate the summary element.
<details>
<summary accesskey=”s”>Click me or press “s” to show or hide content</summary>
<p>Content to be shown or hidden</p>
</details>
`tabindex`
This attribute specifies the order in which the summary element is included in the tabbing order of the page. By default, the summary element is included in the tabbing order, but you can use the `tabindex` attribute to change its position.
<details>
<summary tabindex=”2″>Click me to show or hide content</summary>
<p tabindex=”3″>Content to be shown or hidden</p>
</details>
`open`
This attribute specifies whether the details section should be shown or hidden by default. If the `open` attribute is present, the details section will be shown by default. If the `open` attribute is not present, the details section will be hidden by default.
<details>
<summary>Click me to show or hide content</summary>
<p>Content to be hidden by default</p>
</details>
<details>
<summary open>Click me to show or hide content</summary>
<p>Content to be shown by default</p>
</details>
Styling
You can also style the `<summary>` tag using CSS to customize its appearance. Here’s an example:
<details>
<summary style=”font-weight: bold; color: blue;”>Click me to show or hide content</summary>
<p>Content to be shown or hidden</p>
</details>
This will make the summary element bold and blue.
Conclusion
The `<summary>` tag is a useful HTML element that can be used to create interactive widgets that allow the user to show or hide content on a webpage. By using its attributes and styling it with CSS, you can customize its appearance and behavior to fit your needs.