Asia/Dhaka
Posts

A Developer's Guide to Web Accessibility (a11y)

October 20, 2024
Web Accessibility (often abbreviated as "a11y" because there are 11 letters between 'a' and 'y') is the practice of ensuring your websites and applications can be used by people of all abilities and disabilities. While accessibility is a legal requirement in many jurisdictions, it's fundamentally about empathy and creating a better user experience for everyone. Good accessibility often translates to better SEO, cleaner code, and improved usability on mobile devices. The easiest way to make a site accessible is to use correct HTML elements. Screen readers rely heavily on semantics to understand the structure of a page.
  • Don't use <div> for buttons: A <div> with an onClick handler is not a button. It lacks keyboard focusability and doesn't tell a screen reader it's actionable. Use <button>.
  • Headings matter: Use <h1> through <h6> in logical order. Do not skip heading levels (e.g., jumping from <h2> to <h4>). Screen reader users often navigate pages by skipping between headings.
  • Use landmarks: Use <header>, <nav>, <main>, <article>, and <footer> to define regions of your page.
Many users rely solely on a keyboard to navigate the web.
  • Test it yourself: Put away your mouse and try to use your application using only the Tab, Shift+Tab, Enter, and Space keys.
  • Focus Management: Ensure every interactive element (links, buttons, inputs) has a clearly visible focus state. Do not remove the default browser outline without replacing it with a prominent custom style (outline: none without a fallback is an accessibility failure).
Images are invisible to screen readers unless you provide alternative text.
  • The alt attribute: Every <img> tag must have an alt attribute. If the image conveys meaning, describe it concisely (e.g., alt="A golden retriever catching a frisbee").
  • Decorative images: If the image is purely decorative and adds no meaning, use an empty alt attribute (alt=""). This tells the screen reader to ignore it completely.
Information must not be conveyed by color alone. For example, if a form error is indicated only by turning the input border red, colorblind users may not notice it. Always include text or an icon. Furthermore, ensure sufficient contrast between text and background colors. The WCAG guidelines recommend a contrast ratio of at least 4.5:1 for normal text. ARIA (Accessible Rich Internet Applications) attributes allow developers to add accessibility semantics to complex custom widgets. However, the first rule of ARIA is: No ARIA is better than bad ARIA. Always prefer native HTML elements over custom ARIA components. Use ARIA only when you are building complex UI patterns (like custom dropdowns, modal dialogs, or tabs) that cannot be built with standard HTML. By keeping these principles in mind during development, you can build a more inclusive web for everyone.
On this page
Hey! Got questions?Ask me anything 👋