惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
Y
Y Combinator Blog
IT之家
IT之家
博客园 - 聂微东
L
LangChain Blog
爱范儿
爱范儿
H
Help Net Security
GbyAI
GbyAI
F
Fortinet All Blogs
B
Blog
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
D
DataBreaches.Net
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享

MDN Blog

Introducing the MDN MCP server | MDN Blog Under the hood of MDN's new frontend | MDN Blog Image formats: Codecs and compression tools | MDN Blog A beginner-friendly guide to view transitions in CSS | MDN Blog Launching MDN's new front end | MDN Blog Image formats: Pixel data from encoders to decoders | MDN Blog Celebrating 20 years of MDN | MDN Blog Image formats: Color models for humans and devices | MDN Blog Default styles for h1 elements are changing | MDN Blog Implications of Global Privacy Control | MDN Blog JavaScript Temporal is coming | MDN Blog Fix your website's Largest Contentful Paint by optimizing image loading | MDN Blog MDN 2024 content projects | MDN Blog A new learning experience on MDN | MDN Blog Countdown to the holidays with daily coding challenges | MDN Blog Monitoring and optimizing website performance | MDN Blog How to land your first developer job | MDN Blog Introducing the new MDN Community page | MDN Blog Fixing your website's JavaScript performance | MDN Blog Get back to school! Supercharge your learning with MDN and Scrimba | MDN Blog Efficient data handling with the Streams API | MDN Blog Locale-sensitive text segmentation in JavaScript with Intl.Segmenter | MDN Blog Optimize your workflow with Git stash | MDN Blog How to debug mobile apps across devices | MDN Blog Exploring the Broadcast Channel API for cross-tab communication | MDN Blog MDN partners with Scrimba to enhance web development learning | MDN Blog Introducing the MDN HTTP Observatory | MDN Blog Static Site Generation (SSG) with Next.js | MDN Blog New JavaScript Set methods | MDN Blog Securing APIs: Express rate limit and slow down | MDN Blog
Exclusive accordions using the HTML details element | MDN...
Brian Smith August 5, 2024 3 minutes read · 2024-08-05 · via MDN Blog

The name attribute of the <details> element is soon to be supported across all major browsers. Firefox 130 should also have support, enabling most engines to handle common UI accordion patterns using only <details> elements. This is good news for developers who have been writing custom handling for these cases from scratch. Let's explore what the attribute allows you to do with some quick examples.

How does <details> work?

If you're unfamiliar with <details>, it's an element that creates a 'disclosure widget' in which information is visible only when the widget is toggled to an "open" state. You should include in it a <summary> element, which is used for the text describing the disclosure. If you omit the <summary>, a browser-specific string like "Details" will be used by default. Click the two elements below to toggle them between open and closed states and notice the missing <summary> in the first one:

<details>
  <p>
    The response phrase for 413 "Content Too Large" used to be "Payload Too
    Large", and this message is still widely used.
  </p>
</details>

<details>
  <summary>System configuration</summary>
  <ul>
    <li>200GB RAM</li>
    <li>4PB storage</li>
  </ul>
</details>

Since an accordion is a series of expandable sections that allow users to show or hide content, a series of <details> elements can be used to create an accordion-like interface. With the use of the name attribute, as we'll see later, these elements can closely mimic the behavior of common accordions, in which only one section can be open at a time, with other sections automatically closing when a new one is expanded.

There's great potential for <details> elements to fit into your pages because some minimal styling can go a long way to create interactive accordions using only HTML and CSS.

details {
  font-family: sans-serif;
  width: 50%;
}

summary {
  color: white;
  background-color: cornflowerblue;
  padding: 1rem;
  cursor: pointer;
}

summary:hover {
  background-color: darkslateblue;
}

p,
ul {
  padding: 15px;
  margin: 0 1rem;
  background-color: white;
}
<details>
  <summary>System configuration</summary>
  <ul>
    <li>200GB RAM</li>
    <li>4PB storage</li>
  </ul>
</details>

<details>
  <summary>Recommended settings</summary>
  <ul>
    <li>Extreme mode: on</li>
    <li>Raytracing: enabled</li>
  </ul>
</details>

<details>
  <summary>Other details</summary>
  <ul>
    <li>Material: Faux Leather, Metal</li>
    <li>Item Weight: 10.2Kg</li>
  </ul>
</details>

Using <details name> to create exclusive accordions

A common use case for accordions is that only one of them should be open at any one time. The addition of the name attribute lets you connect multiple <details> elements and make an accordion exclusive, so only one <details> element with the same name can be open at any time, and the browser toggles all others with the same name to a "closed" state. It's quite nice to be able to include this when you want to group multiple <details> elements and ensure exclusivity:

details {
  font-family: sans-serif;
  width: 50%;
}

summary {
  color: white;
  background-color: cornflowerblue;
  padding: 1rem;
  cursor: pointer;
}

summary:hover {
  background-color: darkslateblue;
}

p,
ul {
  padding: 15px;
  margin: 0 1rem;
  background-color: white;
}
<details name="tech-specs">
  <summary>System configuration</summary>
  <ul>
    <li>200GB RAM</li>
    <li>4PB storage</li>
  </ul>
</details>

<details name="tech-specs">
  <summary>Recommended settings</summary>
  <ul>
    <li>Extreme mode: on</li>
    <li>Raytracing: enabled</li>
  </ul>
</details>

<details name="tech-specs">
  <summary>Other details</summary>
  <ul>
    <li>Material: Faux Leather, Metal</li>
    <li>Item Weight: 10.2Kg</li>
  </ul>
</details>

An FAQ is a good use case for these types of exclusive accordions where the visitor can focus on one topic at a time:

details {
  font-family: sans-serif;
  width: 50%;
  border: 1px solid lightgrey;
  border-radius: 5px;
  margin: 0.5rem;
}

details:hover {
  box-shadow: 2px 2px 2px 1px #e6e6e6;
}

summary {
  margin: 0.5rem;
  padding: 0.5rem;
  cursor: pointer;
}

p {
  padding: 1rem;
  margin: 0;
  background-color: #e6e6e6;
}
<details name="faq">
  <summary>Can I request a refund?</summary>
  <p>
    Yes, you have up to 30 days to request a refund. See our T&C for details.
  </p>
</details>

<!-- etc. -->
<details name="faq">
  <summary>What data do you store?</summary>
  <p>
    We use some anonymized visitor stats for the purpose of improving the
    service reliability. See our Privacy Policy for more information.
  </p>
</details>

<details name="faq">
  <summary>Are all products organic?</summary>
  <p>
    We do our best to ensure all ingredients are organic, but some exceptions
    are listed on each product page under "ingredients".
  </p>
</details>

Further reading

If you want to learn more, you can have a read through these other resources:

Summary

With more support landing for this feature in more stable browsers, it's a good indicator that the web platform is providing better functionality for features that web developers have had to write themselves. If you're looking for a polyfill, there's a great post by Bramus, which includes a polyfill for browsers that don't support this yet.

I hope you enjoyed this post and you'll get more mileage out of the <details> element, which is a flexible and useful element for a lot of cases. Feel free to get in touch with us and let me know what you think or if I've missed something. Don't get too lost in the <details> and have fun!