Skip to main content
Practice Problems

Web accessibility and ARIA attributes

What is Web Accessibility?

Web Accessibility (a11y) means designing websites so that all people, including those with disabilities, can perceive, navigate, and interact with the content. It is both an ethical responsibility and often a legal requirement.


Why Accessibility Matters

  • ~15% of the world's population has some form of disability
  • Improves SEO (search engines read semantic HTML)
  • Better usability for everyone (keyboard users, slow networks)
  • Legal compliance (ADA, WCAG, Section 508)

Core Principles (WCAG)

PrincipleDescription
PerceivableContent must be presentable in ways users can perceive (alt text, captions)
OperableInterface must be operable via keyboard, no time traps
UnderstandableContent and UI must be understandable
RobustContent works across assistive technologies

Semantic HTML First

Using the correct HTML elements is the most important accessibility practice:

html
<!-- โŒ Bad โ€” div with click handler --> <div onclick="navigate()">Go to Dashboard</div> <!-- โœ… Good โ€” proper link --> <a href="/dashboard">Go to Dashboard</a> <!-- โŒ Bad โ€” div styled as button --> <div class="btn" onclick="submit()">Submit</div> <!-- โœ… Good โ€” real button --> <button type="submit">Submit</button>

What is ARIA?

ARIA (Accessible Rich Internet Applications) is a set of HTML attributes that provide additional semantics for assistive technologies (screen readers).

Key ARIA Attributes

AttributeDescriptionExample
roleDefines element's purposerole="navigation", role="dialog"
aria-labelText label for elementaria-label="Close menu"
aria-labelledbyReferences another element as labelaria-labelledby="heading-id"
aria-describedbyReferences element with descriptionaria-describedby="help-text"
aria-hiddenHides element from screen readersaria-hidden="true"
aria-expandedIndicates expandable statearia-expanded="false"
aria-requiredMarks field as requiredaria-required="true"
aria-liveAnnounces dynamic content changesaria-live="polite"
aria-disabledIndicates disabled statearia-disabled="true"

ARIA Example

html
<button aria-expanded="false" aria-controls="dropdown-menu" aria-label="Open navigation menu" > โ˜ฐ Menu </button> <nav id="dropdown-menu" aria-hidden="true" role="navigation"> <ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> </ul> </nav>

Essential Accessibility Practices

1. Alt Text for Images

html
<!-- Informative image --> <img src="chart.png" alt="Sales grew 40% in Q3 2024"> <!-- Decorative image โ€” empty alt --> <img src="decoration.svg" alt="">

2. Keyboard Navigation

css
/* Never remove focus outlines without replacement */ :focus { outline: 2px solid #007bff; } /* Custom focus style */ :focus-visible { outline: 2px solid #007bff; outline-offset: 2px; }

3. Color Contrast

  • Minimum contrast ratio: 4.5:1 for normal text
  • Minimum contrast ratio: 3:1 for large text
  • Don't rely on color alone to convey information

4. Form Labels

html
<!-- โœ… Associated label --> <label for="email">Email:</label> <input type="email" id="email" name="email"> <!-- โŒ No label association --> <input type="email" placeholder="Email">

Important:

The first rule of ARIA: don't use ARIA if you can use a native HTML element that already has the semantics you need. A <button> is always better than <div role="button">. Use ARIA only to fill gaps that HTML cannot cover.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

Finished reading?
Practice Problems