Skip to main content
← HTML and CSS

Lesson 7 of 7

Sign-up Form

The boxes people type into. Every page so far only showed things. This one asks for them.

What you will learn

  • <form>
  • <label> and for
  • <input> types
  • required
  • placeholder
  • <select> and <option>
  • Radio buttons
  • <textarea>
  • :focus-visible

Before you start

Planet Grid. You can build and style a page. This lesson adds the tags that collect an answer instead of showing one.

What this page does

  1. It asks six questions, in six different kinds of box.
  2. Leave the name empty and click Join the club. The browser stops you and points at the box.
  3. Type hello in the email box and try again. It stops you again.

Every box needs a label

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

The label's for must match the input's id. Those two words are what tie them together. Get them matching and clicking the words puts the cursor in the box. Try it on the page.

There is a second way. Put the input inside the label and you need no id at all. The checkbox on the page is written that way. Both are correct. Use whichever keeps the code readable.

<label>
    <input type="checkbox" name="laptop">
    I can bring a laptop
</label>

The kinds of box

One type word changes what an <input> does, what a phone keyboard shows, and what the browser checks.

typeWhat you get
textAny text at all. The one to use when in doubt
emailMust look like an address. Phones show the @ key
numberDigits only, with small arrows. Takes min and max
dateOpens a calendar
passwordHides what is typed
checkboxOn or off, on its own
radioOne out of several

Two tags are not <input> at all. <select> is a drop-down, and each choice inside it is one <option>. <textarea> is a box for several lines, and it is the only one of these with a closing tag.

<select id="level" name="level">
    <option value="none">None yet</option>
    <option value="some" selected>A little</option>
    <option value="lots">Quite a lot</option>
</select>

Radio buttons share a name

<input type="radio" name="day" value="tuesday">
<input type="radio" name="day" value="thursday" checked>
<input type="radio" name="day" value="either">

All three say name="day". That shared name is what makes them one group, so choosing one clears the others. Give them different names and all three can be on at once, which is not what a radio button is for.

value is what gets sent. The words a person reads live in the label beside the box. The two are allowed to be different, and usually should be: value="thursday" is easy for code to work with, and Thursday is what the person needs to read.

<fieldset> draws a box around a set of related questions and <legend> names the group. A label names one box. A legend names the whole question.

Showing where the cursor is

Press Tab through the form. A ring follows you from box to box. That ring is the only way somebody using a keyboard can tell where they are.

.field:focus-visible {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px #dbeafe;
}
  • :focus-visible is a pseudo-class, like :hover. :hover is the box you point at. :focus-visible is the box you are typing in.
  • The border also gets thicker and darker, not just bluer. Somebody who cannot tell blue from grey still sees the change.
  • outline: none on its own is one of the most common accessibility mistakes on the web. It is only safe here because the next two lines draw a replacement.

The look of a box

Click into a box before you move these. Set the focus ring to 0 and Tab through the form to feel what it costs.

3px

Click into a box first. Then set this to 0 and press Tab through the form. That is what the form feels like with no focus ring.

0.5rem
#2563eb

Click into a box to see this one.

#2563eb

Build it step by step

One box at a time, and reload after each one. A form is long but it is the same three lines over and over.

  1. Make the page and the form

    Start a new index.html with ! and Tab. Inside <body>, add a <form> with an <h1> in it. A form is just a box, so it takes any rule a div would.

  2. Add one labelled box

    Add a <label for="name"> and an <input type="text" id="name" name="name">. Reload. Click the words and check the cursor lands in the box. If it does not, for and id do not match.

  3. Make it required

    Add the word required to the input, and add a <button type="submit"> at the bottom. Click the button with the box empty. The browser stops you.

  4. Add the other kinds

    Add an email input, a number input, a select with three options, and a textarea. Give every one a label. Reload after each.

  5. Add the radio group

    Add three radio inputs that all share name="day". Wrap them in a <fieldset> with a <legend>. Click each one and check that choosing one clears the others.

  6. Style the boxes as one

    Make index.css. Write one .field rule and put class="field" on every input, the select and the textarea. They all look the same from one rule.

    .field {
        box-sizing: border-box;
        width: 100%;
        padding: 0.5rem 0.6rem;
        font: inherit;
        border: 2px solid #cfd6e0;
        border-radius: 0.5rem;
    }
  7. Show where the cursor is

    Add the .field:focus-visible rule. Tab through the form and watch the ring move.

Try these changes

Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.

Check yourself

Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.

Loading your answers

Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.

How to teach it

  • Open the finished page and click submit on an empty form before you show any code. The browser's own message doing the work for free is the hook for the whole lesson.
  • Break for="name" in front of them and click the label. Nothing happens. Fix it and click again. That five-second demo teaches labels better than the paragraph about screen readers, and the paragraph then lands.
  • Have them add radio buttons with three DIFFERENT names first, on purpose. All three stay on. Then make the names match. The shared name is the idea, and they will remember finding it.
  • Set the focus ring slider to 0 and make them Tab through the form. Somebody always says they cannot tell where they are. That is the point, and it is the argument against outline: none for the rest of their career.
  • Do not go near sending the form anywhere. Say plainly that it collects and does not send, and that the next course is what makes it do something. The gap is deliberate.