LEARNING OBJECTIVES โต
- Implement native, zero-JavaScript exclusive accordions using the HTML
nameattribute on<details>. - Understand the browser-level mutual exclusivity mechanics of named disclosure groups.
- Master in-page searchability (
Ctrl+F) integration with thebeforematchevent andhidden="until-found". - Design robust, responsive accordion architectures for enterprise design systems.
๐ The Mental Model & Story (Intuitive Foundation)
Imagine a physical car radio dashboard from the 1980s with mechanical preset buttons.
- When you press button 1 (Jazz 89.5), it pushes in and locks in place.
- When you later press button 2 (Rock 101.1), a mechanical lever inside the radio automatically pops button 1 back out. Only one radio station can ever be tuned at a time.
- You didn't need an electronic computer or external wiring to pop button 1 out; the physical mechanical linkage inside the radio housing handled the mutual exclusivity automatically.
For years in web development, creating an "exclusive accordion" (where opening one accordion section automatically closes any currently open section) required JavaScript event listeners, looping through sibling elements with querySelectorAll, and removing attributes manually.
The modern HTML Living Standard introduced the name attribute for <details>. By simply assigning the same name="faq-group" to multiple <details> elements, the browser acts as that mechanical radio linkage: opening one details element automatically closes all other sibling details sharing that name with zero JavaScript.
+-----------------------------------------------------------------------------+
| NATIVE EXCLUSIVE ACCORDION (name="faq") |
+-----------------------------------------------------------------------------+
| |
| <details name="faq" open> |
| โผ What is your SLA uptime? |
| "We guarantee 99.99% multi-region uptime." |
| |
| <details name="faq"> |
| โถ How does data replication work? <-- User clicks this |
| |
| <details name="faq"> |
| โถ Can I cancel anytime? |
| |
+-----------------------------------------------------------------------------+
|
(Browser automatically pops previous item closed)
v
+-----------------------------------------------------------------------------+
| <details name="faq"> |
| โถ What is your SLA uptime? <-- Automatically closed by browser! |
| |
| <details name="faq" open> |
| โผ How does data replication work? <-- Now open |
| "Data is synchronously mirrored across 3 availability zones." |
| |
| <details name="faq"> |
| โถ Can I cancel anytime? |
+-----------------------------------------------------------------------------+
Technical Deep Dive & Specifications
The name Attribute on <details>
The WHATWG HTML Living Standard specifies:
The
namecontent attribute gives the name of the group of details elements that this element belongs to. If multiple<details>elements in the same tree have the samenameattribute value, at most one of them can have theopenattribute set at any given time.
interface HTMLDetailsElement : HTMLElement {
[CEReactions] attribute boolean open;
[CEReactions] attribute DOMString name; // Group name property
};
<!-- Native Exclusive Accordion Group (Zero JavaScript) -->
<details name="account-settings">
<summary>Personal Information</summary>
<p>Update your email address and profile photo.</p>
</details>
<details name="account-settings">
<summary>Security & Password</summary>
<p>Manage two-factor authentication and passwords.</p>
</details>
<details name="account-settings">
<summary>Billing & Subscriptions</summary>
<p>Download recent invoices and payment receipts.</p>
</details>
In-Page Find (Ctrl+F) & the beforematch Event
A major flaw of legacy JavaScript accordions (display: none or CSS height clipping) is that when users press Ctrl+F (Cmd+F on macOS) to search for text on a page, words inside closed accordions are invisible to the browser's find tool.
Modern browser rendering engines solve this with hidden="until-found" and native <details> integration:
- When a user searches for text contained inside a collapsed
<details>element, the browser engine detects the text match. - The browser dispatches a native
beforematchevent to the<details>element. - The browser automatically sets
open="true"on the matching<details>element, smoothly revealing the text and scrolling the user directly to the highlighted match.
const detailsItem = document.querySelector('details');
detailsItem.addEventListener('beforematch', () => {
console.log('Search match detected! Browser is auto-expanding this widget.');
});
Pure HTML vs Legacy JavaScript Accordion Comparison
| Feature | Modern HTML (<details name="...">) |
Legacy JavaScript Accordion |
|---|---|---|
| JavaScript Dependency | Zero JavaScript | Requires event listeners & query loops |
| Mutual Exclusivity | Built-in via name="group-name" |
Handled by custom JS state management |
| SSR / No-JS Resilience | 100% functional before scripts execute | Non-functional or broken until JS hydrates |
Find-in-Page (Ctrl+F) |
Auto-expands on match (beforematch) |
Matches are completely missed or hidden |
| Keyboard Accessibility | Native Tab, Enter, Space | Requires manual tabindex and aria-expanded |
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Lines 73, 87, 101: Applies
name="cloud-faq"to all three<details>elements. This binds them into an exclusive group governed by the browser engine. - Line 73: The first item specifies
open. On page load, this item starts open while the others remain closed. - Lines 49โ59: Combines the native
nameattribute with the CSS Grid0frto1frtransition for a zero-JS animated exclusive accordion. - No JavaScript Needed: When the user clicks Item 2 or 3, the browser automatically closes the currently open item without a single line of script.
Expected Browser Render Output
- Initial Load: Item 1 is expanded with its text visible; Items 2 and 3 are collapsed.
- Clicking Item 2: Item 1 smoothly collapses down to 0px, and Item 2 simultaneously expands to reveal its DDoS mitigation description.
๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Build an Enterprise Product Feature Matrix
Construct an enterprise product comparison matrix with two distinct, independent exclusive accordion groups:
- Group 1: Infrastructure Architecture (
name="infra-group"):- Section A: "Global CDN Distribution" (Open by default)
- Section B: "Serverless Compute Runners"
- Group 2: Security & Compliance (
name="security-group"):- Section A: "SOC 2 Type II Certification" (Open by default)
- Section B: "HIPAA & GDPR Compliance"
- Verify that opening an item in Group 1 does not close items in Group 2, but closes other items within Group 1.
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Mismatched Group Names: Accordion exclusivity relies on strict string matching of the
nameattribute. A typo likename="faq"on one andname="faqs"on another will break exclusivity. - Using Non-Standard JavaScript Click Handlers on Named Details: If you attach custom
clickhandlers that calle.preventDefault(), you may interfere with the browser's native mutual-exclusion state updates. - Assuming
namePrevents All Items From Closing: Unlike radio buttons (<input type="radio">) which must have one item checked,<details name="...">allows the user to close all items in the group if desired.
๐ก Pro Tips
- Find-in-Page Integration: Because
<details>natively supportsbeforematch, users searching with Ctrl+F will automatically expand the correct accordion pane, delivering superior UX over custom React/Vue accordion libraries. - Check Browser Baseline: The
nameattribute on<details>is supported across modern baseline browsers (Chrome 120+, Safari 17.2+, Firefox 125+). For legacy browsers, a tiny 5-line JS polyfill can query[name]attributes on toggle events.
๐ Key Takeaways
- The
nameattribute on<details>creates native exclusive accordions with zero JavaScript. - Opening one named
<details>automatically collapses any other open<details>sharing that samename. - Multiple independent accordion groups can coexist on the same page by using different
namevalues. - Native
<details>automatically expands when browser search matches text inside (beforematchevent). - Users can still collapse all items in a group if they wish.
- --