Lesson 10 of 29 · Page Structure & Forms
HTML Forms: input, label, select, textarea and button
Forms are how visitors talk back to a website: sign-ups, logins, search boxes, checkouts. HTML gives you a ready-made field for every kind of answer — and when every field has a proper label, everyone can fill them in, including people who use a screen reader or a phone.
The form and its fields
A <form> wraps the fields that belong together. Its action says where the answers are sent, and method="post" how. Inside, the most common field is <input>, an empty element whose type decides what kind of field it is:
<form action="/signup" method="post">
<label for="hero">Hero name</label>
<input type="text" id="hero" name="hero">
<button type="submit">Join</button>
</form>
The name is the key the value is sent under, like hero=Rowan. A field without a name shows up on the page, but its value is never sent.
Labels and fields are inline elements, so they all line up in one row. Wrap each label and its field in a <p> to give every field a line of its own.
Labels
A <label> is the visible name of a field. Connect the two by giving the label a for attribute with the same value as the field's id. That connection matters:
- Clicking the label puts the cursor in the field, or ticks the checkbox — a much bigger target on a phone.
- A screen reader reads the label aloud when the field gets focus. A field without a label is just "edit text, blank".
A placeholder is not a label. It's a light grey example inside the field that disappears as soon as you start typing.
Input types
The type attribute changes how a field behaves:
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="[email protected]" required>
<label for="age">Age</label>
<input type="number" id="age" name="age" min="12" max="120">
<input type="checkbox" id="news" name="news">
<label for="news">Send me the newsletter</label>
text— a single line of text. It's the default.email— checks that the value looks like an email address; phones show a keyboard with an@key.number— accepts only numbers and shows arrow buttons;minandmaxset the limits.checkbox— a yes/no tick box. Its label usually goes after the box.
The required attribute makes the browser refuse to send the form while that field is empty — or, for a checkbox, unticked — and tells the visitor what's missing.
Menus, long text and buttons
<label for="class">Class</label>
<select id="class" name="class">
<option value="mage">Mage</option>
<option value="knight">Knight</option>
</select>
<label for="bio">About you</label>
<textarea id="bio" name="bio" rows="4"></textarea>
<select>makes a drop-down menu with one<option>per choice. Thevalueis what gets sent; the text is what people see.<textarea>is a text box for several lines. Unlike<input>, it has a closing tag.<button type="submit">sends the form.
Common mistakes
forandidthat don't match. They're case-sensitive:for="Email"doesn't connect toid="email".- A placeholder instead of a label. Once someone starts typing, the hint is gone.
- Forgetting
name. The field works on the page, but its value never reaches the server. <textarea/>or<input></input>. A textarea needs its closing tag; an input never has one.
Assignment
The guild's sign-up form already has a Hero name field. Finish it — every label uses for with its field's id:
- Make the
Hero namefieldrequired. - Add an email field: the label
Emailand an<input type="email">withid="email"andname="email",required, and the placeholder[email protected]. - Add an age field: the label
Ageand an<input type="number">withid="age"andname="age". - Add a class menu: the label
Classand a<select>withid="class"andname="class", offeringMage,KnightandRanger. - Add a newsletter checkbox with
id="news"andname="news", followed by the labelSend me the newsletter. - End the form with a submit button:
Join the guild.
Try it out in the Preview: click a label and watch its field light up.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .