Accessible Coding

Accessible Coding Best Practices

Writing code isn’t just about making a site look good or function well. It’s also about making sure everyone can use it, regardless of their device, ability, or circumstances. In this chapter, you’ll learn practical coding techniques that improve both accessibility and the overall user experience. Additionally, you’ll also learn best practices to make your code more accessible, usable, and future-proof.

Globe collage

Why Use Semantic HTML and Native Controls?

Semantic HTML is the foundation for accessible coding. Using elements like <header>, <nav>, <main>, and <footer> gives more structure and meaning to your code, making it easier for assistive technologies to read and interpret the page. When you use semantic elements correctly, users don’t just see your content; they understand how it’s organized.

Similarly, native controls like <button>, <select>, and <input> are equally important. They come with built-in accessibility features, including keyboard support, focus management, and screen reader compatibility. Replacing them with custom elements (for example, using a styled <div> instead of <button>) often breaks these features, leaving people unable to interact with your site. 

Example: Custom vs. Native Button

Let's examine an example to better illustrate why semantic HTML and native controls are crucial for accessibility.

Non-semantic custom button (not accessible):

<div onclick="submitForm()" role="button" tabindex="0"> Submit </div>

This type of button requires ARIA roles and tabindex and doesn’t behave like a button by default. Additionally, it’s easy to miss keyboard interactions or screen reader support.

Native button (accessible by default)

<button type="submit">Submit</button>

This button automatically supports keyboard navigation and screen readers as it comes with built-in semantics behaviors. More simply, it tell users exactly what will happen when they select the button. Additionally, this button requires less code and fewer workarounds.

The bottom line: By leaning on semantic HTML and native controls, you reduce the need for extra code, minimize errors, and ensure your site works across devices and assistive technologies. It’s the simplest way to make your content more accessible while keeping your code and future-friendly.

Quiz Yourself

How does semantic HTML and native controls make code more accessible?

@

Not Quite!

Hint: Think about how assistive technologies like screen readers interact with code.

What is Progressive Enhancement?

Progressive enhancement is a web development strategy that starts with a solid foundation of clean, semantic HTML and then layers on CSS for design and JavaScript interactivity. This approach ensures that your site’s core content and functionality are available on all devices and browsers, even when scripts or advanced features aren’t available.

Think about it this way: You start with a solid, simple foundation that works everywhere, then add design and interactive features on top for users with modern browsers and devices.

For example, a form built with semantic HTML will submit data without JavaScript. Adding JavaScript can enhance the user experience with features like real-time validation, but the basic functionality would remain intact. 

Remember, people with disabilities are disproportionately underemployed or have income limits for maintaining government assistance, which means they may not have the latest technology. By using progressive enhancement, you create experiences that are accessible, inclusive, and reliable, while also future-proofing your code.

Quiz Yourself

Which of the following best describes progressive enhancement?

@

Not Quite!

Hint: Think about building websites in layers.

What is Responsive Design?

Responsive design is a technique that aims to provide an optimal viewing experience across a wide range of devices by adjusting content to fit different screen sizes and orientations. This is especially important because most people use a mobile phone or tablet as their primary internet device.

Key strategies include using collapsible menus to maintain usability on small screens, ensuring interactive elements operate with touch gestures, and organizing content in a free-flowing, single-column layout that is easy to read and interact with. Serving appropriately sized images also improves load times and overall performance, enhancing the user experience for users.

By incorporating practices like semantic HTML, native controls, progressive enhancement, and responsive design, you create digital spaces that are not only accessible and inclusive, but also user-friendly across a wide variety of devices and contexts. Responsive design is a core part of building websites that are usable, adaptable, and future-proof.

Quiz Yourself

What is an example of responsive design?

@

Not Quite!

Hint: Remember, the purpose of responsive design is to provide an optimal viewing experience across various devices.

How Do You Test Accessible Code?

To ensure your code is truly accessible, you need to test it. It’s the only way to catch issues early, improve usability, and create accessible digital experiences. Below are a few ways to test the accessibility of your code.

Automated Testing Tools

Automated tools can quickly scan your content for common accessibility issues, including missing alt text, poor color contrast, broken ARIA attributes, and other coding issues. While these tools are fast and can scan an entire site, they can’t catch everything. More complex issues, such as contextual issues, require human review.

Expert Testing Methods

Expert testing (or manual testing) is essential to fully understand how accessible your code is. Some key testing techniques include:

Keyboard testing

where you navigate through your content using only keyboard commands or shortcuts.

Screen reader testing

using tools like NVDA, JAWS, and VoiceOver to see how content is presented.

Visual inspection

where you check color contrast, heading structure, and focus indicators.

User testing

that involves users with disabilities to test and interact with your content.

However, expert testing can be time-consuming and resource-intensive, which is why we recommend taking a hybrid approach to testing your code. Using automated testing to find common issues and expert testing to find more complex ones. This enables you to push code that is as accessible as possible to your users.

Best Practices

Test your code early and often throughout the entire development process. This enables you to make your site more reliable, inclusive, and user-friendly, while reducing the risk of users experiencing accessibility barriers in your content. Be sure to use both automated and expert testing as this will help you find and fix more accessibility issues and create a more accessible experience.

Quiz Yourself

What’s the difference between automated and expert testing?

@

Not Quite!

Hint: Think about the specific issues that automated and expert testing look for.

Quiz Yourself

How often should you test your code for accessibility?

@

Not Quite!

Hint: Think about how often your code changes.

What are Common Accessibility Pitfalls to Avoid?

It’s easy for accessibility issues to sneak into your code. Most come from relying too heavily on visual design or custom solutions without thinking about how assistive technologies interpret them. Being aware of the common pitfalls in code can help you avoid mistakes that make your site harder to use.

Here are some of the most common coding mistakes:

  • Using non-semantic elements for interactivity: Styling <div>s or <span>s as buttons or links instead of using <button> or <a>.

  • Removing focus indicators: Hiding the outline (or focus indicator) makes keyboard navigation impossible to follow.

  • Relying on color alone: Color alone can’t convey meaning for users with color vision deficiencies such as color blindness or low vision.

  • Overusing or misusing ARIA roles: Adding unnecessary roles or attributes can confuse assistive technologies. 

  • Ignoring alt text: Missing or meaningless alt attributes leave images inaccessible to assistive technology users.

  • Skipping manual testing: Automated tools miss context; be sure to test with a keyboard, screen reader, real users, and accessibility experts.

Avoiding these pitfalls helps you reduce common accessibility barriers and make your site easier to use.

Quiz Yourself

Which of the following is a common pitfall in accessible coding practices?

@

Not Quite!

Hint: Think about mistakes that would make digital content difficult to use for individuals with disabilities.

Ready for what’s next?

Let’s dive into web page setup.

Frequently Asked Questions