Chapter 5: Accordion

How to create a proper accordion (also called collapsible sections, expand/collapse panels, FAQ style sections, etc.) from the very beginning.

What we want to achieve

An accordion is a group of panels where:

  • Only one panel is usually open at a time (classic behavior)
  • Clicking a header opens its content and closes others
  • Clicking an already open header closes it
  • Smooth open/close animation
  • Looks good on mobile and desktop
  • Is accessible (keyboard + screen reader friendly)

We’ll build three versions — from simple to more polished:

  1. Pure HTML + CSS only (no JavaScript – good for very simple cases)
  2. HTML + CSS + minimal JavaScript (most common & flexible)
  3. Modern version with nice animations & accessibility (what you’ll see in 2025–2026 professional sites)

Let’s go step by step.

Version 1 – Pure CSS Accordion (no JavaScript)

This version uses the :checked pseudo-class + hidden radio/checkbox trick.

HTML

HTML

CSS

CSS

Advantages

  • No JavaScript → very fast & simple
  • Works even with JS disabled

Big disadvantages

  • Only one item can be open at a time
  • You must set a fixed max-height (bad for dynamic content)
  • Harder to make “close all” behavior

So most real projects use JavaScript.

Version 2 – Classic JavaScript Accordion (most common approach)

HTML

HTML

CSS

CSS

JavaScript (clean & modern)

JavaScript

Important points:

  • We use scrollHeight → dynamic height (no fixed max-height needed)
  • We close others automatically (classic accordion behavior)
  • Icon changes from + → −

Version 3 – Modern + Accessible + Smooth (2025–2026 style)

Add these improvements:

  • Use <details> + <summary> (native HTML element!)
  • Add role, aria-expanded, aria-controls
  • Better animation with grid or height: auto + transition

HTML (recommended modern way)

HTML

CSS

CSS

Advantages of <details> + <summary>

  • Native HTML → no JavaScript needed for basic toggle
  • Automatically accessible
  • Works with keyboard (Enter/Space)
  • Screen readers understand it

Want multiple open at once? Just use <div> instead of <details> and control with JS.

Quick summary – which method to choose?

Goal Best method Needs JS? Multiple open? Accessibility Dynamic height?
Very simple, no JS Pure CSS (radio/checkbox) No No Medium No
Classic one-open-at-a-time JS + buttons Yes No Good Yes
Modern, accessible, minimal JS <details> + <summary> No Yes (default) Excellent Yes
Full control + animations JS with scrollHeight + classes Yes Optional Very good Yes

Your practice tasks

  1. Make an accordion where multiple sections can stay open
  2. Add a smooth height transition without fixed max-height
  3. Add + / − icons that rotate instead of change character
  4. Make the header change color when open
  5. Create nested accordions (accordion inside accordion)

Would you like me to show you any of these next steps with complete code?

Or would you like:

  • How to make it look like popular sites (Tailwind version, Material Design, etc.)
  • How to animate with CSS grid or height: auto
  • How to make it fully keyboard accessible
  • Common beginner mistakes
  • How to do it in React / Vue / plain JS frameworks

Just tell me what you want to go deeper into — I’ll explain slowly with examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *