HTML <details> Tag
The HTML <details>
tag is a semantic element that represents additional details that a user can view or hide on demand. It is often used to provide information that may not be relevant to all users or to keep long documents more organized and easy to read. The <details>
element can be used to create an interactive widget that expands or collapses its content when clicked or tapped.
The <details>
element can be used in conjunction with the <summary>
element, which represents a summary or legend for the content inside the <details>
element. The <summary>
element should be the first child element inside the <details>
element and is usually used to provide a brief summary of the content or a title for the section.
Here is an example of the <details>
and <summary>
elements in action:
Click here to view more details
Additional details go here
<details>
<summary>Click here to view more details</summary>
<p>Additional details go here</p>
</details>
When the user clicks on the <summary>
element, the content inside the <details>
element will be displayed. The user can then click on the <summary>
element again to collapse the content.
The <details>
element also has an open
attribute, which can be used to specify whether the content should be visible or hidden by default. If the open
attribute is set to "true", the content will be displayed by default. If it is set to "false" or omitted, the content will be hidden by default.
Here is an example of the <details>
element with the open
attribute:
Question: What is HTML and what is it used for?
Answer: HTML (Hypertext Markup Language) is a standard markup language used for creating and formatting web pages and web applications. It is used to structure and present content on the World Wide Web, and is the foundation of most websites and web applications. HTML consists of a series of elements that are used to define the structure and content of a web page, including headings, paragraphs, lists, links, images, and other types of media. HTML can be written using a simple text editor and viewed in a web browser, making it an essential tool for web development and design.
<details open>
<summary>Question: What is HTML and what is it used for?</summary>
<p>Answer: HTML (Hypertext Markup Language) is a standard markup language used for creating and formatting web pages and web applications. It is used to structure and present content on the World Wide Web, and is the foundation of most websites and web applications. HTML consists of a series of elements that are used to define the structure and content of a web page, including headings, paragraphs, lists, links, images, and other types of media. HTML can be written using a simple text editor and viewed in a web browser, making it an essential tool for web development and design.</p>
</details>
In this example, the content inside the <details>
element will be displayed by default when the page loads.
- One common use case for the
<details>
element is to provide additional information or options that are only relevant to certain users or under certain circumstances. For example, a developer could use the<details>
element to provide advanced configuration options for a web application that are only relevant to experienced users. - Another use case for the
<details>
element is to improve the organization and readability of long documents. By using the<details>
element to collapse sections of content that are less important or less frequently used, developers can make it easier for users to find the information they need and reduce the amount of scrolling required to read the document.