HTML offers a range of semantic elements that help to structure and organize content in a meaningful way. Among these are the <details>
and <summary>
elements, which provide a simple yet powerful way to create interactive content sections. These elements are especially useful for presenting information that can be toggled by the user, such as FAQs, spoilers, or additional details that do not need to be immediately visible. In this article, we'll explore how the <details>
and <summary>
elements work and discuss some practical use cases for them.
Understanding <details>
and <summary>
The <details>
element is a container for content that can be expanded or collapsed by the user. The <summary>
element, which must be the first child of <details>
, serves as the heading that users click on to toggle the visibility of the content.
Basic Structure
Here's a simple example of the basic structure:
<details>
<summary>Click here to see more details</summary>
<p>This is the content that will be shown or hidden when the user clicks on the summary.</p>
</details>
When this code is rendered in a browser, the user will see a clickable heading that says "Click here to see more details." When the heading is clicked, the paragraph will be revealed or hidden.
Attributes
-
open
: The<details>
element can have anopen
attribute which, when present, indicates that the details are visible by default. For example:
<details open>
<summary>Click here to see more details</summary>
<p>This content is visible by default.</p>
</details>
Styling
Both <details>
and <summary>
can be styled using CSS to match the look and feel of your website. For instance:
summary {
font-weight: bold;
cursor: pointer;
}
details[open] summary {
color: blue;
}
details p {
margin-left: 20px;
}
Practical Use Cases
Frequently Asked Questions (FAQs)
A common use case for <details>
and <summary>
is creating a list of frequently asked questions where each question can be expanded to reveal the answer.
<details>
<summary>What is your return policy?</summary>
<p>Our return policy lasts 30 days. If 30 days have gone by since your purchase, unfortunately, we can’t offer you a refund or exchange.</p>
</details>
<details>
<summary>Do you ship internationally?</summary>
<p>Yes, we ship to over 100 countries worldwide. Shipping costs will apply, and will be added at checkout.</p>
</details>
Spoiler Alerts
For content that may contain spoilers, such as book reviews or movie discussions, <details>
and <summary>
provide a way to hide spoilers until the user chooses to reveal them.
<details>
<summary>Click to reveal spoilers for "The Great Gatsby"</summary>
<p>Gatsby dies at the end.</p>
</details>
Additional Information
When you want to provide supplementary information without cluttering the main content, <details>
can be a useful tool.
<details>
<summary>Technical Specifications</summary>
<ul>
<li>Processor: Intel i7</li>
<li>RAM: 16GB</li>
<li>Storage: 512GB SSD</li>
</ul>
</details>
Code Examples
Developers often use <details>
and <summary>
to show and hide code examples in tutorials and documentation.
<details>
<summary>Show HTML Example</summary>
<pre>
<code>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
</code>
</pre>
</details>
Accessibility Considerations
The <details>
and <summary>
elements are inherently accessible because they provide built-in keyboard interaction and are recognized by screen readers. However, it's important to ensure that the summary text is descriptive enough to make sense out of context. Additionally, you should test these elements across different browsers to ensure consistent behavior.
Conclusion
The <details>
and <summary>
elements are versatile tools for creating interactive, accessible, and neatly organized content on your web pages. Whether you're designing a FAQ section, hiding spoilers, providing additional information, or displaying code examples, these elements offer a simple and effective solution. By incorporating <details>
and <summary>
into your HTML, you can enhance the user experience and keep your content organized and engaging.
Experiment with these elements in your projects and see how they can improve the interactivity and usability of your web pages. Happy coding!
Top comments (0)