Table of Contents
Native HTML: Accordion
Andrew Bone ・ Jan 4 '19
Home Web Front-end CSS Tutorial Native HTML: Accordion Revisited

Native HTML: Accordion Revisited

Jan 07, 2025 am 06:45 AM

Six years ago, I explored the native

and elements to create accessible accordions. Since then, the web platform has evolved, introducing exciting new features like exclusive open behaviour and smooth animations for these elements.

Native HTML: Accordion Revisited

Native HTML: Accordion

Andrew Bone ・ Jan 4 '19

#html #css #a11y #design

In this article, we'll revisit

and make the most of modern CSS properties to add polish to your accordions. I'll also share a demo implementation showcasing these features.

The Basics:
and

The

element provides a native way to create toggleable sections in HTML, with the element acting as the clickable label. This makes it easy to create disclosure widgets with minimal effort.

Here’s a simple example:

<details>
  <summary>Read more</summary>
  Some text to be hidden. 
</details>
Copy after login

Clicking the summary toggles the visibility of the associated content. No JavaScript required!

Enhancements: Exclusive Open Behaviour

To mimic traditional accordion behaviour, where only one section is open at a time, you can use the name attribute on your

elements. When
elements share the same name, opening one automatically closes the others in the group.

<details name="exclusive">
  <summary>Section 1</summary>
  <p>Content for section 1.</p>
</details>
<details name="exclusive">
  <summary>Section 2</summary>
  <p>Content for section 2.</p>
</details>
Copy after login

This behaviour is native and works seamlessly in modern browsers!

Adding Smooth Animations with CSS

To make the opening and closing transitions smoother, we can use modern CSS properties like interpolate-size and transition-behavior.

Key Properties

  • interpolate-size: Allows animating between intrinsic sizes (like auto) and fixed sizes. This property is currently only supported in Chrome.
  • transition-behavior: When set to allow-discrete, properties that normally can't be animated like visibility and display wait rather than instantly updating.

Example Styling

Here’s a complete example of the CSS used in the demo:

details {
  interpolate-size: allow-keywords;
  overflow: clip;
  margin-top: 0.125em;
  border: 1px solid #dddddd;
  background: #ffffff;
  color: #333333;
  border-radius: 3px;
}

details summary {
  display: block;
  cursor: pointer;
  position: relative;
  padding: 0.5em 0.5em 0.5em 0.7em;
  background: #ededed;
  color: #2b2b2b;
  border-radius: 3px 3px 0 0;
}

details:not([open]) summary:hover,
details:not([open]) summary:focus {
  background: #f6f6f6;
  color: #454545;
}

details[open] summary {
  outline: 1px solid #003eff;
  background: #007fff;
  color: #ffffff;
}

details[open]::details-content {
  height: auto;
}

details::details-content {
  height: 0;
  overflow-y: clip;
  transition: content-visibility 475ms allow-discrete, height 475ms;
}

details main {
  padding: 1em 2.2em;
}
Copy after login

How It Works

  1. Height Animation: The interpolate-size property allows smooth transitions between height: 0 (closed) and height: auto (open). However, this is currently supported only in Chrome.
  2. Visibility Transition: The transition-behavior property ensures the visibility change appears seamless.

The Demo: Bringing It All Together

Here’s the full implementation:

Browser Support

  • interpolate-size: Currently only supported in Chrome.
  • transition-behavior: Supported in most modern browsers.

For browsers without support, the animations gracefully fall back, and the accordion remains functional without the smooth transitions.

Conclusion

The

and elements, combined with modern CSS, provide a lightweight and accessible solution for creating interactive accordions. These new enhancements make them even more appealing for modern web projects. Try out the demo and give your accordions a fresh, polished look!

Thanks so much for reading. If you'd like to connect with me outside of Dev here are my twitter, bsky and linkedin come say hi ?

The above is the detailed content of Native HTML: Accordion Revisited. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles