Basic Web Accessibility
A Simple Approach for Accessibility Implementation in Web Development
Want some music while reading? 🎵📻
Hello everyone!
It has been a long time since the last time I wrote a post. I’ve been really busy with my new job and I haven’t had time to write anything 😅. But today I have some time to write and I promise that I’m not going to let you down with this post.
I’m now working on a project for my current workplace. The project is a fully functional website to manage patients and their health data on a daily basis for doctors and nurses. Since I’m working in the Healthcare Industry, every line of code needs to be written carefully and optimized. Honestly, I’d be a lie to say that I haven’t struggled. The reason is that I have to adapt to the big corporation’s strict coding style. I’m used to code for my personal project with my own relaxed style, therefore, I have to try to learn many new things and adapt to the new ways of doing things.
Thanks to this big and important project, I now have a chance to practice my accessibility knowledge. I realized that there’s a lot of things I still have to learn. So let’s take a look on this topic!
What Is Web Accessibility
W3C has defined Web accessibility means that websites, tools, and technologies are designed and developed so that people with disabilities can use them. More specifically, people can:
- perceive, understand, navigate, and interact with the Web
- contribute to the Web
Why Do We Need Web Accessibility?
Web developers codes a fully functional website for all kinds of user to use. A website that lacks accessibility is a hindrance for the users with disabilities to use it. To avoid unintentional discrimination and bring the website nearer to all kinds of user out there, web developers should take accessibility into consideration when they start coding a website.
How To Get started
There are many sources out there on this topic, I could give you a couple of good ones:
- Web Accessibility Initiative (WAI) - This source is very good for the basic ground knowledge
- ARIA - If you want to make your HTML documents more accessible, this is the source you should take a look into
- DigitalA11Y Cheat Sheet - This is a fantastic source to have a quick look into the ARIA roles, attributes and states for HTML elements
- Accessibility Cheat Sheets - A list of different accessibility cheat sheets for common ARIA roles and attributes
Document Structure
If you got used to using <div>
tag for all of the website blocks, please stop doing this. <div>
tag should only be used for generic blocks that make no sense if using any other tags.
HTML5 has implemented many different tags for us to structure the HTML document better and improve web accessibility from the root.
This is the recommended HTML body structure
|
|
Common ARIA Roles and Attributes
Different ARIA roles can be checked here
Different ARIA attributes can be checked here
aria-label
This is one of the most common ARIA attribute used in web development. There are many elements that don’t have native <label>
tag to go with them, therefore, we should use aria-label
to provide more information for screen-reader. But please notice that this attribute should be used for interactive elements only such as <button>
, menu
, or dialog
, etc. and don’t over use it.
If your input already goes with <label>
element, your <iframe>
has title, or images have <alt>
, you don’t need this attribute.
Here’s a practical example:
|
|
aria-checked / aria-selected / aria-pressed
These 3 attributes are used with <input type="checkbox">
, <input type="radio">
and toggle button. The toggle button is not provided natively in HTML, but if you create a customized toggle button, aria-pressed
can be used to indicate its pressed state.
|
|
Practical example
Dialog
For example, you want to create a simple dialog that display some information. The code could be like this
|
|
Since we don’t have any native HTML element for dialog, we use <div>
. Because of this, we add role
for this <div>
to specify that it’s a dialog. The dialog is labeled by a heading with id dialogTitle
so we add aria-labelledby
for screen-reader to know that the dialog’s title has a dialogTitle
id and it’s the same idea for aria-describedby
.
Navbar That Has Dropdown menu
Another example is a navigation bar that has dropdown menus. The following navbar is an example navbar for a bookstore’s website.
|
|
As we could see, the Categories navigation item has a dropdown menu so we should specify aria-haspopup
attribute. The state of the menu is closed at the moment so the aria-expanded
attribute value is false
. The second <ul>
element is now a dropdown menu, therefore, its role should be menu
. Finally, the <li>
elements are the menu’s items so they should have the roles menuitem
.
Conclusion
If you read until now, you have seen the basics of accessibility and maybe you could imagine how far we can go with this. If you really want to take accessibility seriously, it would take hours to code a component so don’t overuse this.
Hope you enjoy the post and learn new things after this! Thanks for reading! 🙂