Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Published
2 min read
Up to date

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

HTML Details Element: The Native Accordion You're Not Using

Discover how the HTML details element can replace your JavaScript accordions and why it might be better than your current solution

The HTML <details> element might be one of the most underappreciated native features we have. I’m constantly surprised by how many developers reach for JavaScript solutions when they need to show/hide content, completely overlooking this built-in option.

The <details> element creates an expandable widget that users can open and close. Think of it as a native accordion - no JavaScript required. Here’s the basic syntax:


That’s it. No JavaScript. No CSS. No aria attributes to manage. The browser handles all the accessibility features, keyboard navigation, and state management for you.

<details> Built-in Features

  • Screen reader announcements for expanded/collapsed states
  • Keyboard navigation using Enter and Space keys
  • Proper focus management
  • Semantic HTML structure
  • State management without JavaScript
The <details> element shines when building documentation, FAQs, or navigation menus.
What is the details element?
A native HTML element that creates expandable widgets without requiring JavaScript. Perfect for accordions, FAQs, and collapsible sections.
Why use it?
Built-in accessibility, keyboard navigation, and state management make it a robust choice compared to custom JavaScript solutions.
Browser support?
Excellent support across modern browsers, with easy fallback options for legacy environments.

Code implementation with Tailwind

While the <details> element works perfectly out of the box, this styled version demonstrates how it can be enhanced with CSS techniques while maintaining its native functionality. The beauty lies in its progressive enhancement - starting with semantic HTML, adding thoughtful styling, and preserving all built-in accessibility features.

Next time you reach for a JavaScript accordion, consider whether this native HTML element might serve your needs better. Sometimes the best solutions are already built-in!

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
3 min read

Form Validation That Doesn't Annoy Users: CSS :user-valid and :user-invalid

The new pseudo-classes :user-valid and :user-invalid give us a smarter way to style form validation states based on user interaction

Dec 12, 2024
Read article
Webdev
3 min read

scrollbar-width & scrollbar-gutter: CSS Properties for Layout Control

Prevent content shifts and refine scrollable UIs with scrollbar-width and scrollbar-gutter

Dec 19, 2024
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article
Webdev
3 min read

CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Dec 17, 2024
Read article
Webdev
3 min read

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Dec 5, 2024
Read article
Webdev
3 min read

CSS Supports Nesting Now

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Dec 6, 2024
Read article
Webdev
4 min read

Explicit is better than implicit

Clarity is key: being explicit makes your code more readable and maintainable.

Sep 4, 2024
Read article
Webdev
2 min read

link rel='modulepreload': Optimize JavaScript Module Loading

The rel='modulepreload' indicates that a module script should be fetched, parsed, and compiled preemptively, and stored for later execution

Dec 4, 2024
Read article
Webdev
4 min read

Open Dyslexic Font: Improve Your Web Accessibility

How to implement the Open-Dyslexic font to enhance readability for users with dyslexia

Oct 12, 2024
Read article

Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement. By using my affiliate links, you support my work and get a discount at the same!


This article was originally published on https://www.trevorlasn.com/blog/html-details-element. It was written by a human and polished using grammar tools for clarity.