Accordion Without JavaScript
The browser has a built-in accordion: <details> and <summary>. Add the name attribute and only one item opens at a time; add a few lines of CSS and it even animates. Keyboard-accessible, indexable, zero JavaScript.
- JavaScript
- 0 KB
- Built with
<details>,name,::details-content- Browsers
- All current browsers; animation in Chrome and Edge
- Updated
No. Put each item in a <details> element with a <summary> as its clickable heading. The browser handles opening, closing, keyboard use and screen-reader announcements. Give several items the same name and only one can be open at a time.
Live demo: two kinds of accordion
Both use the same HTML. On the left, all four items share name="ui-faq" — opening one closes the others. On the right, the name is left out, so any number can be open. The answers are real, with links to our free tools for Chinese websites.
One at a time — with name
Which font should a Chinese website use?
Usually no web font at all. Phones and computers in China already have good Chinese fonts — PingFang SC on Apple devices, Microsoft YaHei on Windows. A font stack that lists them loads instantly.
Simplified or Traditional Chinese?
Simplified Chinese for mainland China and Singapore, Traditional Chinese for Taiwan, Hong Kong and Macau. Mixing both on one page looks careless to native readers.
Chinese characters or Pinyin in URLs?
Pinyin. Chinese characters in a URL are percent-encoded when the link is copied, so the address turns into a long string of codes that is hard to read and share.
How long can a title be in Baidu's results?
Baidu cuts titles by pixel width, not by a fixed number of characters — and the limit differs between desktop and phones. Preview it before you publish.
Several open — without name
Which font should a Chinese website use?
Usually no web font at all. Phones and computers in China already have good Chinese fonts — PingFang SC on Apple devices, Microsoft YaHei on Windows. A font stack that lists them loads instantly.
Simplified or Traditional Chinese?
Simplified Chinese for mainland China and Singapore, Traditional Chinese for Taiwan, Hong Kong and Macau. Mixing both on one page looks careless to native readers.
Chinese characters or Pinyin in URLs?
Pinyin. Chinese characters in a URL are percent-encoded when the link is copied, so the address turns into a long string of codes that is hard to read and share.
How long can a title be in Baidu's results?
Baidu cuts titles by pixel width, not by a fixed number of characters — and the limit differs between desktop and phones. Preview it before you publish.
How the HTML accordion works
<details>holds the stateClosed by default, open with the
openattribute. The browser adds and removes that attribute when the visitor clicks — so CSS can style both states withdetails[open].<summary>is a real buttonIt is focusable, opens with Enter or Space, and screen readers announce it as expanded or collapsed. You get all of that without writing a line of ARIA.
namemakes it exclusiveItems that share a
namework like radio buttons: opening one closes the others. Supported in all major browsers since 2024; older ones simply allow several open.- A chevron without text
The default triangle is removed with
list-style: none. The new arrow is two borders of an empty pseudo-element, rotated — nothing that a screen reader would read aloud. - Smooth opening as a bonus
interpolate-size: allow-keywordslets CSS animate fromheight: 0toauto, and::details-contenttargets the answer. Chrome and Edge animate; other browsers open instantly. - Find-in-page opens it
In Chrome and Edge, searching the page with Ctrl/⌘+F opens a closed item when the text is found inside it. A JavaScript accordion can't do that.
Copy the HTML and CSS
This is the “one at a time” version from the demo. For an accordion where several items can be open, delete the name attributes. If you have two accordions on one page, give each its own name.
<div class="ui-accordion">
<details name="ui-faq" open>
<summary>Which font should a Chinese website use?</summary>
<div class="ui-accordion__body">
<p>Usually no web font at all. Phones and computers in China already have good Chinese fonts — PingFang SC on Apple devices, Microsoft YaHei on Windows. A font stack that lists them loads instantly.</p>
<p><a href="/tools/chinese-font-stack-generator/">Generate a Chinese font stack</a></p>
</div>
</details>
<details name="ui-faq">
<summary>Simplified or Traditional Chinese?</summary>
<div class="ui-accordion__body">
<p>Simplified Chinese for mainland China and Singapore, Traditional Chinese for Taiwan, Hong Kong and Macau. Mixing both on one page looks careless to native readers.</p>
<p><a href="/tools/detect-chinese/">Check which script a text uses</a></p>
</div>
</details>
<details name="ui-faq">
<summary>Chinese characters or Pinyin in URLs?</summary>
<div class="ui-accordion__body">
<p>Pinyin. Chinese characters in a URL are percent-encoded when the link is copied, so the address turns into a long string of codes that is hard to read and share.</p>
<p><a href="/tools/pinyin-slug-generator/">Turn Chinese titles into Pinyin slugs</a></p>
</div>
</details>
<details name="ui-faq">
<summary>How long can a title be in Baidu's results?</summary>
<div class="ui-accordion__body">
<p>Baidu cuts titles by pixel width, not by a fixed number of characters — and the limit differs between desktop and phones. Preview it before you publish.</p>
<p><a href="/tools/baidu-serp-preview/">Preview your title in Baidu</a></p>
</div>
</details>
</div>/* Accordion — no JavaScript.
Native <details>/<summary>. Items with the same "name"
behave as a group: opening one closes the others. */
.ui-accordion {
--ui-bg: #ffffff;
--ui-text: #1b1f3b;
--ui-muted: #4b5070;
--ui-accent: #4f46e5;
--ui-hover: #f5f6ff;
--ui-border: #e3e5f0;
background: var(--ui-bg);
border-radius: 12px;
font: 400 0.9375rem/1.6 system-ui, -apple-system, "Segoe UI", sans-serif;
color: var(--ui-text);
}
.ui-accordion details + details {
border-top: 1px solid var(--ui-border);
}
/* The clickable question */
.ui-accordion summary {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 1rem 1.25rem;
font-weight: 600;
cursor: pointer;
list-style: none; /* hides the default triangle */
}
.ui-accordion summary::-webkit-details-marker {
display: none; /* older Safari */
}
.ui-accordion summary:hover {
background: var(--ui-hover);
}
.ui-accordion summary:focus-visible {
outline: 2px solid var(--ui-accent);
outline-offset: -2px;
border-radius: 12px;
}
/* Chevron drawn with borders: no text, so nothing for screen readers to read */
.ui-accordion summary::after {
content: "";
flex-shrink: 0;
width: 0.55rem;
height: 0.55rem;
margin-top: -0.3rem;
border-right: 2px solid currentColor;
border-bottom: 2px solid currentColor;
transform: rotate(45deg);
transition: transform 0.2s ease;
}
.ui-accordion details[open] > summary::after {
margin-top: 0.2rem;
transform: rotate(-135deg);
}
/* The answer */
.ui-accordion__body {
padding: 0 1.25rem 1.1rem;
color: var(--ui-muted);
}
.ui-accordion__body p {
margin: 0 0 0.6rem;
}
.ui-accordion__body p:last-child {
margin-bottom: 0;
}
.ui-accordion__body a {
color: var(--ui-accent);
font-weight: 600;
}
/* Smooth opening in browsers that can animate to "auto" height.
Everywhere else the accordion simply opens instantly. */
@supports (interpolate-size: allow-keywords) {
.ui-accordion {
interpolate-size: allow-keywords;
}
.ui-accordion details::details-content {
height: 0;
overflow: hidden;
transition: height 0.3s ease, content-visibility 0.3s allow-discrete;
}
.ui-accordion details[open]::details-content {
height: auto;
}
}The CSS sets its own spacing and list styles, so it also works inside a page with a global reset. All class names start with ui-.
Is content in a closed accordion indexed?
Yes. The answers are in the HTML from the first byte, so Google and Baidu read them like any other text on the page. Google has said that, with mobile-first indexing, content in accordions and tabs is treated like visible content — on phones, collapsing text is simply good design.
The real risk is accordions that load their answers only when clicked, via JavaScript. Then the text isn't in the HTML at all, and a crawler that doesn't click — which is every crawler — never sees it. Baidu and many AI crawlers don't run JavaScript reliably, so for them the content doesn't exist.
FAQ sections in accordions are also exactly the kind of content AI answers like to quote: a clear question, a short answer. Keep them in the HTML and they can be found by search engines and AI assistants alike. (Google's FAQ rich results, by the way, have been limited to a small number of authoritative government and health sites since 2023 — the content still counts, the stars in the results don't come back.)
Accessibility and browser support
| Situation | Without JS | Details |
|---|---|---|
| Keyboard | Works | Tab to a question, Enter or Space to open and close. |
| Screen readers | Works | Announced as a button with its expanded or collapsed state. |
One at a time (name) | 2024+ | Chrome/Edge 120+, Safari 17.2+, Firefox 130+. Older browsers allow several items open — nothing breaks. |
| Smooth animation | Chrome, Edge | Other browsers open and close instantly. |
| Find in page | Chrome, Edge | Closed items open when the search text is inside them. |
Headings in <summary> | Careful | A summary is a button, and many screen readers don't announce a heading placed inside it. Keep page headings outside the accordion. |
- “Expand all / collapse all” buttons — a few lines that set or remove
openon every item. - Remembering which item was open after a reload, or opening an item from a link like
#shipping. - Analytics on which questions people open — listen for the
toggleevent. All of these sit on top; the accordion keeps working without them.
Questions about HTML accordions
Can you build an accordion without JavaScript?
Yes, with <details> and <summary>. They work in every current browser, with keyboard and screen-reader support built in.
How do I allow only one open item at a time?
Give all <details> elements of the group the same name attribute. Opening one then closes the others — no script needed.
Can the accordion animate without JavaScript?
Yes, in Chrome and Edge: interpolate-size: allow-keywords plus a height transition on ::details-content. Wrapped in @supports, other browsers keep the instant open and close.
Is text in a closed accordion bad for SEO?
No. If it is in the HTML, Google and Baidu index it. Only content that is loaded by JavaScript after a click is at risk.
Accordion or tabs?
Accordions work better on phones and when people may want to read several sections. Tabs suit short, parallel content that fits one screen, like product variants. See the tabs pattern.
This FAQ uses the same technique — no JavaScript.
Want your answers quoted by Baidu and Chinese AI assistants?
Clear questions with short answers, in the HTML, are what search engines and AI answers pick up. We plan and write that content in native Chinese, and run Chinese GEO and Baidu SEO for Western companies.
CSS-First UI Patterns are written and maintained by Jademond Digital, a Chinese digital marketing agency founded in Guangzhou in 2012. We build and audit websites for Baidu, Google and AI search — which is why we care what works without JavaScript. About us →