Skip to content
Codewilds

Lesson 10 of 29 · Page Structure & Forms

HTML Forms: input, label, select, textarea and button

medium20 XP3 min read

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; min and max set 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.

<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. The value is 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

  • for and id that don't match. They're case-sensitive: for="Email" doesn't connect to id="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:

  1. Make the Hero name field required.
  2. Add an email field: the label Email and an <input type="email"> with id="email" and name="email", required, and the placeholder [email protected].
  3. Add an age field: the label Age and an <input type="number"> with id="age" and name="age".
  4. Add a class menu: the label Class and a <select> with id="class" and name="class", offering Mage, Knight and Ranger.
  5. Add a newsletter checkbox with id="news" and name="news", followed by the label Send me the newsletter.
  6. 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
Play the 7 free lessons

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 .

Write code ↓
HTML & CSS+20 XP

Lesson 10 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.