diffsnack
HTML Interactive Find the mistake

Form fields and labels

A <form> collects fields like <input> and <select> and sends them when you tap its submit button. Each field should have a <label> so people and screen readers know what to type.

Step by step

  1. Give every field an id and a name; the name is what gets sent.
  2. Link a <label> to its field by setting for to the field's id.
  3. Tapping a linked label usually puts the cursor in its field or ticks its checkbox.
  4. Pick the right type: email, tel, number, checkbox or radio.
  5. Give radio buttons of one group the same name.
  6. Remember that placeholder is only a hint that disappears while typing, not a label.
  7. A <button> in a form sends it unless you set another type, and disabled fields are left out when the form is sent.
The card, bug marked
1<form action="/contact" method="post">
2 <label for="name">Name</label>
3 <input type="text" id="name" name="name">
4 <label for="mail">E-mail</label>
5 <input type="email" id="email"
6 name="email">
7 <label for="phone">Phone</label>
8 <input type="tel" id="phone" name="phone"
9 placeholder="Optional">
10 <button type="submit">Send</button>
11</form>
This is how it's right
<form action="/contact" method="post">
<label for="email">E-mail</label>
<input type="email" id="email"
name="email">
<button type="submit">Send</button>
</form>

Exercises for this topic

Key rule

Give every field a visible <label>, either with a for that matches the field's id exactly or by putting the field inside the label.

Where you'll meet this

You meet forms on contact pages, sign-ups, bookings and search boxes. Most form bugs are a for that does not match an id or a name with a typo.

Play this card Next article: Form checks by the browser