Sub topic 11
Showing errors visually without relying on color
This chapter assumes you can write basic HTML and CSS and that you know how a label is tied to an input. You should also have read the previous chapter on writing error messages. Here we focus on how an error looks, so that every user can notice it.
Why color alone fails
Red is the usual color for errors. About 1 in 12 men and 1 in 200 women have some form of color vision deficiency, and many people use screen magnification or high-contrast modes that change colors. WCAG success criterion 1.4.1 (Use of Color) says color must not be the only way to show information. An error that is only a red border fails that rule.
An error state needs at least two signals. The first is color. The others should come from this list:
- Text that says an error occurred.
- An icon next to the message.
- A change in shape or weight, such as a thicker border.
- Position, such as a message directly above or below the field.
Build the error state in layers
A strong error state combines several of these signals. Text is the most important one, because it works for everyone, including users who cannot see color at all.
| Signal | What it does | Works without color? |
|---|---|---|
| Red border | Draws the eye | No |
| Thicker border (3px, not 1px) | Changes shape | Yes |
| Error icon | Marks the message | Yes |
| Message starting with "Error:" | States the problem | Yes |
| Message placed next to the field | Links message and field | Yes |
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
class="field field--error"
aria-invalid="true" aria-describedby="email-error">
<p id="email-error" class="error-message">
<span class="error-icon" aria-hidden="true">!</span>
Error: Enter an email address in the format [email protected]
</p>.field {
border: 1px solid #595959;
padding: 0.5rem;
}
.field--error {
border: 3px solid #b3261e;
}
.error-message {
color: #b3261e;
font-weight: 700;
margin: 0.25rem 0 0;
}
.error-icon {
display: inline-block;
width: 1.25em;
height: 1.25em;
margin-right: 0.35em;
border-radius: 50%;
background: #b3261e;
color: #fff;
text-align: center;
line-height: 1.25em;
}The icon has aria-hidden="true" because the words "Error:" already tell a screen reader user what happened. Announcing the icon as well would repeat the information.
Check contrast
Color still matters, and it must be readable. WCAG 1.4.3 requires a contrast ratio of at least 4.5:1 for normal text against its background. A pale red such as #ff6b6b on white fails this test. The #b3261e used above passes on white. WCAG 1.4.11 also asks for a 3:1 ratio for the visual boundary of a control, so the border needs to be dark enough against the page, not only different from the unflagged state.
Also check the error color against the field's own background. If you tint the field pink, test the text against the pink, not against white.
Common mistakes
- Using the red border as the only cue. Add text and an icon.
- Using light-gray text for the message. Gray has low contrast and reads as disabled.
- Putting the message far from the field. Place it between the label and the input, or directly below the input.
- Hiding the icon from view but not from screen readers, or the reverse. Decorative icons should be hidden from assistive technology, and meaningful text should not be.
- Relying on
:invalidstyling alone. It can flag fields before the user has typed anything. Apply the error class from your validation script, as in the earlier validation chapter.
Test it without color
Open your browser's developer tools and emulate a vision deficiency, or apply a grayscale filter to the page. Then look at the form. If you can still find every field with a problem, the state works without color. Also try Windows High Contrast mode, which overrides your colors. Borders survive it, and background-only cues do not.
Exercise
A form marks invalid fields by turning the border from gray to red. Name two changes that would make the error visible to a user who cannot distinguish red from gray, and say what contrast ratio the message text needs.
Show answer
Add a text message that begins with "Error:" next to the field, and add an icon or a thicker border. Any two of these work, because each one signals the error without color. Normal-size message text needs a contrast ratio of at least 4.5:1 against its background (WCAG 1.4.3).