HTML · Unit 3: Tables, forms and a profile card · Lesson 2 of 5
Forms: labels and inputs
Forms collect information.
Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.
The idea
Forms collect information. <input> makes a box to type in. It is an empty element, like <img>.
Every input needs a <label>. Give the input an id and point the label at it with for. Clicking the label then puts the cursor in the box, and screen readers read the label aloud.
The name attribute is what each value is called when the form is sent. The preview here never sends forms anywhere.
Worked example
HTML
<form>
<label for="email">Email</label>
<input id="email" name="email" type="email">
<button type="submit">Sign up</button>
</form>
Key terms
- Label
- Text tied to a form control with for and id, so clicking it focuses the box and screen readers read it.
- Input
- A form control. Its type, like text, email, number or checkbox, decides what it accepts.
Practise it in Code Academy
It has 4 exercises (multiple choice, fill in the blank, fix the broken code and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.
Every lesson is free: no ads, no lives and no paywall.