Tutorials HTML Accessibility (a11y)
Ch 15 / 40
Chapter 15 Beginner 10 min read HTML

HTML Accessibility (a11y)

Over 1.3 billion people worldwide live with some form of disability. Web accessibility (abbreviated a11y — 11 letters between "a" and "y") ensures your HTML works for everyone — including people who use screen readers, keyboard-only navigation, voice control, or high-contrast displays. In this chapter you'll master the techniques, attributes, and ARIA roles that make HTML truly inclusive — and why it also improves SEO.

What you'll learn
What a11y is and the 4 WCAG principles (POUR)
Writing good alt text for images
ARIA attributes — aria-label, aria-describedby, aria-hidden
Landmark roles and how screen readers use them
Keyboard navigation — tabindex, focus management, skip links
Colour contrast requirements and accessible forms

What is Web Accessibility?

Web accessibility means building websites that anyone can use — regardless of ability. This includes people who are blind or low-vision (using screen readers), deaf (needing captions), motor-impaired (using keyboard or switch devices), or cognitively different (needing clear structure). The international standard is the Web Content Accessibility Guidelines (WCAG), maintained by the W3C. It is built on four core principles:

P
Perceivable
All content must be presentable in ways users can perceive — text alternatives for images, captions for video, sufficient colour contrast.
O
Operable
All functionality must be operable — keyboard navigable, no content that causes seizures, users have enough time to interact.
U
Understandable
Content and operation must be understandable — readable text, predictable navigation, input assistance and error identification.
R
Robust
Content must be robust enough that it can be interpreted by a wide variety of assistive technologies — now and in the future.
Accessibility = Better SEO
Accessibility improvements directly boost SEO. alt text helps Google image search. Semantic HTML gives search engines structure. Descriptive link text improves crawlability. Clear heading hierarchy improves content ranking. Building accessible HTML is building better HTML for everyone.

Image Alt Text — The Most Important a11y Rule

The alt attribute on <img> is the single most impactful accessibility attribute. Screen readers read it aloud when the image is encountered. Search engines use it to understand image content. It is also displayed when the image fails to load.

Informative image — describe the content
Write what the image communicates — not what it looks like. Describe the purpose or information conveyed. For a chart: "Bar chart showing course enrolments — Web Dev leads with 2,400 students." Not "Bar chart image."
Decorative image — use empty alt=""
If an image is purely decorative (a divider line, background texture, icon repeated with visible label), set alt="" — an empty string. Screen readers then skip it silently. Never omit the alt attribute entirely — without it, screen readers read the filename aloud (e.g. "img_4827.jpg").
Functional image — describe the action
If an image is inside a link or button, describe what clicking it does — not what the image looks like. For a search icon button: alt="Search". For a logo that links home: alt="K2infocom – go to homepage".
HTML — alt text examples
<!-- ✅ Good: informative — describes content -->
<img src="classroom.jpg"
     alt="Students attending HTML class at K2infocom Vadodara">

<!-- ✅ Good: decorative — empty alt, screen reader skips -->
<img src="divider.svg" alt="" role="presentation">

<!-- ✅ Good: functional — describes what clicking does -->
<a href="/">
  <img src="logo.svg" alt="K2infocom — go to homepage">
</a>

<!-- ❌ Bad: missing alt — screen reader reads filename -->
<img src="img4827.jpg">

<!-- ❌ Bad: redundant alt — says "image of" -->
<img src="teacher.jpg" alt="image of a teacher">

ARIA Attributes — Accessible Rich Internet Applications

ARIA (Accessible Rich Internet Applications) is a set of HTML attributes that add or modify the semantics of elements for assistive technologies. The golden rule: use native HTML first — only add ARIA when there is no semantic HTML element that conveys the meaning. A <button> is always better than <div role="button">.

aria-label
Provides a text label for an element that has no visible text — like an icon-only button. The label is read by screen readers in place of any visible content.
<button aria-label="Close dialog"> <iconify-icon icon="x"></iconify-icon> </button>
aria-labelledby
Links an element to another element that serves as its label, using that element's id. More powerful than aria-label when the label is already visible on screen.
<h2 id="dialog-title">Enroll Now</h2> <dialog aria-labelledby="dialog-title"> ... </dialog>
aria-describedby
Points to an element that provides additional descriptive text for the element — like a hint or error message below a form input. The description is read after the label.
<input type="password" aria-describedby="pwd-hint"> <p id="pwd-hint">Minimum 8 characters</p>
aria-hidden
When aria-hidden="true", the element and all its children are completely hidden from assistive technologies — useful for decorative icons or duplicate content that would be confusing if read aloud.
<span aria-hidden="true">★★★★☆</span> <span class="sr-only">4 out of 5 stars</span>
aria-expanded
Indicates whether a collapsible element (dropdown, accordion, menu) is currently expanded or collapsed. Update it with JavaScript when the state changes — screen readers announce the change.
<button aria-expanded="false" aria-controls="menu" > Menu </button> <ul id="menu">...</ul>
aria-live
Marks a region as a live region — screen readers automatically announce updates to its content. Values: polite (waits for idle), assertive (announces immediately, interrupting). Use for status messages, errors, countdown timers.
<div aria-live="polite" role="status"> Form submitted successfully! </div>
aria-required
Communicates to screen readers that a form field is required. Prefer the native HTML required attribute — but add aria-required="true" when building custom form controls.
<input type="email" required aria-required="true">
aria-invalid
Signals that the current value of a field does not pass validation. Screen readers announce the invalid state. Set it to "true" via JavaScript after the user submits or blurs. Pair with aria-describedby pointing to the error message.
<input type="email" aria-invalid="true" aria-describedby="email-err" > <p id="email-err">Invalid email format</p>

Landmark Roles — Navigation for Screen Readers

Landmark roles define the major structural regions of a page. Screen reader users can press a shortcut key to jump directly to any landmark — bypassing all the content between them. HTML5 semantic elements automatically provide most landmark roles — but you can also use role on <div> elements when needed.

banner → <header>
The site-wide header — logo, site name, top navigation. There should be only one banner landmark per page.
navigation → <nav>
A set of navigation links. Multiple nav landmarks are allowed — use aria-label to distinguish them (e.g. "Main navigation", "Breadcrumbs").
main → <main>
The primary content of the page. Only one main landmark per page. Screen reader users jump here to skip repeated navigation.
complementary → <aside>
Supports the main content but is separate from it — sidebar, related links, pull-quote. Makes sense when taken out of context.
contentinfo → <footer>
Site-level footer — copyright, contact info, legal links. One contentinfo landmark per page.
search → role="search"
No native HTML element for this — use role="search" on the wrapper around a search form so screen reader users can jump to it directly.
region → <section> + aria-label
A significant section of a page — only becomes a landmark when it has an accessible name via aria-label or aria-labelledby.
form → <form> + aria-label
A form landmark — only active when the <form> has an accessible name. Add aria-label="Contact form" to make it a landmark users can jump to.
HTML — Landmark roles in action
<!-- Semantic HTML provides most roles automatically -->
<header>...</header>  <!-- role="banner" -->

<nav aria-label="Main navigation">...</nav>

<!-- role="search" — no native element for this -->
<div role="search">
  <label for="search">Search courses</label>
  <input type="search" id="search">
  <button type="submit">Search</button>
</div>

<main>...</main>   <!-- role="main" -->
<aside>...</aside> <!-- role="complementary" -->
<footer>...</footer> <!-- role="contentinfo" -->

Keyboard Navigation — tabindex & Focus

Many users — with motor impairments, power users, and those using screen readers — navigate entirely by keyboard. Every interactive element must be reachable and operable with a keyboard alone. The Tab key moves focus forward; Shift+Tab moves it backward; Enter and Space activate elements.

tabindex="0"
Makes any element focusable in natural document order. Use this to make a custom interactive element (like a <div> acting as a button) reachable by keyboard. Prefer native interactive elements (<button>, <a>) which are focusable by default.
tabindex="-1"
Makes an element programmatically focusable (via JavaScript's .focus()) but not in the normal tab order. Use this for modal dialogs (focus is sent there on open), skip-link targets, and custom widget internals.
Positive tabindex (avoid)
Values like tabindex="1", tabindex="2" create a custom tab order that overrides natural document flow — making it extremely confusing for keyboard users. Never use positive tabindex values.
HTML — tabindex and keyboard accessibility
<!-- ✅ Native button — focusable by default, use Enter/Space -->
<button type="button" onclick="openModal()">Enroll Now</button>

<!-- ✅ tabindex="0" — custom element in tab order -->
<div
  role="button"
  tabindex="0"
  onclick="handleClick()"
  onkeydown="if(event.key==='Enter'||event.key===' ')handleClick()"
>
  Custom Button
</div>

<!-- ✅ tabindex="-1" — modal focusable programmatically -->
<div role="dialog" tabindex="-1" id="modal">
  ...modal content...
</div>

<!-- JS: send focus when modal opens -->
<!-- document.getElementById('modal').focus() -->

Colour Contrast

Low contrast text is one of the most common accessibility failures — affecting users with low vision, colour blindness, or anyone reading in bright sunlight. WCAG specifies minimum contrast ratios between text and background colour:

Content Type WCAG AA (minimum) WCAG AAA (enhanced)
Normal text (below 18pt / 14pt bold) 4.5 : 1 7.0 : 1
Large text (18pt+ or 14pt+ bold) 3.0 : 1 4.5 : 1
UI components & graphical objects 3.0 : 1 —
Decorative text or logos No requirement

Here are live contrast examples — notice how the failing combinations are hard to read:

K2infocom Sample Text
16.5 : 1 ✓
AAA — Excellent
K2infocom Sample Text
5.2 : 1 ✓
AA — Passes
K2infocom Sample Text
7.4 : 1 ✓
AAA — Excellent
K2infocom Sample Text
1.3 : 1 ✗
FAIL — Unreadable
K2infocom Sample Text
2.1 : 1 ✗
FAIL — Too low
K2infocom Sample Text
4.6 : 1 ✓
AA — Passes
Free Tools to Check Contrast
Use free tools to check contrast ratios before shipping: WebAIM Contrast Checker (webaim.org/resources/contrastchecker), browser DevTools accessibility panel, or the axe browser extension which audits your whole page for a11y issues instantly.

Accessible Form Checklist

Forms are the most common source of accessibility failures. Here is a complete checklist with code for each requirement:

HTML — Fully accessible form
<form
  aria-label="Course enrolment form"
  action="/enroll"
  method="POST"
  novalidate
>

  <!-- 1. Label linked to input via for/id -->
  <label for="name">
    Full Name
    <span aria-hidden="true"> *</span>
    <span class="sr-only">(required)</span>
  </label>

  <!-- 2. Input with hint and validation states -->
  <input
    type="text"
    id="name"
    name="name"
    required
    aria-required="true"
    aria-describedby="name-hint name-error"
    autocomplete="name"
  >

  <!-- 3. Visible hint -->
  <p id="name-hint">Enter your full legal name as on your ID</p>

  <!-- 4. Error message — shown by JS on invalid submit -->
  <p
    id="name-error"
    role="alert"
    aria-live="polite"
    hidden
  >
    Please enter your full name.
  </p>

  <!-- 5. Descriptive submit button -->
  <button type="submit">Submit Enrolment</button>

</form>

<!-- Screen reader only helper class (in CSS) -->
<!--
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border-width: 0;
}
-->

Common Mistakes — Do's & Don'ts

These are the most frequent accessibility mistakes beginners make:

Do — Correct
<!-- Use native button for actions -->
<button type="button"
        aria-label="Close menu"
        onclick="closeMenu()">
  ✕
</button>

<!-- Focus ring in CSS -->
button:focus-visible {
  outline: 3px solid #e44d26;
  outline-offset: 2px;
}

<!-- Descriptive link text -->
<a href="/courses/webdev">
  View Web Dev course details
</a>
Don't — Wrong
<!-- div as button — no keyboard access -->
<div onclick="closeMenu()"
     class="close-btn">
  ✕
</div>

<!-- Removing focus ring (keyboard blind) -->
* { outline: none !important; }

<!-- Vague link text -->
<a href="/courses/webdev">
  Click here
</a>

Try It Yourself

The editor below contains a fully accessible page demo. Click Open in Editor and press Tab in the preview to test keyboard navigation — watch the skip link appear and focus rings move.

Live Editor
✎ Editor
👁 Preview
Try This
Open in Editor and immediately press Tab — the skip link appears at the top. Tab again to move to navigation links. Click Submit Enrolment without filling fields — ARIA error messages appear and aria-invalid="true" is applied. Fill in valid data — the aria-live status message announces success.
Free Resource
HTML5 Cheat Sheet — All Tags & Attributes
One printable PDF covering all ARIA attributes, landmark roles, tabindex rules, skip link patterns, and the WCAG contrast checklist.
Download PDF
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React, and Node.js in one industry-ready curriculum with real projects and placement support.
120+
Chapters
40 hrs
Content
10
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js MongoDB AI Tools

Quick Check — Chapter 15

Expand each question to check your understanding before moving on to the multiple choice quiz.

Questions & Answers
An empty alt="" tells screen readers to skip the image silently — it is marked as decorative and carries no meaningful information. Use it for: purely decorative images (background textures, divider lines), icons that are accompanied by visible text labels, and repeated design elements. Never omit the alt attribute entirely — without it, screen readers fall back to reading the filename (e.g. "img4827.jpg") which is completely meaningless and frustrating. The difference: alt="" = "skip this", no alt = "read the filename".
aria-label provides a direct text label written into the attribute value — ideal for elements with no visible text (icon-only buttons, input fields without visible labels). aria-labelledby points to another element's id and uses that element's text as the label — ideal when the label is already visible on screen (linking a dialog to its heading, linking a region to its <h2>). Prefer aria-labelledby when a visible label exists — it avoids duplicating text and keeps screen reader and visual labels in sync automatically.
The focus outline is the only visual indicator for keyboard users showing which element is currently active. Removing it globally makes the page completely unusable for keyboard-only users — people with motor impairments, screen reader users, and power users who prefer keyboard navigation cannot tell where focus is. This violates WCAG 2.4.7 (Focus Visible). The correct approach is to replace the default outline with a well-designed custom focus ring using :focus-visible (which only shows for keyboard navigation, not mouse clicks): :focus-visible { outline: 3px solid #e44d26; outline-offset: 2px; }.
A skip link is an <a> element placed as the very first element in the body, hidden off-screen by default and visible only when focused. It links to the id of the main content section. When a keyboard user presses Tab on page load, the skip link is the first thing focused — pressing Enter jumps focus directly to main content, bypassing the navigation menu that repeats on every page. WCAG 2.4.1 (Bypass Blocks) requires that pages with repeated navigation provide a way to bypass it. Without a skip link, keyboard users must Tab through every navigation item on every page visit.
aria-live="polite" marks a region as a live region — when its content changes dynamically (via JavaScript), screen readers automatically announce the new content after the user finishes their current task (waits for idle). Use it for: form submission success messages, search results updating, loading complete notifications, cart total updates. aria-live="assertive" interrupts the user immediately — use only for critical errors or time-sensitive alerts. Without aria-live, screen reader users never know when dynamic content changes.
WCAG AA requires a minimum contrast ratio of 4.5:1 between text colour and background colour for normal-sized text (below 18pt or 14pt bold). For large text (18pt or larger, or 14pt bold or larger), the minimum is 3:1. For UI components like input borders, focus rings, and graphical elements: 3:1. Purely decorative text and logos are exempt. WCAG AAA (enhanced) raises the bar to 7:1 for normal text. Use tools like WebAIM's Contrast Checker or the axe browser extension to verify all text combinations in your design.
Multiple Choice Questions
MCQ 1 of 4
Which ARIA attribute provides a text label for an element that has no visible text — such as an icon-only button?
MCQ 2 of 4
Which tabindex value makes an element focusable programmatically via JavaScript but not in the natural tab order?
MCQ 3 of 4
What is the minimum WCAG AA contrast ratio required for normal body text?
MCQ 4 of 4
Which attribute hides an element from assistive technologies while keeping it visually present?
Chapter Summary
  • Accessibility (a11y) ensures websites work for everyone. The WCAG standard is built on 4 principles: Perceivable, Operable, Understandable, Robust (POUR).
  • Alt text: informative images need descriptive alt text; decorative images need alt=""; never omit the attribute entirely. Functional images describe the action, not the appearance.
  • Key ARIA attributes: aria-label (text label for no-text elements), aria-labelledby (references visible label by id), aria-describedby (hints and errors), aria-hidden="true" (hide from assistive tech), aria-expanded, aria-live, aria-invalid.
  • Landmark roles are provided automatically by semantic HTML: <header> = banner, <nav> = navigation, <main> = main, <aside> = complementary, <footer> = contentinfo.
  • Keyboard navigation: tabindex="0" adds to tab order; tabindex="-1" enables programmatic focus only. Never remove focus outlines — replace with custom :focus-visible styles.
  • Skip links (WCAG 2.4.1 required) let keyboard users jump past navigation. Colour contrast must be at least 4.5:1 for normal text (WCAG AA). Use the .sr-only CSS class to provide screen-reader-only text without visual impact.
~10 min read
Beginner level
Chapter 15 of 40

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel