Accordion
collapsible UI for multiple sectionsAn accordion component that allows users to expand and collapse content sections. This component works entirely without JavaScript, using CSS and HTML form elements to provide smooth animations and interactions.
Overview
The accordion component is perfect for organizing content into collapsible sections, making it ideal for FAQs, documentation sections, or any interface where you want to save vertical space while keeping content accessible. Each section can be independently expanded or collapsed by clicking on its header.
Key Features
- No JavaScript Required: Uses pure CSS and HTML form elements for functionality
- Smooth Animations: Built-in CSS transitions for expand/collapse actions
- Accessible: Proper labeling and keyboard navigation support
- Flexible Content: Each section can contain any HTML content
- Optional Animation: Choose between animated and non-animated sections
Prerequisites
You’ll need to import views/components at the top of your template to use the ExpandCollapse helper component.
Basic Usage
1 | |
Structure Breakdown
Container
<ul class="accordion">: The main container that holds all accordion sections
Individual Sections
Each accordion section consists of three parts:
- Hidden Checkbox:
<input type="checkbox" hidden />- Controls the expand/collapse state - Clickable Label:
<label>- The header that users click to toggle the section - Content Area:
<div class="bd">or<div class="bd-no-animation">- Contains the collapsible content
Important Notes
- Unique IDs: Each checkbox must have a unique
idattribute - Matching Labels: The
forattribute on each label must match its corresponding checkboxid - Triple Div Structure: For animated sections, use the nested
<div><div><div>structure for proper animation - ExpandCollapse Helper: The
{%= components.ExpandCollapse(3, ps) %}adds the expand/collapse icon
Animation Options
Animated Sections (Default)
Use class="bd" with the triple-div structure for smooth expand/collapse animations:
1 | |
Non-Animated Sections
Use class="bd-no-animation" for instant show/hide without transitions:
1 | |