Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 5 of 14 25 min

Build forms that collect information. Connect labels to inputs, pick the right input types for email, numbers, dates and choices, add dropdowns, text areas and buttons, and let the browser check answers with built-in validation.

By the end of today you can

  • Build a form with form, label, input and button
  • Connect each label to its input with for and id
  • Choose input types such as email, number, date, checkbox and radio
  • Use select, textarea and fieldset
  • Add validation with required, min, max, minlength and pattern

Sign-ups, searches, checkouts, contact pages: forms are how websites ask for information. Today you'll build them properly, so they're easy to fill in on any device.

Mind map · click a dot to fold, drag to pan

A first form

index.html · edit it, the preview updates
Page preview

Try Subscribe with an empty email, then with hello, then with a real-looking address. The browser checks it, and when it's valid the preview shows what would be sent.

Sketch
How a form field works. A label element with for='email' is connected to the input with id='email', so clicking the label focuses the input and screen readers announce it. The input's name='email' is the key its value is sent under. When the button is pressed, the form sends name=value pairs, such as email=ana@example.com, to the address in the form's action, using its method (get or post).
  • <form>: wraps the fields. action is where the data goes; method is how (get puts it in the URL, post sends it hidden in the request).
  • <label for="…"> + <input id="…">: always pair them. Clicking the label focuses the input, and screen readers announce the label.
  • name: the key the value is sent under. No name, not sent.
  • <button> inside a form submits it.

Inputs sit side by side because they're inline elements. CSS will tidy that up; for now, a <p> around each pair puts them on separate lines.

Input types

The type attribute changes the control, the phone keyboard and the checking:

index.html · edit it, the preview updates
Page preview
typeForBonus
text (default)Short text
emailEmail addressesChecks the format; @ keyboard on phones
passwordPasswordsHides what's typed
numberNumbersmin, max, step; number keyboard
telPhone numbersPhone keypad
urlWeb addressesChecks the format
date, timeDates and timesA date picker
checkbox, radioChoicesSee below

placeholder shows a hint inside an empty field. It's not a replacement for a label: it disappears when you start typing.

Choices: checkboxes, radios and dropdowns

index.html · edit it, the preview updates
Page preview
  • Radio buttons sharing a name are a group: pick one.
  • Checkboxes: pick any number.
  • Wrapping an input inside its <label> connects them without for/id.
  • <select> + <option>: a dropdown, good for long lists.
  • <textarea>: multi-line text.
  • <fieldset> + <legend> group related fields under a caption.
  • checked (or selected on an option) sets the starting choice.

Validation

The browser can check answers before sending:

index.html · edit it, the preview updates
Page preview
AttributeChecks
requiredNot empty
minlength, maxlengthLength of text
min, max, stepNumbers and dates
patternMatches a regular expression
type="email" / urlLooks like an email / web address

Built-in validation helps users; it doesn't protect you. Anyone can switch it off, so the server that receives the form must always check the data again.

Recap

  • <form action method> wraps fields; a <button> sends it.
  • Pair every input with a <label> (for + id, or wrap it). Give inputs a name.
  • Choose the right type: email, number, date, tel, password…
  • Radios (one choice, same name), checkboxes (many), select, textarea, fieldset.
  • required, min, max, minlength, pattern check input in the browser; always check again on the server.

That's the HTML toolkit. Tomorrow: CSS, and your pages start to look good.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

    Why connect a <label> to its input with for="email" and id="email"?

  2. 2

    What does the name attribute on an input do?

  3. 3

    Why use type="email" instead of type="text" for an email address?

  4. 4

    Three radio buttons should allow only one choice. What do they need in common?

  5. 5

    Is the browser's required/pattern validation enough to keep bad data out?

Finished day 5?

Mark complete and continue to Day 6: Hello, CSS

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going