UI/UX Design 5 min read

Building Inclusive and Usable Interfaces for Your Web Applications: Practical Guide to Accessible UI Components with WCAG 2.1 Standards

PROFSCODE Team 12 Jul 2026
Share:
Accessible UI Component Development: A Developer's Guide to WCAG 2.1, ARIA, and Semantic HTML

Introduction: Why Accessibility is No Longer an Option, But a Necessity

In today's digital world, making web applications and websites accessible to everyone is not just an ethical responsibility, but also a legal requirement and a critical factor for business success. Accessibility ensures that individuals with disabilities (including those with visual, auditory, motor, and cognitive impairments) can easily use your products and services. In this article, specifically for software developers, we will teach you how to create accessible UI components using ARIA (Accessible Rich Internet Applications) attributes and semantic HTML, based on the principles of Web Content Accessibility Guidelines (WCAG) 2.1, with practical examples.

Semantic HTML: The Foundation of Accessibility

The first and most crucial step in creating an accessible UI is to use the correct semantic HTML elements. Semantic HTML informs web browsers and assistive technologies (like screen readers) about the meaning of the content. For example, using the <button> element instead of styling a <div> element as a button naturally improves accessibility because it inherently supports keyboard events, focus management, and screen reader roles.

Example 1: Semantic Button Usage

<!-- Bad Practice: Non-semantic, creates accessibility issues --><div class="button" onclick="doSomething()">Click Me</div><!-- Good Practice: Correct semantic, ready for keyboard and screen readers --><button type="button" onclick="doSomething()">Click Me</button>

ARIA Attributes: Filling Semantic Gaps and Managing Dynamic Content

Sometimes semantic HTML alone may not be sufficient, especially when dealing with dynamic content, custom widgets, or complex UI components. This is where ARIA (Accessible Rich Internet Applications) attributes come into play. ARIA defines additional roles, states, and properties for HTML elements, allowing assistive technologies to better understand these elements. However, remember the rule: "If you can use ARIA, don't use semantic HTML." This means, do not try to solve a situation with ARIA that can be solved with semantic HTML. ARIA should only be used where HTML falls short.

Example 2: Accessible Form Fields and Error Management

Using labels for form fields is mandatory. Additionally, we can use attributes like aria-describedby to associate error messages.

<label for="username">Username:</label><input type="text" id="username" name="username" aria-required="true"><!-- Associating an error message --><label for="email">Email:</label><input type="email" id="email" name="email" aria-invalid="true" aria-describedby="email-error"><div id="email-error" role="alert" style="color: red;">Invalid email format.</div>

Example 3: Implementing an Accessible Modal (Dialog)

Modal windows are special components that open on top of content and often dim the background. For these to be accessible, focus management and ARIA attributes are crucial.

  • When the modal opens, focus should be moved to the modal and kept within it (focus trap).
  • When the modal closes, focus should return to the element that opened the modal.
  • aria-modal="true", role="dialog", aria-labelledby, and aria-describedby should be used.
<!-- Button to open the modal --><button type="button" id="openModalBtn" onclick="openModal()">View Details</button><!-- Modal Structure --><div id="myModal" role="dialog" aria-modal="true" aria-labelledby="modalTitle" aria-describedby="modalDescription" style="display: none;">    <h2 id="modalTitle">Product Details</h2>    <p id="modalDescription">This is a modal window with detailed information about the product.</p>    <button type="button" onclick="closeModal()">Close</button>    <!-- Modal content here --></div><script>    const modal = document.getElementById('myModal');    const openModalBtn = document.getElementById('openModalBtn');    let previouslyFocusedElement;    function openModal() {        previouslyFocusedElement = document.activeElement;        modal.style.display = 'block';        modal.focus(); // Focus on the modal        // More complex JavaScript is required to ensure focus management by listening to keyboard events.        // For example, cyclical focusing within the modal with the Tab key.    }    function closeModal() {        modal.style.display = 'none';        previouslyFocusedElement.focus(); // Return focus    }    // Example for keyboard navigation within the modal (simple)    modal.addEventListener('keydown', function(event) {        if (event.key === 'Escape') {            closeModal();        }        // For more complex focus looping:        // const focusableElements = modal.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');        // if (event.key === 'Tab') { ... }    });</script>

Best Practices and Checklist

  • Keyboard Accessibility: Ensure all interactive elements (links, buttons, form fields) are accessible and usable via keyboard. The focus order should be logical (use tabindex only when necessary and carefully).
  • Color Contrast: Ensure sufficient contrast between text and background colors (WCAG 2.1 AA or AAA levels).
  • Focus Indicators: Ensure there is a clear visual indicator when interactive elements are in focus (:focus pseudo-class).
  • Alternative Texts: Use descriptive alt attributes for all visual content.
  • Meaningful Link Texts: Link texts should clearly state their destination instead of just "click here".
  • Responsive Design: Maintain accessibility across different devices and screen sizes.

Conclusion

Developing accessible UI components not only fulfills legal requirements but also reaches a wider audience and improves the overall quality of your product. Correctly using semantic HTML, intelligently integrating ARIA attributes, and following fundamental best practices like keyboard accessibility and contrast are key to creating inclusive and usable web experiences. By making accessibility a part of your development process, we can build a better web for everyone.

Back to Blog

Comments (0)

No comments yet. Be the first to comment!

Submit Comment