Skip to content

Fixing the sixmost commonaccessibility bugs

Accessibility with just HTML and CSS

Corina Murg smiling. Corina has brown hair and light skin. She is wearing a colorful beanieCorina Murg|
Published on March 2, 2024

Introduction

The 2024 WebAIM Million Study of 1,000,000 home pages revealed a group of 6 bugs present on 96% of the pages. Let's make sure your portfolio site is not one of them.

Color contrast

Text 18.5px or smaller:
min. contrast ratio of 4.5:1
Bold text larger than 18.5px:
min. contrast ratio of 3.0:1
Text 24px or larger:
min. contrast ratio of 3.0:1

Mind the color contrast ratio to help users with low vision

For users with low vision reading can be challenging when the text color doesn't stand out much from the background. Noticing interactive elements like buttons or form inputs is also difficult if the background or border does not contrast enough with the surrounding content.

To avoid these problems, we rely on color contrast ratio tools to compare the difference in luminance (brightness) between any two colors. The ratios 4.5:1, for smaller text, and 3.0:1, for larger text, are minimum values expected in order for your content to be accessible to users with low vision. In terms of accessibility guidelines, this is a AA level requirement.

The desirable AAA level requires a 7.0:1 contrast ratio for smaller text, and 4.5:1 for larger text. Whenever possible, aim for the AAA level as it provides the best experience for users with low vision.

How to check the color contrast ratio

When you run an accessibility report with automated tools like Google's Lighthouse or axe DevTools, if any elements failed the contrast ratio guidelines, you will know right away.

If you'd like to be more proactive and test your colors as you're building, use WebAIM's contrast checker.

Webaim's contrast checker tool

Checking the color contrast is straightforward:

✔enter the foreground and background colors for a given element

✔the tool calculates the contrast ratio

✔you receive a pass or fail result for the AA and AAA levels of compliance

Webaim's contrast checker results

A neat feature of the tool: it provides luminance sliders that allow you to adjust the levels and see how the contrast ratio changes.

For quick access, you can also add the contrast checker to your Bookmarks bar in one easy step!

Did you know?Links also have to follow the contrast rules!

Here's a quick checklist to make sure your links stand out from the surrounding text and the background:

✔Contrast with background is at least 4.5:1 for smaller text, and 3.0:1 for larger text

✔Links are underlined, OR the next two conditions are met:

✔Contrast with surrounding text is at least 3.0:1

✔Non-color visual clues (like underline or a focus ring) are available on mouse hover and keyboard focus

And to help us, WebAIM has a special links contrast checker.

Webaim's link contrast checker

The "alt" attribute for images

<imgsrc="..."alt="Fill me in or leave me empty, but do include me!"/>

Help screen readers decide whether to announce an image or not

Is your image purely decorative? Or, is the information presented in the image conveyed by the adjacent text as well? Then it's best to leave the alt attribute empty.

Does your image contain essential information that is not available in the text? Then add a short description in the alt attribute. No need to add the word "image" or "picture" in the description, as screen readers announce it as an image by default.

Either way, it's important to add the alt attribute to all images.

Missing, empty, or descriptive alt? alt attribute

Missing: screen readers might read the source file name. When you have an image, the attribute is required!

Empty: screen readers ignore the image. If your image is purely decorative, the attribute should be empty: alt=""

Descriptive: if the image is important for understanding your content use the attribute to describe it: alt="some description"

Remember: A missing alt attribute is NOT the same as an empty alt!

Buttons with discernable text

<button><img alt="Close" src="x-mark.svg"/></button>

Help screen readers properly announce a button

The techniques we covered in the previous section apply to buttons as well. With an image, you have a new option: use the alt attribute to describe the image, and it will become the accessible name of the button.

In the example above, the button has no visible text, but contains an image with an alt attribute. A screen reader will announce it as "Close graphic button".

Notice the "graphic" part? Since the image has an alt attribute that is not empty, the screen reader has to announce there is an image present. It is up to you if this information is useful to the user or not (most likely not?!). If not, use one of the techniques discussed in the Links section, and remember to include the alt attribute but leave it empty: alt="".

Labels for inputs

// Implicit label by wrapping the input<label>Enter your full name: <input id="name" type="text"/></label>

// Explicit label by referencing the id of the input<label for="name">Enter your full name:</label><input id="name" type="text">

Help assistive tech users understand the purpose of an input

When it comes to your portfolio site, labels and inputs are likely to be part of your contact form. We will look at ways to make your contact form accessible in a separate article, but now let's focus on the label and input elements.

Why are labels important?

For screen reader users, the label is the only way to understand the purpose of the input.

Speech command users rely on the label to select the input or any other form field.

Sighted users can click on the label to bring focus to the input. If you are a sighted developer, this is actually an easy way to test if a label is properly associated with an input.

Which method should you use: implicit or explicit?

Both are valid, but the implicit method (label wrapping the input) will cause problems for users relying on speech commands, specifically those using the Dragon NaturallySpeaking software. Why? Well, simply because there's a bug in their code!

Since Dragon is the most popular speech recognition software, it's best to use the explicit method (with the for attribute) until they fix the issue.

The language attribute

<html lang="en">

Use a valid language in the lang attribute

It enables screen readers to correctly identify the language of a webpage, so it can use the correct pronunciation.

Is it common for a project to lack this attribute? Not at all! That's because when we initiate a new code base we are likely to use editors that generate an HTML file pre-configured with all the essential elements, including the lang attribute.

Portfolio sites that started as templates though might be missing it. These templates are designed to be customized, so remember to add the lang attribute with the value of the language you are using.

Did you know?The lang attribute can do a lot more!

It can take a specific dialect:

<html lang="en-US">

It allows you to specify the language of a certain word or section of the page:

<p>Hello in English, <span lang="fr">Bonjour</span> in French.</p>

What next?

Congratulations! You are now familiar with the 6 most common accessibility issues and how to avoid them in your portfolio site. Ready to continue your accessibility bug hunt?