Hey there, Fellow Coder! š
Letās talk about something super important but often ignored: Accessibility (or a11y for short). You might think, āMy app looks good, it works, isnāt that enough?ā Well, imagine building a coffee shop but putting stairs right in front of the door with no ramp. You just blocked a lot of people from getting their caffeine fix! š±
Making your React app accessible isnāt just about being āniceā. Itās about ensuring everyone, including people with disabilities, can use what you built. Plus, it helps with SEO! Google loves accessible sites.
Letās look at 3 simple ways to improve accessibility in React.
1. Semantic HTML: The Foundation
The biggest mistake? Using <div> for everything.
āI can just style a div to look like a button!ā
Yes, you can. But a screen reader wonāt know itās a button.
Bad Practice:
// Screen reader says: "group"
<div onClick={submitForm} className="btn-primary">
Submit
</div>
Good Practice:
// Screen reader says: "button, Submit"
<button onClick={submitForm} className="btn-primary">
Submit
</button>
By using the correct HTML tag (<button>, <main>, <nav>, <article>), you get accessibility features for free!
2. Alt Text for Images
This is Accessibility 101. Every <img> tag needs an alt prop.
This describes the image to users who canāt see it.
Bad Practice:
<img src="logo.png" />
Good Practice:
<img src="logo.png" alt="Company Logo" />
If the image is purely decorative (like a background shape), use an empty string alt="". This tells the screen reader to skip it.
3. Donāt Overuse ARIA
ARIA (Accessible Rich Internet Applications) attributes are powerful, but rule #1 of ARIA is: Donāt use ARIA if you can use native HTML.
Use aria-label when you have a button with only an icon and no text.
Example:
<button aria-label="Close Menu" onClick={closeMenu}>
<svg>...</svg>
</button>
Without aria-label, a screen reader might just say ābuttonā with no context. With it, it says āClose Menu buttonā. Much better!
Quick Check: The Keyboard Test āØļø
Want to test your app? Put your mouse away. Try to navigate your site using only the Tab key.
- Can you reach all the buttons and links?
- Can you see which element is focused (outline)?
If you canāt, you have some work to do!
Closing
Accessibility isnāt a āfeatureā you add at the end. Itās part of the process. Start small: use semantic HTML, add alt text, and test with your keyboard. Your users (and your SEO ranking) will thank you!
Happy coding! š»ā¤ļø