By · Updated

Expandable Card CSS

Collapsible Card Styling With details and summary, No JavaScript Needed

No ratings yet

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the Expandable Card works

This expandable card CSS builder makes cards that open in place. An expandable card shows a heading and a one-line teaser, and opens to reveal more when clicked. This one is built on the browser's own <details> element, so it is keyboard accessible and works with no JavaScript. Style it, preview it open or closed, and copy the HTML and CSS.

Expandable card CSS: details and summary

Collapsible content used to mean a JavaScript widget, an ARIA pattern to get right and a dozen edge cases around focus and screen readers. The <details> and <summary> elements do all of that in the browser: clicking or pressing Enter on the summary toggles the content, the expanded state is announced to assistive technology, and the open attribute reflects the state in the DOM. The job left for CSS is to make it look like a card rather than a disclosure triangle.

Removing the default marker. Browsers draw a triangle before the summary text. Setting list-style: none on the summary removes it in Firefox and Chromium, and the ::-webkit-details-marker rule handles Safari. With the marker gone, the summary becomes a flex row that can hold a title, a subtitle and your own icon at the end.

The icon is a pseudo-element. The chevron and plus options are rendered by ::before on a small circular badge, and the [open] attribute selector swaps or rotates it. A chevron rotates 90 degrees; a plus becomes a minus by changing content. The icon is aria-hidden because the state is already conveyed by the details element itself — a screen reader will say "expanded" or "collapsed" without any help from the glyph.

Why the open animation is a fade, not a slide. Height animation on <details> is notoriously awkward because the content goes from display: none to visible in one step. Newer browsers can animate it with interpolate-size: allow-keywords, but support is still uneven. A short fade and six-pixel rise on the body gives the open state a sense of motion that works everywhere, and the reduced-motion query turns it off for visitors who have asked for that.

Preview open or closed. The Preview control toggles the open attribute in the sample markup so you can style both states. You can also just click the card in the preview. Only the initial state is baked into the copied HTML; remove the open attribute if you want the card collapsed on page load.

Grouping several cards. Stack multiple expandable cards for an FAQ or a feature list. If you want only one open at a time, give every <details> the same name attribute — recent browsers treat that as an exclusive accordion natively, again without a script.

Focus and contrast. The summary shows an accent-coloured outline on keyboard focus so the control is visible when tabbed to. The subtitle sits at 70% opacity to read as secondary; if your text and background colours are close, check that it still meets contrast requirements.

FAQ

Do I need JavaScript for this card?

No. The <details> element toggles itself when the summary is clicked or activated with the keyboard, and exposes the expanded state to assistive technology. JavaScript is only needed if you want to react to the change, for example to load content lazily or track analytics.

How do I make only one card open at a time?

Give each <details> element the same name attribute, such as name="faq". Browsers that support exclusive accordions will close the others automatically when one is opened. Older browsers ignore the attribute and simply allow several to be open, which is a safe fallback.

Why does the content not slide open smoothly?

Because the browser switches the content from hidden to shown in a single step, so there is no intermediate height to animate. Smooth height animation needs either the newer interpolate-size property or a JavaScript measurement of the content height. The generated fade is the dependable cross-browser alternative.

Can I put a link or button inside the summary?

Avoid it. The summary is itself an interactive control, and nesting another one inside it produces confusing behaviour for both mouse and keyboard users. Put links and buttons in the expanded body instead, where they behave normally.

Why is the default triangle still showing in Safari?

Safari uses a vendor pseudo-element for the marker. The snippet includes summary::-webkit-details-marker { display: none; } for exactly that reason; if the triangle is still visible, check that this rule was copied along with the rest of the CSS.

See also: all UI Components tools, Layout & Spacing, Shadows & Motion, Shapes & SVG, Forms & Inputs, Colour.

Related tools