Skip to content
Tutorials Center
My learning

Topics

Lesson 89 of 139 ยท Lists, Tables and Forms

Styling Forms

Style form controls that look consistent and stay accessible. Learn appearance, accent-color, focus rings, :user-invalid validation states, field-sizing and custom checkboxes.

Form controls are the trickiest elements to style. Each browser and operating system draws them a little differently, some parts (like the arrow in a <select>) were historically off-limits to CSS, and every visual choice affects whether people can actually fill the form in. The good news is that modern CSS has made this much easier: you can tint native controls with one property, show errors only after someone has interacted with a field, and grow a text area with its content.

This lesson covers the CSS side of forms. For the markup (labels, input types, validation attributes), see Forms, Input Types and Form Validation.

Start with fonts and box sizing

Two defaults surprise almost everyone. First, form controls don't inherit fonts: inputs and buttons use a small system font no matter what the page uses. Second, inputs use box-sizing: content-box, so width: 100% plus padding and border spills out of their container.

A tiny base rule fixes both:

CSS
input, select, textarea, button {
  font: inherit;
  box-sizing: border-box;
}

font: inherit pulls in the family, size, weight and line height from the parent. Inheritance and box-sizing explain the underlying rules.

A consistent text field

Text inputs, <select> and <textarea> accept the usual box properties: padding, border, border-radius, background, color. Style them together so every field on the page feels like part of one family:

CSS
.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid #94a3b8;
  border-radius: 6px;
  background: white;
  color: #0f172a;
}

Keep borders visible. A field that's only a faint background tint is hard to find, especially for people with low vision. Aim for a border with at least 3:1 contrast against the background around it.

Placeholder text is styled with ::placeholder. Make it lighter than real input but still readable, and never use it in place of a label: it disappears as soon as someone types.

Taming native looks: appearance

appearance: none turns off the platform's native drawing for a control, leaving a plain box that you style from scratch. It's commonly used on <select> to replace the arrow, and on checkboxes and radios for fully custom designs.

CSS
select {
  appearance: none;
  padding-inline-end: 2rem;
  background: white url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23334155' stroke-width='2'/%3E%3C/svg%3E")
    no-repeat right 0.7rem center;
}

Once native drawing is gone, you are responsible for everything: the arrow, the focus ring, the checked state, the disabled state and high-contrast modes. Use appearance: none only when you are ready to rebuild all of that.

The easy win: accent-color

Often you don't need to rebuild anything. accent-color tints the native checked state of checkboxes, radio buttons, range sliders and progress bars to match your brand, in one line. The browser keeps handling sizing, focus, keyboard support and contrast for you.

accent-color on native controls

Change #db2777 to any color. Browsers automatically choose a light or dark check mark so it stays visible on your accent. accent-color is supported in all modern browsers.

Focus styles that people can see

Keyboard users rely on the focus indicator to know where they are. The default focus ring is functional but may clash with your design, and on some backgrounds it's nearly invisible. Replace it, never just remove it.

CSS
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 2px;
}

For text fields, browsers show :focus-visible whenever the field has focus, whether it was reached by mouse or keyboard, because you need to see where you're typing. outline is better than border here: it doesn't change the element's size, and in Windows high-contrast mode outlines stay visible while box shadows vanish. Outlines covers outline-offset and friends.

Never write outline: none without a replacement. Removing the focus indicator makes a form unusable for anyone navigating with a keyboard.

Validation states without nagging

The browser knows when a field breaks its rules (required, type="email", minlength, pattern and so on), and exposes that state to CSS:

  • :valid and :invalid match immediately, even before the user has touched the field. Style :invalid in red and an empty required form shows errors on page load. That's unfriendly.
  • :user-valid and :user-invalid match only after the user has interacted with the field (typically after changing it and moving away, or after trying to submit). This is what you want for error styling. They are supported in all current major browsers.
  • :required and :optional match fields by whether they're required, useful for adding a marker to labels.

Pair the color change with text or an icon, because color alone isn't enough for color-blind users. A sibling message that appears only for invalid fields works well, and :has() lets the whole field group react.

Errors after interaction with :user-invalid

Load the example and nothing is red. Type "abc" into the email field and tab away: now the error appears. The :has() selector, covered in :is(), :where(), :not() and :has(), shows the message by looking at the input inside the field. For screen-reader users, connect messages to inputs with aria-describedby in the HTML as well.

Text areas that grow: field-sizing

field-sizing: content lets an input or text area size itself to its content instead of a fixed number of rows or characters. A <textarea> with it grows as you type; pair it with min-height and max-height (or min-block-size/max-block-size) to keep it reasonable.

CSS
textarea {
  field-sizing: content;
  min-height: 3lh;   /* at least three lines */
  max-height: 12lh;  /* then scroll */
}

Support: field-sizing shipped in Chromium browsers (Chrome, Edge) in 2024 and reached all major browsers in 2026 (Baseline newly available since June 2026), so older versions may lack it. Browsers that don't understand it simply ignore it and keep the fixed size, so it's a safe progressive enhancement. The lh unit equals the element's line height.

Custom checkboxes, accessibly

Sometimes accent-color isn't enough: maybe the design calls for a toggle switch. The safe pattern keeps the real <input> in the page and restyles it with appearance: none. Because it's still a real checkbox, keyboard use, form submission, label clicks and screen-reader announcements all keep working.

A toggle switch built on a real checkbox

Press Tab to reach a switch, then Space to toggle it. Some details worth copying:

  • role="switch" tells screen readers to announce "on/off" instead of "checked/not checked".
  • Pseudo-elements like ::before work on a checkbox once appearance: none is set (in all modern browsers), which is how the knob is drawn without extra markup.
  • The transition is disabled for people who ask for reduced motion. You'll learn more in Motion, Performance and prefers-reduced-motion.
  • The forced-colors block keeps the switch visible in Windows high-contrast mode, where backgrounds are replaced by system colors.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do inputs often show a smaller, different font than the rest of the page?

    Browsers give form controls their own font. font: inherit on inputs, selects, textareas and buttons fixes it.
  2. 2. You want checkboxes and radio buttons in your brand color with the least effort. What do you use?

    accent-color tints native controls while the browser keeps handling size, focus, keyboard support and contrast.
  3. 3. Why use :user-invalid instead of :invalid for error styles?

    :invalid matches immediately, so empty required fields would look like errors on page load. :user-invalid waits for interaction.
  4. 4. What is the safest way to build a custom checkbox?

    The real input keeps keyboard support, label clicks, form submission and screen-reader semantics. A hidden input or a div loses some or all of them.
  5. 5. What does field-sizing: content do on a <textarea>?

    The control sizes itself to fit its content. Use min and max heights to keep it in bounds; unsupported browsers keep the fixed size.

Summary

  • Start with font: inherit and box-sizing: border-box on all form controls.
  • Give fields visible borders, readable placeholders and real labels.
  • accent-color brands native checkboxes, radios, ranges and progress bars in one line.
  • appearance: none removes native drawing, so you must rebuild every state yourself.
  • Replace focus rings with a strong :focus-visible outline, never remove them.
  • Use :user-invalid/:user-valid for friendly validation styling, plus text, not just color.
  • field-sizing: content grows text areas with their content where supported.

Next: Styling Buttons and Links, where you will design every interactive state from hover to disabled.