aria-controls Guide: What Is It? How to Use It?

Jul 27 2024 by Oskay Günaçar

What Is aria-controls?

The aria-controls attribute identifies the element (or elements) that are controlled by the current element. This is particularly useful for assistive technologies, such as screen readers, which can then inform users about the control relationship between elements. By providing this information, aria-controls helps users understand how interacting with a control (like a button or a link) will affect other parts of the web page.

Example Usage

Here's a simple example to illustrate the use of aria-controls:

<button aria-controls="content1">Show Content</button>
<div id="content1" hidden>
    <p>This is the content controlled by the button above.</p>
</div>

In this example:

  • The button element has the aria-controls attribute set to "content1", which is the id of the div element it controls.
  • This relationship indicates that interacting with the button will affect the visibility or state of the div with id="content1".

Why Is aria-controls Important?

The aria-controls attribute is a crucial component in web accessibility, enhancing the usability and navigability of web applications for users with disabilities. Here’s why aria-controls is important:

Enhances Accessibility

Aria-controls provides context to assistive technologies, such as screen readers, by indicating the relationship between control elements and the elements they affect. This additional context is essential for users who rely on auditory feedback to understand and navigate web content. By indicating which elements are controlled by interactive components, aria-controls enables users to better understand and predict the outcomes of their interactions.

Supports Better User Experience

On complex web pages, users can easily get lost in the multitude of interactive elements. Aria-controls clarifies which elements are interconnected, aiding in smoother navigation and interaction. For users with cognitive impairments or those unfamiliar with the interface, knowing the control-target relationships reduces confusion and enhances usability.

Facilitates Assistive Technology Functionality

Aria-controls provides vital information to screen readers, allowing them to inform users about the controls available and their associated elements. This helps users efficiently navigate through and interact with the content. Beyond screen readers, other assistive technologies can also leverage aria-controls to enhance the overall accessibility of web applications.

By incorporating aria-controls in your web development projects, you contribute to a more inclusive and accessible web experience for all users.

When to Use ARIA-Controls

The aria-controls attribute is particularly useful in situations where a control element, such as a button or link, is intended to manipulate the content or visibility of another element. Here are some scenarios and examples where aria-controls should be used:

Interactive Widgets

For elements that control the display or state of other elements, aria-controls is essential. This includes interactive components like tabs, accordions, modals, and dropdowns. When a user activates a tab, for example, the aria-controls attribute can indicate which content panel is associated with that tab, providing clear context to assistive technologies.

Complex Components

Custom components often have intricate relationships between controls and the elements they manipulate. Using aria-controls in these scenarios ensures that users, especially those relying on assistive technologies, can understand and interact with these components effectively. For example, a custom carousel control that moves between different slides can benefit from aria-controls by indicating which slide is currently controlled.

Dynamic Content Updates

In cases where user interactions dynamically update content on the page, aria-controls helps maintain clarity. For example, a "Show More" button that expands a hidden section of content can use aria-controls to identify the element being revealed. This way, screen readers can inform users about the new content made available through their action.

Form Enhancements

Interactive forms that conditionally display additional fields based on user input can use aria-controls to link controls with the fields they affect. For instance, selecting an option in a dropdown might display additional input fields relevant to that choice. Aria-controls can ensure that users are aware of these dynamically displayed fields.

Navigation Elements

For navigation elements that control the visibility of menus or submenus, aria-controls can be used to indicate the controlled menu items. This is particularly useful in navigation bars or side menus where a menu button toggles the display of nested menu items.

How to Implement ARIA-Controls

Implementing the aria-controls attribute involves a few straightforward steps to ensure that your web elements are accessible and provide the necessary context for assistive technologies. Here's a step-by-step guide on how to add and use aria-controls in your HTML:

1. Identify the Control Element

First, determine which element will act as the control. This is typically an interactive element like a button, link, or another UI component that triggers a change in another element.

Example:

<button id="toggleButton">Show More</button>

2. Identify the Controlled Element

Next, identify the element that will be controlled. This is the element that will change in response to the user's interaction with the control element.

Example:

<div id="additionalContent" hidden>
  <p>This is the additional content that will be shown or hidden.</p>
</div>

3. Add the aria-controls Attribute

Add the aria-controls attribute to the control element, with its value set to the id of the element it controls. This establishes a relationship between the control element and the controlled element.

Example:

<button id="toggleButton" aria-controls="additionalContent">Show More</button>

4. Implement the JavaScript Logic

Implement the JavaScript logic to handle the interaction. This typically involves adding event listeners to the control element and toggling the visibility or state of the controlled element.

Example:

<script>
  document.getElementById('toggleButton').addEventListener('click', function() {
    var content = document.getElementById('additionalContent');
    var isHidden = content.hidden;
    content.hidden = !isHidden;
    this.textContent = isHidden ? 'Show Less' : 'Show More';
  });
</script>

5. Ensure Accessibility

Ensure that your implementation is accessible by testing it with various assistive technologies. Verify that screen readers correctly announce the relationship between the control and the controlled elements and that users can interact with the elements as expected.

Example of Complete Implementation

Here's a complete example combining all the steps:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ARIA Controls Example</title>
</head>
<body>
  <button id="toggleButton" aria-controls="additionalContent">Show More</button>
  <div id="additionalContent" hidden>
    <p>This is the additional content that will be shown or hidden.</p>
  </div>

  <script>
    document.getElementById('toggleButton').addEventListener('click', function() {
      var content = document.getElementById('additionalContent');
      var isHidden = content.hidden;
      content.hidden = !isHidden;
      this.textContent = isHidden ? 'Show Less' : 'Show More';
    });
  </script>
</body>
</html>

By following these steps, you can effectively implement the aria-controls attribute in your web applications, enhancing accessibility and providing a better user experience for all users.

Best Practices for ARIA-Controls

To ensure the effective and accessible use of the aria-controls attribute, it's important to follow certain best practices. These practices help maximize the benefits of aria-controls for users who rely on assistive technologies, ensuring a seamless and inclusive web experience.

Use Clear and Unique IDs

The aria-controls attribute links a control element to the element it controls using IDs. Ensure that these IDs are unique and descriptive. This avoids confusion and helps assistive technologies accurately convey relationships.

Example:

<button id="expandDetails" aria-controls="detailsSection">Show Details</button>
<div id="detailsSection" hidden>
  <p>Here are the detailed information...</p>
</div>

Ensure Visibility States Are Managed Correctly

When using aria-controls, it's crucial to manage the visibility or state of the controlled element correctly. Use attributes like hidden or ARIA states like aria-expanded to reflect changes accurately.

Example:

<button id="toggleContent" aria-controls="content" aria-expanded="false">Show Content</button>
<div id="content" hidden>
  <p>This is the content that can be toggled.</p>
</div>

<script>
  document.getElementById('toggleContent').addEventListener('click', function() {
    var content = document.getElementById('content');
    var expanded = this.getAttribute('aria-expanded') === 'true';
    content.hidden = expanded;
    this.setAttribute('aria-expanded', !expanded);
    this.textContent = expanded ? 'Show Content' : 'Hide Content';
  });
</script>

Combine with Other ARIA Attributes

Combine aria-controls with other ARIA attributes like aria-expanded, aria-hidden, or aria-labelledby to provide more context and enhance accessibility. This combination helps users understand the state and relationships between elements.

Example:

<button id="menuButton" aria-controls="menu" aria-expanded="false" aria-haspopup="true">Open Menu</button>
<ul id="menu" hidden>
  <li>Option 1</li>
  <li>Option 2</li>
</ul>

<script>
  document.getElementById('menuButton').addEventListener('click', function() {
    var menu = document.getElementById('menu');
    var expanded = this.getAttribute('aria-expanded') === 'true';
    menu.hidden = expanded;
    this.setAttribute('aria-expanded', !expanded);
  });
</script>

Provide Keyboard Accessibility

Ensure that elements with aria-controls are accessible via keyboard interactions. Users should be able to navigate and interact with these elements using keyboard shortcuts, improving overall accessibility.

Example:

<button id="toggleInfo" aria-controls="info" aria-expanded="false">Show Info</button>
<div id="info" hidden>
  <p>This is additional information.</p>
</div>

<script>
  document.getElementById('toggleInfo').addEventListener('click', function(event) {
    if (event.key === 'Enter' || event.type === 'click') {
      var info = document.getElementById('info');
      var expanded = this.getAttribute('aria-expanded') === 'true';
      info.hidden = expanded;
      this.setAttribute('aria-expanded', !expanded);
      this.textContent = expanded ? 'Show Info' : 'Hide Info';
    }
  });
</script>

Test with Assistive Technologies

Regularly test your implementation with various assistive technologies, such as screen readers, to ensure the aria-controls attribute is working as intended. This helps identify and fix any accessibility issues early in the development process.

Provide Clear Visual Indicators

While aria-controls enhances accessibility for users relying on assistive technologies, it's also helpful to provide clear visual indicators for all users. This can include icons or text changes that reflect the current state of the controlled element.

Example:

<button id="toggleDescription" aria-controls="description" aria-expanded="false">
  <span class="icon">+</span> Show Description
</button>
<div id="description" hidden>
  <p>This is the detailed description.</p>
</div>

<script>
  document.getElementById('toggleDescription').addEventListener('click', function() {
    var description = document.getElementById('description');
    var expanded = this.getAttribute('aria-expanded') === 'true';
    description.hidden = expanded;
    this.setAttribute('aria-expanded', !expanded);
    this.querySelector('.icon').textContent = expanded ? '+' : '-';
    this.textContent = expanded ? 'Show Description' : 'Hide Description';
  });
</script>

By following these best practices, you can ensure that your use of aria-controls is effective, enhancing the accessibility and usability of your web applications for all users.

Testing and Debugging ARIA-Controls

Ensuring that your implementation of aria-controls is both functional and accessible requires thorough testing and debugging. This helps identify any issues and ensures that users, especially those relying on assistive technologies, have a seamless experience. Here are some strategies and tools for testing and debugging aria-controls.

Use Browser Developer Tools

Most modern browsers come with built-in developer tools that can be used to inspect and debug ARIA attributes.

  1. Inspect Element: Right-click on the control element and select "Inspect" or "Inspect Element" to open the developer tools.
  2. Check ARIA Attributes: Verify that the aria-controls attribute is correctly set on the control element and that the id it references is unique and exists in the DOM.
  3. Monitor Changes: Use the console to monitor changes in the DOM and ensure that any JavaScript interactions correctly update the aria-controls relationships and related attributes (like aria-expanded).

Screen Reader Testing

Testing with screen readers is crucial to ensure that the aria-controls attribute works as intended.

  1. Popular Screen Readers: Test with various screen readers such as NVDA, JAWS, VoiceOver (macOS/iOS), and TalkBack (Android).
  2. Navigation: Navigate through the control and controlled elements to ensure the screen reader correctly announces the relationships and any state changes.
  3. Interaction: Interact with the control elements and verify that the screen reader provides appropriate feedback about the changes.

Automated Accessibility Testing Tools

Several automated tools can help identify issues with ARIA attributes and overall accessibility.

  1. Axe: A popular accessibility testing tool that can be integrated into your browser or testing pipeline to identify ARIA-related issues.
  2. WAVE: The Web Accessibility Evaluation Tool provides visual feedback about the accessibility of your web content, including ARIA attribute checks.
  3. Lighthouse: A built-in tool in Chrome’s developer tools that audits accessibility among other aspects of web performance.

Manual Testing

Manual testing involves interacting with your web application to ensure that aria-controls behaves as expected.

  1. Keyboard Navigation: Ensure that all control elements can be navigated using the keyboard and that pressing Enter or Space activates them correctly.
  2. Focus Management: Verify that focus is managed appropriately, especially after interacting with control elements, to maintain a logical navigation order.
  3. Visibility Changes: Manually toggle the control elements and check that the controlled elements appear or disappear as expected and that the aria-controls relationships are clear.

Debugging Common Issues

Identify and fix common issues that may arise when using aria-controls.

  1. Missing or Incorrect IDs: Ensure that the id referenced in aria-controls exists and is unique.
  2. State Synchronization: Verify that the state of the control elements (like aria-expanded) is correctly synchronized with the visibility of the controlled elements.
  3. JavaScript Errors: Check the browser console for any JavaScript errors that might affect the functionality of aria-controls.

Example:

<button id="toggleContent" aria-controls="content" aria-expanded="false">Show Content</button>
<div id="content" hidden>
  <p>This is the content that can be toggled.</p>
</div>

<script>
  document.getElementById('toggleContent').addEventListener('click', function() {
    var content = document.getElementById('content');
    var expanded = this.getAttribute('aria-expanded') === 'true';
    content.hidden = expanded;
    this.setAttribute('aria-expanded', !expanded);
    this.textContent = expanded ? 'Show Content' : 'Hide Content';
  });
</script>

User Testing

Gather feedback from users, especially those who rely on assistive technologies, to ensure the aria-controls implementation meets their needs.

  1. Beta Testing: Conduct beta testing sessions with a diverse group of users to identify any accessibility issues.
  2. Surveys and Feedback: Collect feedback through surveys or direct user feedback to understand how well the aria-controls attribute enhances accessibility.

By following these testing and debugging practices, you can ensure that your implementation of aria-controls is robust, functional, and accessible, providing a better user experience for everyone.

Conclusion

Incorporating the aria-controls attribute in your web development projects is a vital step toward creating an accessible and user-friendly web experience. By clearly defining the relationships between control elements and the elements they manipulate, you provide essential context for assistive technologies, helping users navigate and interact with your content more effectively.

Through thoughtful implementation, adherence to best practices, and rigorous testing and debugging, you can ensure that your use of aria-controls is both functional and accessible. This not only enhances the usability of your web applications but also demonstrates a commitment to inclusivity and accessibility for all users.

By prioritizing accessibility in your development process, you contribute to a more inclusive web, where everyone, regardless of their abilities, can access and enjoy your content. Start integrating aria-controls today and make a positive impact on the user experience for all your visitors.

Oskay Günaçar
Oskay Günaçar is a Technical SEO expert and backend developer. His main areas of interest are back-end programming, Python, Django, Go, semantic SEO, technical SEO. With more than 5 years of SEO experience, he has held SEO specialist positions at İkiler, Webtures, Boosmart, and Storyly.io companies, and has been involved in numerous projects as an outsourced expert. Through the TechSEOHub website, he aims to share the expertise and experience he has gained from managing and developing (SEO) processes for many successful and large websites in the industry, and to produce content for easy access to accurate and high-quality information.