Topic 06 / 08

Web Accessibility (a11y) & ARIA Basics

1. Simple Definition

Web Accessibility (a11y) ka matlab hai aisi websites develop karna jise har tarah ke log use kar sakein — chahe wo visually impaired hon (screen reader users), physically challenged hon (sirf keyboard use karne wale), ya color-blind hon. ARIA (Accessible Rich Internet Applications) HTML ko extra attributes provide karta hai jab native HTML tags kam pad jaate hain.

2. Real-Life Analogy

♿ Metro Station Ke Ramps Aur Audio Announcements

Metro station par seedhiyon ke sath-sath wheelchair ramp banaya jata hai taaki har koi travel kar sake. Blind logon ke liye platform par yellow tactile tiles aur audio announcements hoti hain ("Darwaze baayi taraf khulenge"). Web par yehi role Accessibility aur ARIA ka hai!

3. Why Do We Need It?

World Health Organization (WHO) ke anusaar dunya ke 16% log kisi na kisi disability ke sath jeete hain. Accessibility koi "nice-to-have" feature nahi hai, balki legal requirement (US ADA, European Accessibility Act) aur human empathy ka part hai.

4. Syntax

<!-- 1. Icon Button with Accessible Name -->
<button aria-label="Close search window">❌</button>

<!-- 2. Expanding Accordion State -->
<button aria-expanded="false" aria-controls="faq-ans">Kyun a11y zaroori hai?</button>

<!-- 3. Tab Navigation Role -->
<div role="tablist">
  <button role="tab" aria-selected="true">Tab 1</button>
</div>

5. Basic Example (Accessible Icon Buttons & Alerts)

<!-- Icon-only button that screen readers can read -->
<button class="theme-btn" aria-label="Toggle Dark Theme" onclick="toggleTheme()">
  🌙
</button>

<!-- Screen Reader Only Live Region for Dynamic Alerts -->
<div aria-live="polite" class="sr-alert">
  Item cart me add ho gaya!
</div>

6. Output / Expected Result

Accessible Icon Button
Sighted users ko play symbol dikhega; Screen reader bolegi: "Play Tutorial Video button".

7. Code Explanation

  • aria-label: Jab kisi button me sirf icon ya symbol hota hai (jaise 🔍 ya ❌), toh screen reader ko bolne ke liye text label provide karta hai.
  • aria-expanded="true/false": Screen reader ko batata hai ki dropdown menu ya drawer abhi khula hai ya band hai.
  • tabindex="0": Kisi custom element ko keyboard TAB key se focusable banata hai.
  • tabindex="-1": Element ko keyboard sequence se hata deta hai lekin JavaScript se programmatically focus karne deta hai.
  • aria-hidden="true": Purely decorative icons (jaise decorative background shapes) ko screen reader se hide kar deta hai.

8. Real-World Example

Google search results, Twitter drawer navbars, aur Apple websites me sabhi interactive icons ke sath aria-label aur aria-expanded judda rehta hai.

9. The First Rule of ARIA (Most Important)

⚠️ "No ARIA is better than Bad ARIA"

Agar aapke paas native HTML element available hai, toh ARIA mat lagayein!
❌ Bad: <div role="button" onclick="...">Click</div>
✅ Best: <button onclick="...">Click</button> (Native button me keyboard Space/Enter, focus outline pehle se shamil hota hai).

10. Best Practices

  • Focus states ko CSS me outline: none; karke gayab mat kijiye! Visible focus ring keyboard users ke liye ankhon ki tarah hoti hai.
  • Text aur background ke beech minimum 4.5:1 contrast ratio (WCAG AA) hona chahiye.
  • Apni website ko sirf TAB key daba kar bina mouse ke chala kar test karein.

11. Try It Yourself

Playground me ek search icon button banayein aur usme aria-label="Search all lessons" add karein.

12. Challenge

Ek accessible mobile drawer toggle button banayein jisme JavaScript se click karne par aria-expanded true aur false switch ho.

13. Interview Questions

💼 Q: WCAG kya hai aur uske 4 core principles kya hain?

Answer: WCAG ka full form hai Web Content Accessibility Guidelines. Iske 4 core principles hain POUR:
1. Perceivable: Content sabhi senses (sight, hearing) se mehsoos ho sake.
2. Operable: Website keyboard aur touch sabhi se operate ho sake.
3. Understandable: Bhasha aur navigation clear ho.
4. Robust: Alag-alag screen readers aur browsers me stably chale.

14. Quick Revision

  • a11y = accessibility (11 letters between a and y).
  • Native tags > ARIA hacks.
  • aria-label icon buttons ke liye life saver hai.
  • Visible focus indicators hamesha maintain rakhein.

15. FAQ

Q1. Screen readers blind users ke alawa aur kaun use karta hai?

Low-vision users, dyslexia wale log, aur wo log jo lambe articles sunna pasand karte hain.

Q2. tabindex="5" likhna achha hai ya bura?

Bahut bura (Anti-pattern)! Positive tabindex (1, 2, 5) natural keyboard tab order ko tod deta hai. Sirf 0 ya -1 use karein.

Q3. Color contrast check karne ke liye best tool kya hai?

Chrome DevTools ka Inspect element tool colors par direct WCAG AA/AAA checkmark dikhata hai.

Q4. aria-live attribute kya karta hai?

Jab page refresh hue bina koi notification aati hai (e.g. "Order Placed"), screen reader ko bina user ke move kiye wo message announce karne ko kehta hai.

Q5. Keyboard focus ko visually kaise style karein?

CSS me :focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; } use kiya jata hai.

Topic 06 / 08

Web Accessibility (a11y) & ARIA Basics

1. Simple Definition

Web Accessibility (a11y) is the practice of designing and developing web applications so that all people, including those with visual impairments (screen reader users), motor disabilities (keyboard-only navigators), and cognitive or sensory conditions, can browse and interact effortlessly. ARIA (Accessible Rich Internet Applications) provides specialized attributes that enhance HTML semantics when native tags alone cannot convey dynamic UI states.

2. Real-Life Analogy

♿ Transit Station Wheelchair Ramps & Audio Announcements

At a modern rapid transit station, stairs are accompanied by ramps and elevators so that wheelchair users can navigate safely. For blind passengers, platforms feature tactile paving stones and clear audio announcements ("Doors opening on the left"). On the web, Accessibility and ARIA serve that exact equal-access purpose!

3. Why Do We Need It?

According to the World Health Organization (WHO), over 1.3 billion people live with some form of disability. Accessibility is not merely a bonus feature; it is an ethical imperative and a legal standard (US ADA Title III, European Accessibility Act).

4. Syntax

<!-- 1. Icon Button with Accessible Name -->
<button aria-label="Close search window">❌</button>

<!-- 2. Expanding Accordion State -->
<button aria-expanded="false" aria-controls="faq-ans">Why is accessibility important?</button>

<!-- 3. Tab Navigation Role -->
<div role="tablist">
  <button role="tab" aria-selected="true">Tab 1</button>
</div>

5. Basic Example (Accessible Icon Buttons & Alerts)

<!-- Icon-only button readable by screen readers -->
<button class="theme-btn" aria-label="Toggle Dark Theme" onclick="toggleTheme()">
  🌙
</button>

<!-- Screen reader live region for dynamic notices -->
<div aria-live="polite" class="sr-alert">
  Item added to cart!
</div>

6. Output / Expected Result

Accessible Icon Button
Sighted users see a play icon; screen readers announce: "Play Tutorial Video button".

7. Code Explanation

  • aria-label: Supplies an invisible accessible string for icon-only buttons without text content.
  • aria-expanded: Informs screen reader users whether an accordion or dropdown menu is open (true) or closed (false).
  • aria-live="polite": Tells the screen reader to announce dynamic DOM updates once the user finishes their current action.

8. First Rule of ARIA

⚠️ The First Rule of ARIA: Use Native HTML First

Never recreate native elements with <div role="button"> when a native <button> tag is available! Native buttons possess built-in Tab key focus, Enter key submission, and Space bar triggering for free.

9. Best Practices

  • Ensure color contrast ratio meets WCAG AA standards (minimum 4.5:1 for normal text).
  • Never remove CSS outline (outline: none) without providing a distinct visible alternative focus style.
  • Always provide descriptive alt text for non-decorative images.

10. Practice Exercise

🎯 Exercise: Keyboard Accessible Dropdown

Build a dropdown toggle button with aria-haspopup="true", aria-expanded="false", and ensure pressing the Escape key closes the dropdown and returns focus to the button.

11. Mini Challenge

⚡ 15-Minute Challenge: Screen Reader Audit

Turn on Windows Narrator (Win + Ctrl + Enter) or Mac VoiceOver (Cmd + F5) and navigate your application purely using keyboard Tab and arrow keys!

12. Interview Questions

💼 Q: What is the difference between aria-label, aria-labelledby, and aria-describedby?

Answer: aria-label provides a hardcoded accessible text string. aria-labelledby points to the ID of another visible element providing the primary label. aria-describedby points to the ID of secondary helper or error text.

13. Summary / Cheat Card

  • Semantic HTML is the foundation of accessible web applications.
  • Use aria-label for icon-only interactive controls.
  • Never break keyboard navigation or suppress visible focus outlines.

14. FAQ

Q1. What does "a11y" stand for?

"a11y" is a numeronym for "accessibility" (the letter 'a' followed by 11 letters, ending with 'y').

Q2. Can ARIA fix bad semantic markup?

No. ARIA modifies screen reader perception, but does not fix keyboard focus or browser event handling behavior.

Q3. What is a "skip link"?

A skip link is an anchor placed at the very top of a webpage that allows keyboard users to jump directly to main content, skipping repeated navigation bars.