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
- It asks six questions, in six different kinds of box.
- Leave the name empty and click Join the club. The browser stops you and points at the box.
- Type
helloin 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.
type | What you get |
|---|---|
text | Any text at all. The one to use when in doubt |
email | Must look like an address. Phones show the @ key |
number | Digits only, with small arrows. Takes min and max |
date | Opens a calendar |
password | Hides what is typed |
checkbox | On or off, on its own |
radio | One 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>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-visibleis a pseudo-class, like:hover.:hoveris the box you point at.:focus-visibleis 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: noneon 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.
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.
Click into a box to see this one.
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.
Make the page and the form
Start a new
index.htmlwith!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.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,forandiddo not match.Make it required
Add the word
requiredto the input, and add a<button type="submit">at the bottom. Click the button with the box empty. The browser stops you.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.
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.Style the boxes as one
Make
index.css. Write one.fieldrule and putclass="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; }Show where the cursor is
Add the
.field:focus-visiblerule. 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: nonefor 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.