Accessibility Tips for Web Developers
Web accessibility ensures that people with disabilities can perceive, understand, navigate, and interact with websites. It's not just a nice-to-have feature—it's a fundamental aspect of web development that benefits all users. This article provides practical tips for improving the accessibility of your websites.
Semantic HTML
Using semantic HTML is the foundation of accessible websites:
Use Appropriate Elements
Choose HTML elements that accurately represent the content's meaning:
<!-- Instead of this -->
<div class="button" onclick="submitForm()">Submit</div>
<!-- Use this -->
<button type="submit">Submit</button>
Document Structure
Use heading elements (<h1> through <h6>) to create a logical document outline. Don't skip heading levels, and use only one <h1> per page.
Landmarks
Use semantic landmark elements to help screen reader users navigate your page:
<header>
<nav>...</nav>
</header>
<main>
<article>...</article>
<aside>...</aside>
</main>
<footer>...</footer>
Keyboard Accessibility
Many users navigate websites using only a keyboard:
Focus Management
Ensure all interactive elements can receive keyboard focus and that focus order is logical:
/* Don't remove focus outlines without providing an alternative */
:focus {
outline: 2px solid #7c3aed;
outline-offset: 2px;
}
Skip Links
Provide a way for keyboard users to skip navigation and go directly to main content:
<a href="#main-content" class="skip-link">Skip to main content</a>
/* Style the skip link to be visible only when focused */
.skip-link {
position: absolute;
top: -40px;
left: 0;
padding: 8px;
z-index: 100;
}
.skip-link:focus {
top: 0;
}
Custom Widgets
When building custom interactive components, implement keyboard support according to WAI-ARIA Authoring Practices.
Text and Typography
Readable text is essential for all users, especially those with visual or cognitive impairments:
Text Size and Scaling
Use relative units (em, rem) for font sizes to allow text to scale when users change their browser settings:
body {
font-size: 1rem; /* Base font size */
}
h1 {
font-size: 2rem; /* Scales relative to body */
}
Line Height and Spacing
Provide adequate line height and paragraph spacing for better readability:
body {
line-height: 1.5;
}
p {
margin-bottom: 1.5em;
}
Color Contrast
Ensure sufficient contrast between text and its background. WCAG 2.1 Level AA requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text.
Images and Media
Make visual content accessible to screen reader users and those with visual impairments:
Alternative Text
Provide descriptive alt text for images that convey information:
<!-- Informative image -->
<img src="chart.jpg" alt="Bar chart showing sales increasing by 25% in Q2 2023">
<!-- Decorative image -->
<img src="decorative-divider.jpg" alt="">
Video Captions
Include captions for videos to make them accessible to deaf or hard-of-hearing users:
<video controls>
<source src="video.mp4" type="video/mp4">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
</video>
Audio Descriptions
Provide audio descriptions for videos when important visual information isn't conveyed through dialogue.
Forms
Forms are often challenging for users with disabilities:
Labels
Associate labels with form controls using the for attribute:
<label for="name">Name</label>
<input id="name" type="text">
Error Messages
Provide clear error messages and associate them with the relevant form fields:
<label for="email">Email</label>
<input id="email" type="email" aria-describedby="email-error">
<div id="email-error" class="error" role="alert">Please enter a valid email address</div>
Fieldsets and Legends
Group related form controls using fieldset and legend elements:
<fieldset>
<legend>Shipping Address</legend>
<!-- Address form fields -->
</fieldset>
ARIA (Accessible Rich Internet Applications)
ARIA attributes can enhance accessibility when used correctly:
ARIA Landmarks
Use ARIA landmarks to identify regions of a page when semantic HTML elements aren't sufficient:
<div role="navigation">...</div>
<div role="main">...</div>
ARIA States and Properties
Communicate the state of interactive elements:
<button aria-expanded="false" aria-controls="dropdown-menu">Menu</button>
<div id="dropdown-menu" hidden>...</div>
Live Regions
Announce dynamic content changes to screen reader users:
<div aria-live="polite" aria-atomic="true">
<!-- Content that will update dynamically -->
</div>
Testing
Regular testing is crucial for maintaining accessibility:
Automated Testing
Use tools like Axe, WAVE, or Lighthouse to catch common accessibility issues.
Keyboard Testing
Navigate your entire website using only the keyboard to identify accessibility barriers.
Screen Reader Testing
Test with screen readers like NVDA, JAWS, or VoiceOver to ensure your content is properly announced.
User Testing
Whenever possible, include people with disabilities in your user testing process.
Conclusion
Web accessibility is an ongoing process, not a one-time task. By incorporating these practices into your development workflow, you'll create websites that are more usable for everyone, including people with disabilities.
Remember that many accessibility improvements benefit all users, not just those with disabilities. Features like keyboard navigation, clear error messages, and good contrast make your website more usable for everyone.
Taylor Kim
Accessibility specialist and frontend developer advocating for inclusive web experiences.