Skip to content
Tutorials Center
My learning

Topics

Lesson 132 of 139 ยท Projects

Project: Contact Us Page

Build a contact page in four stages with an accessible form, a two-column CSS grid, clear focus styles and validation states using :user-invalid and :has().

Almost every site has a contact page, and almost every contact form is a little bit broken: tiny labels, fields that stretch across the whole screen, error colors nobody can distinguish, and a submit button that looks like plain text. It is a perfect project for practicing forms, because a good one is mostly about restraint and clarity.

In this project you will build a contact page for a fictional bike workshop, Spoke & Sprocket, in four stages. You will lay out the form with CSS Grid, style the fields for keyboard and touch users, and show validation states with :user-invalid, which only reacts after the person has interacted with a field. The background lessons are Styling Forms and Pseudo-classes.

Each editor holds the complete code so far. The form does not send anything: a tiny script prints what would be submitted, because previews on this site cannot post to real servers.

Stage 1: Accessible markup

Accessibility starts in the HTML, before any CSS:

  • Every field has a visible <label> connected with for and id. A placeholder is not a label: it disappears as soon as you type.
  • The right type (email, tel) brings the right keyboard on phones, and autocomplete lets browsers fill in known values.
  • required marks mandatory fields and gives you free validation. A short hint under a field is linked with aria-describedby, so screen readers read it together with the label.
  • Related choices go in a <fieldset> with a <legend>.

At this stage there is almost no CSS, just a font and a readable width, so you can see how usable plain HTML already is.

Stage 1: the markup

Stage 2: A two-column grid

Short fields (name and email, side by side) look better than a tall single column on wide screens. The form becomes a grid with two flexible columns. Fields that need the full width (the fieldset, the message, the button) get grid-column: 1 / -1, which means "from the first line to the last line", no matter how many columns exist (Grid Placement).

The columns use repeat(auto-fit, minmax(min(100%, 15rem), 1fr)). On a narrow phone, each column needs at least 15rem, so they stack automatically with no media query. Gaps replace margins between fields (Grid Tracks).

Wrapping each label and input in a .field block makes them one grid item, and display: grid on .field stacks the label above its input with a small gap.

Stage 2: grid layout

Notice that the fields use font: inherit. Form controls do not inherit the page font by default, which is why unstyled inputs look different from the text around them. width: 100% plus the global box-sizing: border-box makes each input fill its grid cell exactly, padding and border included.

Stage 3: Focus, hover and touch comfort

A form must be obvious to use without a mouse. Three details do most of the work:

  1. A strong focus ring. Replacing the default outline is fine, but never remove it without a replacement. Here the focused field gets a thick colored outline with outline-offset, and the border color changes too (Outline).
  2. Large targets. Inputs have about 2.75rem of height, comfortably above the 44px recommended for touch. Radio labels get padding so the clickable area is generous.
  3. Hover feedback that is not the only cue. Hover darkens the border slightly, but focus is the state that matters.

accent-color colors the native radio buttons and checkboxes with the brand color without custom artwork (User Interface). Finally, :focus-visible on the button shows the ring only for keyboard users.

Stage 3: focus and interaction

Press Tab to walk through the form: the blue ring tells you where you are. Click a field with the mouse, and :focus-visible still shows a ring in text fields, because the browser knows you are about to type.

Stage 4: Validation states

Validation feedback has two rules: show it only when it is useful, and never rely on color alone.

The old :invalid pseudo-class matches an empty required field the moment the page loads, so the whole form starts out red. :user-invalid fixes that: it matches only after the person has interacted with the field (changed it and left it, or tried to submit) (Pseudo-classes).

The design uses four ideas:

  • A red border and an icon-like symbol in the message, so color-blind users also get the cue.
  • A message that stays hidden until needed. With :has(), the .field wrapper can react to its input: .field:has(:user-invalid) .error { display: block } (:has() and friends).
  • :user-valid adds a green border to reassure people after they fix a mistake.
  • The messages are written in HTML and linked with aria-describedby, so assistive technology reads them. The browser's own bubble is still used on submit, so the form works even if the CSS fails.
Stage 4: validation states

Try it: click into the email field, type hello and press Tab. The field turns red and the message appears. Fix it to a real address and the border turns green and the message disappears. The submit handler only runs when the browser's validation passes, because it validates before firing submit.

:user-invalid and :user-valid are modern selectors. In a browser without them, the whole rule is ignored and the form still works with the browser's built-in messages. If you must support old browsers, add a small JavaScript class (for example touched) instead.

Ideas to take it further

  • Add :has(:checked) to highlight the chosen topic card.
  • Show a character counter with a ::after and attr().
  • Respect dark mode: define colors as custom properties and redefine them under prefers-color-scheme: dark (Dark Mode).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why use <label for> instead of a placeholder as the field's name?

    Placeholders vanish when you type and are not reliable names for assistive technology. A real label is always visible and enlarges the click target.
  2. 2. What does grid-column: 1 / -1 do?

    -1 means the last explicit grid line, so the item always spans the whole row, even when the columns change.
  3. 3. Why prefer :user-invalid over :invalid for error styling?

    :invalid matches an empty required field immediately. :user-invalid waits for interaction or a submit attempt.
  4. 4. How can a .field wrapper show its error message only when its input is invalid?

    :has() lets a parent select itself based on its descendants, which was impossible before.
  5. 5. Why does the error message include a warning symbol as well as red color?

    Never use color as the only way to convey information. A symbol and text make the state clear to everyone.

Summary

  • Use a real <label> for every field, the right type and autocomplete, and link hints and errors with aria-describedby.
  • A grid with repeat(auto-fit, minmax(min(100%, 15rem), 1fr)) gives a responsive form with no media queries; grid-column: 1 / -1 makes full-width rows.
  • Inherit the page font (font: inherit) and keep targets at least about 44px tall.
  • Replace outlines with a strong :focus-visible style, never with nothing.
  • :user-invalid and :user-valid give feedback only after interaction; :has() lets a wrapper react to its input.
  • Never use color as the only signal.

Next: Project: Newsletter Layout, where you build a newsletter and learn why email CSS plays by different rules.