Sub topic 11
Showing errors visually without relying on color
This chapter covers how to make an invalid field look invalid to everyone, including people who cannot tell red from green, use a high-contrast mode, or print the page in grayscale. It assumes you can write basic HTML and CSS and that you know how labels and hint text attach to fields, as covered in earlier chapters. The accessibility wiring (ARIA, error summary, focus) comes in the next chapter. Here the focus is on what people see.
Why color alone fails
Many sites mark an error by turning the input border red. That fails for several groups of users:
- About 1 in 12 men and 1 in 200 women have some form of color vision deficiency. A red border and a gray border can look almost identical to them.
- Users of forced-colors modes, such as Windows High Contrast, have author colors replaced with a system palette. A red border may disappear.
- People on dim screens, in bright sunlight, or reading a grayscale printout lose color distinctions.
- Screen reader users get nothing from a color change at all.
WCAG 2.2 Success Criterion 1.4.1 (Use of Color) says color must not be the only visual means of conveying information or indicating an action. Success Criterion 1.4.11 (Non-text Contrast) adds that visual information needed to identify a component or its state needs at least a 3:1 contrast ratio against adjacent colors.
The four cues of a good error state
A robust error state combines several independent signals. If one fails, another still works.
| Cue | What it does | Works without color? |
|---|---|---|
| Error text | Names the problem and the fix, placed next to the field | Yes |
| Icon | Gives a quick shape-based signal | Yes, if it has a distinct shape |
| Border change | Changes thickness or style, not only hue | Yes, if the change is in width or style |
| Color | Reinforces the other cues | No, so never use it alone |
The error text is the most important cue. The previous chapter on writing error messages covered its wording. Here we treat where it sits and how it looks.
Error text position and prefix
Place the message between the label (and any hint) and the input, or directly below the input. Keep it close to the field it describes so the link is clear. Starting the message with a word such as "Error:" gives a text signal that does not depend on color or an icon.
Do not show the error only in a tooltip or on hover. Touch users and keyboard users may never trigger it.
Border and shape changes
Change the border in a way that is visible in grayscale. Thickness is the most reliable choice, because a 2px gray line and a 4px gray line differ even with no color perception. A left-hand bar on the field group is another common pattern.
Be aware that on a field with a visible border, going from 1px to 3px or 4px is a clear difference. Going from a gray 1px border to a red 1px border is not.
Icons
An icon adds a shape cue. Choose one with a distinct silhouette, such as a circle with an exclamation mark or a triangle. Two rules apply:
- The icon must not be the only cue. Pair it with text.
- If the icon is decorative next to text that already says "Error", hide it from assistive technology so it is not announced twice.
A worked example
The following markup and CSS give an invalid field four cues: a text prefix, a thick border, a left bar, and an icon, all with enough contrast. The icon is drawn with a CSS pseudo-element, so no image file is needed.
<div class="field field--error">
<label for="email">Email address</label>
<p class="error-message" id="email-error">
Error: Enter an email address in the format [email protected]
</p>
<input id="email" name="email" type="email" autocomplete="email"
aria-invalid="true" aria-describedby="email-error"
value="sam.example.com">
</div>:root {
--error: #b3261e; /* about 6.5:1 on white */
--border: #6b6b6b; /* about 5.3:1 on white */
}
.field {
margin-block: 1.5rem;
}
.field input {
display: block;
inline-size: 100%;
max-inline-size: 28rem;
padding: 0.5rem 0.75rem;
border: 2px solid var(--border);
border-radius: 4px;
font: inherit;
}
.field--error {
padding-inline-start: 1rem;
border-inline-start: 6px solid var(--error);
}
.field--error input {
border-width: 4px;
border-color: var(--error);
}
.error-message {
margin: 0.25rem 0 0.5rem;
color: var(--error);
font-weight: 700;
}
.error-message::before {
content: "";
display: inline-block;
inline-size: 1em;
block-size: 1em;
margin-inline-end: 0.4em;
vertical-align: -0.15em;
background: currentColor;
clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.field input:focus-visible {
outline: 3px solid #1a5fb4;
outline-offset: 2px;
}Walk through what a user sees in grayscale:
- A thick bar on the left edge of the field group.
- A triangle icon followed by bold text starting with "Error:".
- An input whose border is twice as thick as a normal field.
Each of these survives removal of color. The red adds emphasis for users who can see it.
Contrast requirements
Three contrast checks apply to error states:
- Error text must meet 4.5:1 against its background for normal-size text (SC 1.4.3). Many "pure red" values such as
#ff0000on white give only about 4:1 and fail. The#b3261eused above passes comfortably. - The input border must have at least 3:1 contrast with the surrounding background (SC 1.4.11). This applies in the normal state too. A pale gray
#cccborder on white is about 1.6:1 and fails. - An icon that conveys meaning needs at least 3:1.
Also check the error colors on whatever background the form sits on. A red that passes on white can fail on a light gray card or a dark theme.
Forced-colors mode
In forced-colors mode the browser overrides most author colors. Borders and text keep their shape but take system colors, and background colors and clip-path fills may change. This is why thickness and text matter: they survive the override.
Two points to check:
- Do not rely on
background-colororbox-shadowalone to show an error. Forced-colors mode removes box shadows and replaces backgrounds. - A
borderstays visible, so a thicker border still reads as different.
You can add a targeted rule for this mode:
@media (forced-colors: active) {
.field--error input {
border-width: 4px;
border-style: double;
}
.error-message::before {
forced-color-adjust: none;
background: CanvasText;
}
}The double border style is a second non-color cue that works when every color is replaced. Using CanvasText keeps the icon visible against the system background.
Common mistakes
- Red border only. The most frequent failure. Add text and a shape change.
- Green for valid, red for invalid, nothing else. Success states need the same care. If you show a success state at all, add a check mark and text, and use it sparingly, because a field without an error is usually enough.
- Light red text on white. Fails 4.5:1. Darken it.
- Error text in the placeholder. It disappears when the user types and has poor contrast in most browsers.
- Messages far from the field. If the text sits at the top of a long page, the user cannot link it to a field without scrolling. Put a message at the field, and (as the next chapter covers) add a summary at the top for long forms.
- Animations that shake the field. Shake effects can trigger motion sensitivity. If you animate, respect
prefers-reduced-motion. - Removing the focus outline. An error state is no use if the keyboard focus indicator vanishes. Keep a clear
:focus-visiblestyle that differs from the error style.
Styling with the attribute
In the earlier example the error class was added by script. Because the markup already sets aria-invalid="true", you can style from that attribute instead. This keeps visual state and assistive state in sync, because one attribute drives both.
input[aria-invalid="true"] {
border-width: 4px;
border-color: #b3261e;
}Avoid styling from the :invalid pseudo-class alone. It matches as soon as the page loads, so an empty required field looks broken before the user has done anything. Reserve error styling for fields you have validated, using the timing rules from the previous chapter.
Checklist
- Does every error include text that names the problem?
- Is there at least one non-color visual cue besides the text, such as a thicker border or an icon?
- Does error text have 4.5:1 contrast and the field border 3:1?
- Does the form still make sense in grayscale and in forced-colors mode?
- Is the error styling tied to
aria-invalidor a state class, not to:invalidat page load? - Is the focus indicator still clearly visible on an invalid field?
Exercise
A form marks invalid fields by changing the border from 1px gray (#ccc) to 1px red (#ff0000). It also shows the text "Please correct this field" in light red. Name three problems with this design and one fix for each.
Show answer
First, color is the only visual change to the field. A user with red-green color blindness or a grayscale display cannot see the difference. Fix: make the border thicker (for example 4px) and add an icon or left bar. Second, the border contrast is too low. #ccc on white is about 1.6:1 and fails the 3:1 requirement for non-text contrast, and #ff0000 on white is about 4:1, which is also weak for text. Fix: use a darker gray such as #6b6b6b for the normal border and a darker red such as #b3261e for errors. Third, the message "Please correct this field" does not name the problem or the fix, and light red text fails the 4.5:1 text contrast ratio. Fix: write a specific message such as "Error: Enter an email address in the format [email protected]" in a dark, high-contrast color.
Exercise
Why is it better to style invalid fields from the aria-invalid="true" attribute than from the :invalid pseudo-class?
Show answer
The :invalid pseudo-class matches any field that fails its constraints, including an empty required field on page load, so users see errors before they have done anything. The aria-invalid attribute is set only after your script or server has decided the field is in error. Styling from it also keeps the visual state and the state exposed to assistive technology tied to one source, so they cannot drift apart.