Lesson 10 of 14 · 10 min

Forms

Forms let people send you information: a sign-up, an order, a search.

<form>
  <label for="name">Your name</label>
  <input type="text" id="name" name="name" required>

  <label for="phone">Phone</label>
  <input type="tel" id="phone" name="phone">

  <button type="submit">Send</button>
</form>

Labels matter

Every field needs a <label>. Its for attribute must match the field's id. Then:

  • screen readers read the label when the field is focused,
  • tapping the label focuses the field, a bigger target on a phone.

A placeholder is not a label: it disappears as soon as you type.

The right input type

The type changes the keyboard phones show and adds free checking:

type Gives
email an @ keyboard; checks for an email shape
tel a number pad for phone numbers
number numbers only; min and max allowed
date a date picker
password hidden characters
checkbox, radio ticks and single choices

Built-in validation

required, min, max, minlength and pattern make the browser check input before the form is sent, with no code at all.

Choosing from a list

<label for="size">Size</label>
<select id="size" name="size">
  <option>Small</option>
  <option>Large</option>
</select>

Your task

Build an order form with:

  1. A required email field.
  2. A <select> for the cake size with at least 3 options.
  3. A label for every field, connected with for and id.
  4. A submit button.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Order a cake</title>
</head>
<body>
  <h1>Order a cake</h1>
  <form>
    <input type="text" placeholder="Your name">
    <!-- add the email field, the size select and a submit button -->
  </form>
</body>
</html>

Your page

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…