Aral Code

Going Further Lesson 32 of 33

Styling Forms with CSS

Make forms clean and easy to use — style inputs, labels, focus states, buttons, checkboxes and select menus, and lay out a mobile-friendly registration form.

Forms are where visitors actually do something: register, send a message, order, answer a quiz. A badly styled form, with tiny boxes, unclear labels and no feedback, makes people give up. In this lesson you'll turn a plain HTML form into a clean, friendly one.

The plain form

Example A form with no CSS
<form>
  <label for="name">Full name</label>
  <input id="name" type="text" placeholder="Juan Dela Cruz">
  <label for="grade">Grade level</label>
  <select id="grade"><option>Grade 7</option><option>Grade 8</option><option>Grade 9</option><option>Grade 10</option></select>
  <label for="msg">Why do you want to join the coding club?</label>
  <textarea id="msg" rows="3"></textarea>
  <button type="submit">Sign up</button>
</form>

Everything is crammed onto a few lines, the boxes are small, and the fonts don't match the page. Let's fix it.

Step 1: stack labels and fields

Make labels and inputs display: block so each sits on its own line, and make inputs full width:

label { display: block; margin: 14px 0 6px; font-weight: bold; }
input, select, textarea { display: block; width: 100%; }

Step 2: comfortable inputs

Form controls don't inherit the page's font by default, which is why they often look different. Fix that, and add padding, borders and rounded corners:

input, select, textarea {
  font: inherit;
  padding: 12px;
  border: 1.5px solid #c9ccc8;
  border-radius: 10px;
  background: white;
}

Step 3: a clear focus state

When someone taps a field, it should be obvious which one is active:

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: #0f5c4d;
  box-shadow: 0 0 0 4px rgba(15, 92, 77, 0.18);
}

The finished form

Example A clean, mobile-friendly form
<style>
  * { box-sizing: border-box; }
  body { font: 16px/1.5 Arial, sans-serif; color: #1d2321; background: #f3f6f4; margin: 0; padding: 16px; }
  .form {
    max-width: 440px;
    margin: 0 auto;
    background: white;
    padding: 22px;
    border-radius: 16px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.07);
  }
  .form h2 { margin: 0 0 4px; color: #0f5c4d; }
  .form .hint { margin: 0 0 8px; color: #5b6662; font-size: 14px; }
  label { display: block; margin: 14px 0 6px; font-weight: bold; }
  input, select, textarea {
    display: block;
    width: 100%;
    font: inherit;
    padding: 12px;
    border: 1.5px solid #c9ccc8;
    border-radius: 10px;
    background: white;
    transition: border-color 0.2s, box-shadow 0.2s;
  }
  input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: #0f5c4d;
    box-shadow: 0 0 0 4px rgba(15, 92, 77, 0.18);
  }
  textarea { resize: vertical; }
  .check { display: flex; gap: 10px; align-items: flex-start; font-weight: normal; }
  .check input { width: 20px; height: 20px; margin-top: 2px; accent-color: #0f5c4d; flex: none; }
  button {
    width: 100%;
    margin-top: 18px;
    padding: 14px;
    font: bold 17px Arial, sans-serif;
    color: white;
    background: #0f5c4d;
    border: 0;
    border-radius: 12px;
    cursor: pointer;
  }
  button:hover { background: #127a65; }
</style>
<form class="form">
  <h2>Join the Coding Club 💻</h2>
  <p class="hint">Meetings every Friday, 3:00 PM, Computer Lab 2.</p>

  <label for="name">Full name</label>
  <input id="name" type="text" placeholder="Juan Dela Cruz" required>

  <label for="grade">Grade level</label>
  <select id="grade">
    <option>Grade 7</option><option>Grade 8</option><option>Grade 9</option><option>Grade 10</option>
  </select>

  <label for="msg">Why do you want to join?</label>
  <textarea id="msg" rows="3" placeholder="Gusto kong matutong gumawa ng website…"></textarea>

  <label class="check"><input type="checkbox" required> My parent or guardian knows I'm joining.</label>

  <button type="submit">Sign up</button>
</form>

Checkboxes and radio buttons

Checkboxes and radio buttons are hard to fully restyle, but one property changes their color easily: accent-color.

input[type="checkbox"], input[type="radio"] {
  accent-color: #0f5c4d;
  width: 20px;
  height: 20px;
}

Also wrap the input and its text inside the <label>, as in the example. Then tapping the text also checks the box, which is much easier on a phone.

Select menus

<select> menus can be styled with padding, border and font like inputs. The dropdown list that opens is drawn by the phone or computer and can't be styled much. That's normal, and it helps people because it looks familiar on their device.

Showing errors

Use the :invalid pseudo-class (from the Pseudo-classes lesson) together with :user-invalid or :not(:placeholder-shown), so fields turn red only after the user has interacted with them, and always show a short text message, not only a color:

Example An error message under a field
<style>
  body { font: 16px Arial, sans-serif; }
  input { font: inherit; padding: 10px; border: 1.5px solid #c9ccc8; border-radius: 8px; width: 260px; }
  .error-msg { display: none; color: #b4372b; font-size: 14px; margin-top: 6px; }
  input:not(:placeholder-shown):invalid { border-color: #b4372b; }
  input:not(:placeholder-shown):invalid + .error-msg { display: block; }
</style>
<label for="cp">Mobile number</label><br>
<input id="cp" type="tel" pattern="09[0-9]{9}" placeholder="09XXXXXXXXX">
<p class="error-msg">Please enter 11 digits starting with 09.</p>

Laying out fields side by side

On wider screens, short related fields (like first and last name) can sit side by side using Grid, and stack on phones:

.row { display: grid; gap: 12px; }
@media (min-width: 560px) {
  .row { grid-template-columns: 1fr 1fr; }
}

Form design checklist

  • Every input has a visible <label>. Placeholders are hints, not labels, because they disappear when you type.
  • Fields are big enough to tap (at least 44px tall).
  • The focused field is clearly highlighted.
  • Errors are explained in words, near the field.
  • The submit button is big, clear, and says what it does ("Sign up", "Send message"), not just "Submit".

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Why add font: inherit; to inputs and buttons?
2. Which property easily changes the color of checkboxes and radio buttons?
3. Why shouldn't a placeholder replace a label?
4. What is the minimum input font size to avoid auto-zoom on iPhones?

By Sir Reyel · Last updated 2026-12-04