chsrijan.com/writings/..
Back to writings

Enhancing Web Accessibility: A Practical Guide

Web accessibility ensures that websites are usable by everyone, including individuals with disabilities. Adopting accessibility practices is not just ethically sound but also expands your audience reach and improves user experience for all. This article explores practical techniques with code examples to help you integrate accessibility into your projects.


1. Semantic HTML: The Foundation of Accessibility

Semantic HTML elements describe their meaning in a way that is understandable to both the browser and assistive technologies. For instance, using <header>, <main>, and <footer> improves navigation for screen readers.

Example: Replacing Non-Semantic Divs

<!-- Avoid this: Non-semantic structure -->
<div id="header">
  <div class="nav">Navigation</div>
</div>

<!-- Use this: Semantic structure -->
<header>
  <nav>Navigation</nav>
</header>

Why it matters: Semantic elements provide better context for users relying on assistive technologies like screen readers.


2. Accessible Forms

Forms are a critical part of most websites. Ensuring their accessibility involves proper labeling and feedback mechanisms.

Example: Accessible Login Form

<form>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required />

  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required />

  <button type="submit">Login</button>
</form>

Tips:

  • Use <label> elements for inputs to associate descriptions explicitly.
  • Include aria-live regions for dynamic error messages.

Example: Adding Real-Time Error Feedback

<div id="error-message" aria-live="polite"></div>

<script>
  const emailInput = document.getElementById('email');
  const errorMessage = document.getElementById('error-message');

  emailInput.addEventListener('input', () => {
    if (!emailInput.validity.valid) {
      errorMessage.textContent = 'Please enter a valid email address.';
    } else {
      errorMessage.textContent = '';
    }
  });
</script>

3. Keyboard Navigation

Ensure that all interactive elements are reachable and usable via keyboard alone. Use logical tabindex values and focus management.

Example: Custom Focus Styling

button:focus {
  outline: 2px solid #005fcc;
  outline-offset: 2px;
}

Example: Managing Focus Programmatically

const modal = document.getElementById('modal');
const openButton = document.getElementById('open-modal');
const closeButton = document.getElementById('close-modal');

openButton.addEventListener('click', () => {
  modal.style.display = 'block';
  closeButton.focus();
});

closeButton.addEventListener('click', () => {
  modal.style.display = 'none';
  openButton.focus();
});

4. ARIA for Enhanced Accessibility

Accessible Rich Internet Applications (ARIA) attributes provide additional information to assistive technologies when semantic HTML isn't sufficient.

Example: ARIA Roles and Attributes

<div role="alert" aria-live="assertive">
  Your session is about to expire!
</div>

Note: Use ARIA sparingly and as a supplement to native HTML elements.


5. Testing for Accessibility

Always test your website's accessibility using tools and manual checks:

  • Automated Tools: Lighthouse (built into Chrome DevTools), Axe.
  • Screen Readers: NVDA, VoiceOver.
  • Keyboard Navigation: Tab through your site to ensure usability.

By adopting these practices, you can build websites that are more inclusive and provide a superior experience for all users. Accessibility is not a feature—it’s a necessity.