Forms are how the web listens: sign-ups, searches, contact pages, checkouts. The HTML side is very approachable, and getting one detail right — pairing every input with a label — instantly makes your forms more usable and accessible than most you'll meet online.
1Inputs and their labels
A form lives in a <form> element. Inside it, the workhorse is <input>, a void element whose type attribute changes what it collects:
| type | Gives you |
|---|---|
text | A single line of text. |
email | A text box that checks for an @ and shows an email keyboard on phones. |
password | Hidden, dotted-out text. |
number | A numeric field with up/down arrows. |
checkbox / radio | Tick boxes / pick-one options. |
Always label your inputs
Every input needs a <label>. Link them by giving the input an id and the label a matching for:
<label for="email">Your email</label> <input type="email" id="email">
- Inputs live in a
<form>; thetypeattribute decides what each<input>collects. - Pair every input with a
<label>using matchingforandid. - A linked label improves accessibility and gives a bigger tap target on mobile.
2More controls and basic validation
Beyond <input>, you'll reach for:
<textarea>— a multi-line text box (e.g. a message).<select>with<option>s — a dropdown menu.<button>— typicallytype="submit"to send the form.
Built-in validation
The browser can check fields for you before sending, with simple attributes:
required— the field must be filled in.minlength/maxlength— limit text length.type="email"— must look like an email.
- Use
<textarea>for long text,<select>for dropdowns,<button type="submit">to send. - Attributes like
required,minlengthandtype="email"give free browser validation. - Browser validation is convenience only — always validate on the server too.
★ Practical: a contact form
In any playground, build a small contact form with:
- A labelled
textinput for a name, markedrequired. - A labelled
emailinput. - A labelled
<textarea>for a message. - A
<button type="submit">, and confirm every field has a matching label.
Ready to test yourself?
Take the short quiz. Score 60% or more to mark this lesson complete.
Start the quiz →💡 Log in to save your progress and earn the certificate.