← Back to HTML Course | Chapter 7: Advanced APIs & Features | Lesson 8 of 11

HTML Accessibility

Imagine building a gorgeous public park with beautiful slides and picnic tables. But when you look at the entrance, there are only steep, rocky stairs—no ramps, paths, or signs. Visitors using wheelchairs or strollers would be completely blocked from enjoying your park. Web accessibility (often called A11y) is building those essential ramps and signs for your website. It is the art of structuring your HTML code so that everyone, including visually impaired users navigating with screen readers or people unable to use a mouse, can browse your site easily. Writing accessible code is not extra work; it is simply writing clean, standard HTML that works for everyone.

Web Accessibility Basics

Web accessibility means designing pages that can be navigated by anyone, regardless of physical or cognitive abilities. Writing semantic, standard HTML code is the foundation of an accessible webpage.

Note: Use native HTML tags (like buttons and links) to inherit built-in keyboard navigation support automatically.

Warning: Styling non-clickable tags (like div or span) to look like buttons makes them completely unusable for keyboard-only users.

Example: Web Accessibility Basics

markup
<button onclick="alert('Clicked')">Click me</button>

Semantic HTML Landmarks

Semantic tags (like main, nav, header, and footer) serve as layout landmarks on your webpage. Screen readers use these landmarks to announce structural outlines, allowing users to skip directly to content blocks easily.

Note: Always wrap your main page topic inside a single, clean main tag to help users navigate.

Warning: Overusing plain div tags instead of semantic landmarks can make navigating your page confusing for screen-reader users.

Example: Semantic HTML Landmarks

markup
<header>Site Header</header>
<nav>Navigation</nav>
<main>Main content</main>
<footer>Footer</footer>

ARIA Attributes and Roles

ARIA (Accessible Rich Internet Applications) attributes are specialized accessibility tags used to add context to your markup when standard HTML semantics are not enough.

Note: Always use aria-label to describe icon-only buttons that do not contain visible text.

Warning: The first rule of ARIA is: do not use ARIA if a native, semantic HTML tag can handle the job instead.

Example: ARIA Attributes and Roles

markup
<button aria-label="Close dialog">X</button>

Keyboard Navigation and Focus Styles

Keyboard-only and visually impaired users rely on the Tab key to navigate webpages chronologically. You must ensure that every interactive element has a visible focus indicator (like a border outline) so users can see where they are on the page.

Note: Use the tabindex attribute to include custom, styled elements in the webpage tab sequence.

Warning: Never use CSS outline: none to hide browser focus outlines without replacing them with custom focus styles.

Example: Keyboard Navigation and Focus Styles

markup
<a href="#" style="outline: 2px solid blue;">Focusable link</a>

Color Contrast and Alt Descriptions

Accessibility relies heavily on high color contrast and clear graphic descriptions. Ensure your text has sufficient color contrast against background fills, and always write descriptive alt text for your images.

Note: Aim to maintain a contrast ratio of at least 4.5:1 for regular paragraph text.

Warning: Omitting alt attributes is a major accessibility violation that makes your images invisible to screen readers.

Example: Color Contrast and Alt Descriptions

markup
<p style="color: #000000; background-color: #FFFFFF;">High contrast text</p>
<img src="photo.jpg" alt="A red bicycle leaning against a wall">

Skip Links Implementation

A skip link lets keyboard users jump straight from the top of the page to the main content, bypassing a long navigation menu. It is usually hidden visually until it receives keyboard focus, at which point it becomes clearly visible.

Note: Place the skip link as the very first focusable element in the body so it is always the first thing a keyboard user encounters.

Warning: A skip link hidden with display:none cannot receive keyboard focus at all — use an off-screen positioning technique instead.

Example: Skip Links Implementation

markup
<a href="#main-content" style="position: absolute; left: -9999px;">Skip to main content</a>
<main id="main-content">Main content</main>

Accessible Form Labels

Every form input needs an associated label so screen readers can announce what the field is for. Wrapping the input inside the label, or connecting them using a matching for and id attribute, both create this essential relationship.

Note: Clicking or tapping directly on a properly connected label should also focus its input — a great way to test if your labels are correctly linked.

Warning: Using only a placeholder as a substitute for a real label is a common mistake — placeholders disappear once typing starts and are not reliably announced by all screen readers.

Example: Accessible Form Labels

markup
<label for="email">Email</label>
<input type="email" id="email" name="email">

Accessibility Testing Tools (Lighthouse, axe)

Lighthouse, built directly into Chrome DevTools, and axe, available as a browser extension, both automatically scan a page and flag common accessibility problems like missing alt text or poor color contrast. Automated tools catch many issues quickly, but they cannot replace actually testing with a keyboard and a real screen reader.

Note: Run Lighthouse's accessibility audit early and often during development, not just once right before launch.

Warning: Passing every automated accessibility check does not guarantee a page is actually usable — automated tools typically catch only around a third of real accessibility issues.

Example: Accessibility Testing Tools (Lighthouse, axe)

markup
<!-- Run Chrome DevTools > Lighthouse > Accessibility audit on this page -->
<img src="banner.jpg" alt="Summer sale banner">
Common Mistakes
  1. Using non-clickable div or span tags to design custom button layouts, breaking keyboard navigation.
  2. Hiding browser focus outlines using CSS (outline: none) without replacing them with custom focus styles.
  3. Forgetting to include alt attributes on your image elements, leaving them invisible to screen readers.
Chapter Summary
  • Web accessibility ensures your website can be navigated by anyone, regardless of physical or cognitive abilities.
  • Use semantic HTML tags (like buttons and headers) and ARIA attributes to keep your content readable and accessible.
  • Ensure all interactive elements can be focused using the keyboard, and maintain high color contrast for clean readability.
Browser Support

Standard accessibility attributes and semantic elements are supported natively by all modern web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.