Skip to Content

Blog > Creating an Accessible Accordion with HTML Description Lists

Creating an Accessible Accordion with HTML Description Lists

Karl Groves. - 17/04/2025

When designing web components, accessibility is paramount to ensure inclusiveness for all users, including those with disabilities. A common interactive element that greatly benefits from an accessible implementation is the accordion—a user interface component that allows users to expand and collapse content sections. In this tutorial, we will dive deep into building an accessible accordion using HTML description lists (<dl>).

Why Use Description Lists for Accordions?

HTML description lists (<dl>) are designed to pair terms and their descriptions, making them a natural fit for structuring an accordion.
Each term (<dt>) can serve as a clickable header, with its paired description (<dd>) acting as the collapsible content.
This semantic approach is not only meaningful for browsers and search engines but also improves accessibility when combined with proper ARIA attributes.


Structuring the Accordion with HTML

The backbone of our accordion is a <dl> element containing multiple <dt> and <dd> pairs:

<dl class="accordion">
  <dt>
    <button aria-expanded="false" aria-controls="section1" id="accordion1">
      Section 1
    </button>
  </dt>
  <dd id="section1" role="region" aria-labelledby="accordion1">
    <p>Content for section 1.</p>
  </dd>
  <dt>
    <button aria-expanded="false" aria-controls="section2" id="accordion2">
      Section 2
    </button>
  </dt>
  <dd id="section2" role="region" aria-labelledby="accordion2">
    <p>Content for section 2.</p>
  </dd>
</dl>

The buttons are linked to their respective content sections using aria-controls and aria-labelledby attributes, providing clear relationships for screen readers.


Adding Styles with CSS

Styling ensures that collapsed sections are hidden and expanded sections are clearly visible:

.accordion {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 0;
  max-width: 400px;
}

.accordion button {
  width: 100%;
  padding: 10px;
  text-align: left;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.accordion dd {
  display: none;
  padding: 10px;
  background-color: #fafafa;
}

.accordion dd[aria-hidden="false"] {
  display: block;
}

Implementing Toggle Behavior with JavaScript

The following JavaScript snippet controls the expand/collapse behavior while maintaining accessibility attributes:

const buttons = document.querySelectorAll('.accordion dt button');

buttons.forEach((button) => {
  button.addEventListener('click', () => {
    const expanded = button.getAttribute('aria-expanded') === 'true';
    button.setAttribute('aria-expanded', !expanded);
    
    const content = document.getElementById(button.getAttribute('aria-controls'));
    content.setAttribute('aria-hidden', expanded ? 'true' : 'false');
  });
});

Enhancing Accessibility Further

To fully support accessibility, consider:

  • Keyboard Navigation: Ensure users can navigate using Tab and activate with Enter/Space.
  • Focus Management: Set appropriate focus states when expanding content.
  • Screen Reader Announcements: Use aria-live for dynamic announcements.

Final Result

The full, working example can be seen at https://codepen.io/afixt/pen/vEBgYWy

See the Pen
Accessible Accordion Based on Description Lists (DL)
by AFixt (@afixt)
on CodePen.

Learn More

For more tips and training on accessible web development, check out LearningAccessibility.com

Related Blog Posts

Graphic overview of SPA detailing various touchpoints such as headline, guiding user through the page, focus on essentials, call to action, and responsive design.
Ensuring Accessibility in Single Page Applications: A Comprehensive Guide

Single Page Applications (SPAs) have become the go-to architecture for modern web development. By enabling dynamic content loading without refreshing the page, SPAs offer a smooth and seamless user experience that feels more like a native app. However, while SPAs provide impressive usability benefits, they also introduce unique accessibility challenges that developers need to address […]

Michael Beck - 03/07/2025

Stylized cartoon of a laptop with the acronyms HTML, CCS, and PHP floating around it.
How to Create Accessible Data Tables: Best Practices for Web Developers

Data tables are an essential component of many websites and applications, helping to display large sets of information in a structured, organized manner. Whether it’s a table showing product details, financial data, or a comparison chart, tables help users quickly access and analyze complex information. But for people with disabilities, particularly those relying on assistive […]

Michael Beck - 26/06/2025

Stylized cartoon of a woman with a light bulb overlaid on her head
Designing for Cognitive Disabilities: Best Practices for an Inclusive Web

In the world of web design and development, we often hear about accessibility in terms of physical disabilities—things like vision impairments or mobility challenges. But one area that doesn’t always get as much attention is designing for users with cognitive disabilities. These users, who may have conditions such as dyslexia, ADHD, autism, or cognitive impairments […]

Michael Beck - 19/06/2025

Two men sharing one set of earbuds while working on a laptop.
Top Tools for Testing Accessibility: A Guide for Developers and Webmasters

As web accessibility becomes a critical aspect of digital design, testing for accessibility is more important than ever. Whether you’re an experienced web developer or just getting started in the world of web development, it’s essential to ensure your website is accessible to all users, including those with disabilities. This not only promotes inclusivity but […]

Michael Beck - 12/06/2025

Overhead photo of a user working on a laptop with a notebook, pen, and glass of water nearby
How to Use ARIA Roles and Properties Effectively

Accessible Rich Internet Applications (ARIA) is one of the most powerful tools in a developer’s accessibility toolkit—but it’s also one of the most misunderstood. While ARIA can improve accessibility when used correctly, misuse can actually make things worse for users who rely on assistive technologies (AT), such as screen readers. So, how do you use […]

Michael Beck - 04/06/2025