Everything you need as a full stack developer

HTML-Only Accordions and Tabs with `<details>` and `<summary>`

- Posted in HTML by

TL;DR HTML's <details> and <summary> elements can revolutionize interactive content creation, enabling developers to build accordions and tabs without JavaScript. Introduced in HTML5, these elements provide a native way to create collapsible content with a summary or title. By leveraging these underutilized elements, developers can create accessible and lightweight content that works natively in modern browsers.

Unlocking HTML's Hidden Gems: Accordions and Tabs with <details> and <summary>

As full-stack developers, we often find ourselves caught up in the whirlwind of JavaScript frameworks and libraries that promise to simplify our lives. However, in the process, we might overlook some of the most powerful tools hidden within plain old HTML. Today, we're going to explore two underutilized elements that can revolutionize the way we create interactive content: <details> and <summary>. Get ready to unlock a world of possibilities for building accordions and tabs without writing a single line of JavaScript.

What are <details> and <summary>?

Introduced in HTML5, the <details> element represents a widget from which the user can obtain additional information or controls on demand. This element is often used to create accordions, where users can expand or collapse content as needed.

The <summary> element, on the other hand, provides a summary or title for the details that follow. When a user clicks on the summary, the corresponding details are revealed.

Basic Syntax

To get started with creating HTML-only accordions and tabs using <details> and <summary>, you'll need to use the following basic syntax:

<details>
  <summary>Summary or Title</summary>
  <!-- Additional content or controls go here -->
</details>

Here, the <summary> element serves as a toggle for the details that follow. By default, the details are collapsed, and only the summary is visible.

Creating Accordions

One of the most common use cases for <details> and <summary> is creating accordions. Imagine you're building a frequently asked questions (FAQ) page, where users can expand or collapse individual questions to reveal their answers.

<details>
  <summary>What is HTML?</summary>
  <p>HTML stands for HyperText Markup Language, which is used to create web pages.</p>
</details>

<details>
  <summary>What is CSS?</summary>
  <p>CSS stands for Cascading Style Sheets, which is used to control the layout and visual styling of web pages.</p>
</details>

<details>
  <summary>What is JavaScript?</summary>
  <p>JavaScript is a high-level programming language that's commonly used for client-side scripting on the web.</p>
</details>

In this example, each accordion item uses a <details> element with a corresponding <summary> that serves as a toggle. When a user clicks on a summary, the associated details are revealed.

Creating Tabs

Another creative way to use <details> and <summary> is by creating tabs. By using multiple <details> elements with unique summaries, you can create a tabbed interface without writing any JavaScript.

<details>
  <summary>Tab 1</summary>
  <!-- Content for Tab 1 goes here -->
</details>

<details>
  <summary>Tab 2</summary>
  <!-- Content for Tab 2 goes here -->
</details>

<details>
  <summary>Tab 3</summary>
  <!-- Content for Tab 3 goes here -->
</details>

To make the tabs work, you can add some basic CSS to target the open state of each <details> element.

details > summary {
  list-style: none;
}

details[open] > summary {
  background-color: #ccc;
}

Benefits and Limitations

Using <details> and <summary> for accordions and tabs offers several benefits:

  • Native support: These elements are part of the HTML specification, which means they work natively in modern browsers without requiring additional libraries or frameworks.
  • Accessibility: The <details> element is designed to provide a clear summary of its contents, making it an excellent choice for accessibility-focused development.
  • Lightweight: By leveraging native HTML elements, you can create interactive content without adding unnecessary JavaScript overhead.

However, there are some limitations to consider:

  • Limited customization: While you can style the <details> and <summary> elements using CSS, their functionality is limited compared to more robust accordion or tab libraries.
  • Browser support: Although these elements work in modern browsers, older versions might not support them. Be sure to test your implementation across different browser versions.

Conclusion

In this article, we've explored the basics of HTML-only accordions and tabs using <details> and <summary>. By embracing these underutilized elements, you can create interactive content that's both accessible and lightweight. While there are limitations to consider, the benefits of native support, accessibility, and reduced JavaScript overhead make these elements an excellent choice for modern web development.

So next time you're building a project, take a step back and ask yourself: "Can I achieve this with plain old HTML?" You might be surprised at what you can accomplish without relying on complex libraries or frameworks.

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more