TExcelTAD
Home › CSS › CSS Forms

CSS Forms

Browsers give form controls a plain default look that varies between operating systems. With CSS you can make inputs, selects, textareas and buttons consistent, readable and pleasant to use — while keeping them accessible.

Styling text inputs

The main properties: width, padding, border, border-radius, font and a clear :focus style. Remember that inputs don't inherit fonts by default, so font: inherit is a useful reset.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 10px; }
  *, *::before, *::after { box-sizing: border-box; }
  label { display: block; font-weight: 600; margin: 10px 0 4px; }
  input, textarea {
    font: inherit;
    width: 100%;
    max-width: 320px;
    padding: 10px 12px;
    border: 2px solid #cfd8dc;
    border-radius: 8px;
    background: #fafafa;
    transition: border-color .2s, box-shadow .2s;
  }
  input:focus, textarea:focus {
    outline: none;
    border-color: #3f51b5;
    background: white;
    box-shadow: 0 0 0 4px rgb(63 81 181 / .2);
  }
  textarea { resize: vertical; min-height: 60px; }
</style>
</head>
<body>
  <label for="n">Name</label>
  <input id="n" placeholder="Ada Lovelace">
  <label for="m">Message</label>
  <textarea id="m" placeholder="Say hello…"></textarea>
</body>
</html>

Tip: If you replace the default outline with outline: none, you must provide another clear focus indicator — here a coloured border plus a soft box-shadow ring.

Useful form properties

Property Purpose
font: inherit Makes controls use the page font
box-sizing: border-box width: 100% includes padding and border
resize none, vertical, horizontal, both for textareas
accent-color Recolours checkboxes, radios, ranges and progress bars
caret-color Colour of the text cursor
appearance: none Removes native styling so you can draw your own
::placeholder Styles placeholder text

Checkboxes, radios and accent-color

accent-color is the easiest way to brand native controls without rebuilding them.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 10px; }
  .brand { accent-color: #e91e63; }
  .green { accent-color: #2e7d32; }
  label { display: block; margin: 6px 0; }
  input[type=checkbox], input[type=radio] { width: 18px; height: 18px; vertical-align: -3px; }
  ::placeholder { color: #b0bec5; font-style: italic; }
</style>
</head>
<body>
  <div class="brand">
    <label><input type="checkbox" checked> Pink checkbox</label>
    <label><input type="radio" name="r" checked> Pink radio</label>
    <input type="range" value="70">
  </div>
  <div class="green">
    <label><input type="checkbox" checked> Green checkbox</label>
    <progress value="60" max="100"></progress>
  </div>
</body>
</html>

Custom select and buttons

appearance: none strips the native arrow from a <select>; you can then add your own with a background image.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 10px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
  select {
    appearance: none;
    font: inherit; padding: 10px 40px 10px 12px;
    border: 2px solid #90a4ae; border-radius: 8px;
    background: white url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23455a64' stroke-width='2' fill='none'/%3E%3C/svg%3E") no-repeat right 12px center / 12px;
  }
  button {
    font: inherit; font-weight: 600; padding: 10px 20px;
    border: none; border-radius: 8px; background: #ff7043; color: white; cursor: pointer;
  }
  button:hover { background: #f4511e; }
  button:focus-visible { outline: 3px solid #ffab91; outline-offset: 2px; }
</style>
</head>
<body>
  <select><option>Small</option><option>Medium</option><option>Large</option></select>
  <button type="button">Add to cart</button>
</body>
</html>

A complete form with validation styles

Combine labels, a layout and the :user-invalid / :valid pseudo-classes for feedback that appears only after the user has interacted with a field.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  *, *::before, *::after { box-sizing: border-box; }
  body { font-family: system-ui, sans-serif; background: #eceff1; padding: 10px; }
  form { max-width: 340px; margin: auto; background: white; padding: 16px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,.1); display: grid; gap: 8px; }
  input { font: inherit; padding: 9px; border: 2px solid #cfd8dc; border-radius: 6px; width: 100%; }
  input:focus { outline: 2px solid #5c6bc0; outline-offset: 1px; }
  input:user-invalid { border-color: #e53935; background: #ffebee; }
  input:valid:not(:placeholder-shown) { border-color: #43a047; }
  button { font: inherit; padding: 10px; border: none; border-radius: 6px; background: #3949ab; color: white; }
</style>
</head>
<body>
  <form onsubmit="return false">
    <label for="e">Email</label>
    <input id="e" type="email" placeholder="you@site.test" required>
    <label for="p">PIN (4 digits)</label>
    <input id="p" pattern="[0-9]{4}" placeholder="1234" required>
    <button>Sign in</button>
  </form>
</body>
</html>

Always pair each input with a <label> (using for/id) — placeholders disappear when typing and are not a replacement for labels.

Test yourself

Which property recolours native checkboxes and radio buttons in one line?

  1. color
  2. accent-color
  3. fill
  4. caret-color

Why add box-sizing: border-box when giving inputs width: 100%?

  1. To round the corners
  2. So padding and border don't push the input wider than its container
  3. To enable focus
  4. To remove the arrow

What does appearance: none do to a select?

  1. Hides it
  2. Removes the native styling such as the arrow so you can style it yourself
  3. Disables it
  4. Makes it multi-select