Reading history

Styling HTML Forms with CSS: Inputs, Focus and Validation

7 min read 6 views

Style form inputs, selects, checkboxes and buttons with CSS: font inherit, focus-visible, accent-color, appearance none and :user-invalid validation.

Forms are where visitors sign up, pay and send messages, so they deserve as much care as any other part of a page. Browser default inputs look plain and differ from one browser to the next. A few CSS rules make them consistent, comfortable to use, and clear about mistakes. This lesson styles text fields, select boxes, checkboxes and buttons, and uses modern pseudo-classes to show validation errors only after the user has typed something.

In this lesson you will learn

  • Making inputs, selects and text areas match
  • Clear focus styles with :focus-visible
  • Colouring checkboxes, radios and sliders with accent-color
  • Replacing the native select arrow with appearance: none
  • Friendly validation with :user-invalid, and button states

You should know HTML forms and pseudo-classes.

Styling Form Controls

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Styled form controls</title>
  <style>
    body { font-family: sans-serif; margin: 12px; max-width: 360px; font-size: 14px; }
    label { display: block; font-weight: bold; margin: 10px 0 4px; }
    input[type="text"], input[type="email"], select, textarea {
      width: 100%; box-sizing: border-box;            /* padding stays inside the width */
      font: inherit; padding: 8px 10px;               /* controls do not inherit fonts by default */
      border: 1px solid #90a4ae; border-radius: 6px; background: white;
    }
    input:focus-visible, select:focus-visible, textarea:focus-visible {
      outline: 3px solid #90caf9; border-color: #1565c0;
    }
    select {                                          /* replace the native arrow */
      appearance: none;
      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='%23455a64' stroke-width='2'/%3E%3C/svg%3E") no-repeat right 10px center;
    }
    .choices { accent-color: #2e7d32; }              /* colours checkboxes, radios and ranges */
    .choices label { display: inline; font-weight: normal; margin-right: 10px; }
  </style>
</head>
<body>
  <form>
    <label for="name">Name</label>
    <input type="text" id="name" placeholder="Your full name">

    <label for="dept">Department</label>
    <select id="dept"><option>CSE</option><option>EEE</option><option>BBA</option></select>

    <label for="msg">Message</label>
    <textarea id="msg" rows="2"></textarea>

    <div class="choices">
      <p><input type="checkbox" id="news" checked> <label for="news">Send me news</label>
         <input type="radio" name="t" id="am" checked> <label for="am">Morning</label>
         <input type="radio" name="t" id="pm"> <label for="pm">Evening</label></p>
      <input type="range" aria-label="Volume" value="70">
    </div>
  </form>
</body>
</html>

Result

  • width: 100% with box-sizing: border-box makes every field exactly as wide as the form, with the padding inside that width.
  • Form controls do not inherit the page font by default. font: inherit fixes this, and a font size of at least 16px also stops iPhones from zooming in when a field gets focus.
  • :focus-visible shows a strong outline when a field is focused. Never remove focus outlines without replacing them, because keyboard users rely on them to see where they are.
  • appearance: none removes the browser’s own drawing of the select box. The arrow is then added back as a small SVG background image placed at the right.
  • accent-color sets the colour of checkboxes, radio buttons and range sliders in one line, while keeping their built-in behaviour.

Validation and Button States

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Validation and button states</title>
  <style>
    body { font-family: sans-serif; margin: 12px; max-width: 360px; font-size: 14px; }
    label { display: block; font-weight: bold; margin: 10px 0 4px; }
    input { width: 100%; box-sizing: border-box; font: inherit; padding: 8px 10px;
            border: 2px solid #b0bec5; border-radius: 6px; }
    label:has(+ input:required)::after { content: " *"; color: #c62828; }  /* mark required fields */
    input:user-invalid { border-color: #c62828; background: #ffebee; }    /* only after the user edits */
    input:user-valid   { border-color: #2e7d32; }
    .hint { font-size: 12px; color: #607d8b; margin: 3px 0 0; }
    button { margin-top: 12px; font: inherit; padding: 8px 16px; border: 0; border-radius: 6px;
             background: #1565c0; color: white; cursor: pointer; }
    button:hover  { background: #0d47a1; }
    button:active { transform: translateY(1px); }
    button:disabled { background: #b0bec5; cursor: not-allowed; }
  </style>
</head>
<body>
  <form>
    <label for="email">Email</label>
    <input type="email" id="email" required value="rina@">
    <p class="hint">Edit this field: it turns red while the address is not valid.</p>

    <label for="phone">Phone (11 digits)</label>
    <input id="phone" inputmode="numeric" pattern="[0-9]{11}" placeholder="01712345678">

    <button type="button">Register</button>
    <button type="button" disabled>Closed</button>
  </form>
</body>
</html>

Result

  • label:has(+ input:required)::after adds a red asterisk to the label of every required field. :has() selects an element based on what comes after it.
  • :invalid matches any field whose value breaks its rules, even before the user types. Showing errors on an empty form at page load is confusing.
  • :user-invalid matches only after the user has changed the field and left it, or tried to submit. Edit the email field in the demo, then click elsewhere: it turns red while the address is incomplete and green once it is valid.
  • pattern="[0-9]{11}" sets a rule with a regular expression, here exactly 11 digits. inputmode="numeric" shows a number keypad on phones.
  • Buttons need visible :hover, :active and :disabled states. A small downward move on :active makes clicks feel responsive.

Form Styling Cheat Sheet

Selector or propertyUse
font: inheritUse the page font inside controls
:focus-visibleVisible focus for keyboard users
accent-colorColour checkboxes, radios, ranges, progress bars
appearance: noneRemove native styling to build your own look
::placeholderStyle placeholder text
:required, :optionalMark required or optional fields
:user-invalid, :user-validError and success styles after interaction
:disabledDim controls that cannot be used

Common Mistakes

  1. Using placeholders instead of labels. The hint disappears as soon as the user types, and many screen readers treat it poorly.
  2. Removing focus outlines with outline: none and adding nothing in their place.
  3. Showing errors with colour alone. Add a text message too, for colour-blind users.
  4. Trusting browser validation on the server. CSS and HTML checks help users, but the server must check data again.

Practice Exercises

Exercise 1. Give placeholder text a light grey colour and italic style.

Show answer
::placeholder {
  color: #90a4ae;
  font-style: italic;
}

Exercise 2. Show an error message .error placed right after an input only while that input is user-invalid.

Show answer
.error { display: none; color: #c62828; }
input:user-invalid + .error { display: block; }

Frequently Asked Questions

Can I fully style a select dropdown list?

You can style the closed box with appearance: none, but the open list of options is still drawn by the operating system in most browsers. Newer versions of Chrome and Edge support a fully customisable select with appearance: base-select, but it is not yet available everywhere.

Do :user-invalid and :has() work in all browsers?

Yes, both work in current versions of Chrome, Edge, Firefox and Safari. In older browsers the rules are simply ignored, so the form still works without the extra styling.

Summary

  • Use font: inherit, full width and border-box for consistent fields.
  • Keep strong focus styles with :focus-visible.
  • accent-color and appearance: none restyle built-in controls.
  • Show errors with :user-invalid, after the user has interacted.

Next: HTML Entities and Special Characters.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *