HTML <details> Element

Publish in HTML Tutorial el 23/05/2025 20:21

The <details> element creates a disclosure widget that contains information that is initially hidden but can be revealed by the user. It's a native HTML way to create collapsible content without JavaScript. The element works with a nested <summary> tag that serves as the clickable header.


Basic Syntax

Here's the basic structure of a <details> element:


And here's how it renders:

Play Code


Examples

1. Basic FAQ Section

A common use case for <details> is creating FAQ sections without JavaScript.

Play Code


2. Nested Details Elements

You can nest <details> elements to create hierarchical structures.

Play Code


3. Details with Custom Styling

The <details> element can be styled with CSS for better visual appearance.

Play Code


4. Details with JavaScript Interaction

You can enhance <details> with JavaScript for additional functionality.

Play Code


5. Accordion with JavaScript

Create an accordion where only one item can be open at a time.

Play Code


Tips and Tricks

  • Use the open attribute to make a details element expanded by default: <details open>
  • The toggle event fires when the state changes between open and closed
  • You can style the marker (triangle icon) using ::marker pseudo-element
  • For better accessibility, ensure your summary text clearly indicates there's expandable content
  • Consider adding a small transition for the content reveal for better UX


Browser Support

The <details> element is supported in all modern browsers, but for older browsers like Internet Explorer, you might need a polyfill. Always test your implementation across target browsers.

También te puede interesar

HTML <style> Element
HTML <style> Element

The <style> HTML element contains style information for a document, or part of a document. ...

HTML <strong> Element
HTML <strong> Element

The <strong> element is used to indicate that its content has strong importance, seriousnes...

HTML <span> Element
HTML <span> Element

The <span> element is an inline container used to mark up a part of a text or document. Unl...

HTML <source> Element
HTML <source> Element

The <source> element is used to specify multiple media resources for media elements like &l...

HTML <small> Element
HTML <small> Element

The <small> element is used to represent side-comments and small print, typically for discl...

HTML <section> Element
HTML <section> Element

HTML <section> Element: The Complete Guide The <section> element is a semantic HTML ...