FAQ Accordion List Template

Answer your users' frequently asked questions with this interactive and accessible FAQ accordion.

This template allows users to click on a question to reveal the answer, keeping your help section tidy and easy to navigate.

Screenshot of a list of questions that expand to show answers.

Get Source Code Preview

About this Template

This template is built upon a semantic definition list (dl), with each question as a dt (definition term) and each answer as a dd (definition description). Lightweight, dependency-free JavaScript powers the expand and collapse functionality, ensuring fast performance. ARIA attributes are used to communicate the state of each accordion item to screen readers, making it fully accessible.

Features

Code Breakdown

The FAQ is structured as a dl element. Each question is a button inside the dt for maximum accessibility, and the corresponding answer is inside the dd. By default, the answers are hidden with display: none.

The JavaScript finds all the question buttons. It adds a click event listener to each one. When a button is clicked, the script toggles an .active class on the question and toggles the visibility of the next element, which is the answer. It also updates the aria-expanded attribute on the button from false to true, which is essential for assistive technologies.

Code

Here's the full code for the template:

View Output