KodingKaro
CSS · Intermediate · Forms and UI Styling

Validation, errors, and accessible states

A polished form makes validation feedback easy to notice and understand, including for visitors who do not distinguish the chosen colors.

✓ What you will learn in this topic

  • Explain the key idea: Style real states and pair color changes with clear semantic and textual feedback.
  • Apply the concept: Invalid controls and their error copy are easier to identify; the focused field label receives a matching emphasis.
  • Avoid this common mistake:

Concept explanation

Pair an invalid border or icon with an explicit error message in the page. A valid state should not be indicated only by green. The HTML should connect errors to controls and communicate invalid state; CSS can make that relationship visible but cannot supply the missing semantics.

Use :disabled, :checked, and :required only when those actual states apply. Keep contrast high enough for text and control boundaries.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo [aria-invalid="true"] {
  border-color: #b91c1c;
}
.demo .error {
  color: #991b1b;
  font-weight: 600;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo [aria-invalid="true"] {
  border-color: #b91c1c;
}
.demo .error {
  color: #991b1b;
  font-weight: 600;
}

Expected result: Invalid controls and their error copy are easier to identify; the focused field label receives a matching emphasis.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.form-field:has(input:focus-visible) label {
  color: #1d4ed8;
}
.form-field .error {
  border-inline-start: 0.25rem solid #b91c1c;
  padding-inline-start: 0.75rem;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Removing native control affordances or using placeholder text as the only label.

Best practice

Keep explicit labels, visible focus, clear error text, and adequate target sizes; test native controls on real browsers.

Practice exercise

Build a sign-up form with labeled fields, a visible focus state, and an error message that is not communicated by color alone. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Style real states and pair color changes with clear semantic and textual feedback.
  • Keep explicit labels, visible focus, clear error text, and adequate target sizes; test native controls on real browsers.

Knowledge check

Why should an error not be communicated only with red?

Reveal the explanation

A text message, icon with an accessible name, or other cue makes the state clearer.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo [aria-invalid="true"] {
  border-color: #b91c1c;
}
.demo .error {
  color: #991b1b;
  font-weight: 600;
}

Expected result:

Invalid controls and their error copy are easier to identify; the focused field label receives a matching emphasis.

Hands-on practice task

Build a sign-up form with labeled fields, a visible focus state, and an error message that is not communicated by color alone.

In this section

Other lessons in Forms and UI Styling

Broaden your knowledge

Related learning topics