Six years ago, I explored the native 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
Andrew Bone ? Jan 4 '19
In this article, we'll revisit
The Basics: and
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>
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
<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>
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; }
How It Works
- 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.
- 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 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

CSSismostlycase-insensitive,butURLsandfontfamilynamesarecase-sensitive.1)Propertiesandvalueslikecolor:red;arenotcase-sensitive.2)URLsmustmatchtheserver'scase,e.g.,/images/Logo.png.3)Fontfamilynameslike'OpenSans'mustbeexact.

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

CSScounterscanautomaticallynumbersectionsandlists.1)Usecounter-resettoinitialize,counter-incrementtoincrease,andcounter()orcounters()todisplayvalues.2)CombinewithJavaScriptfordynamiccontenttoensureaccurateupdates.

In CSS, selector and attribute names are case-sensitive, while values, named colors, URLs, and custom attributes are case-sensitive. 1. The selector and attribute names are case-insensitive, such as background-color and background-Color are the same. 2. The hexadecimal color in the value is case-sensitive, but the named color is case-sensitive, such as red and Red is invalid. 3. URLs are case sensitive and may cause file loading problems. 4. Custom properties (variables) are case sensitive, and you need to pay attention to the consistency of case when using them.

Theconic-gradient()functioninCSScreatescirculargradientsthatrotatecolorstopsaroundacentralpoint.1.Itisidealforpiecharts,progressindicators,colorwheels,anddecorativebackgrounds.2.Itworksbydefiningcolorstopsatspecificangles,optionallystartingfromadefin
