Every time you create an account, log in, or type your address to shop online, you are filling in a form. A form is the part of a page where visitors fill in data such as a name, an email and a password. It looks like a paper form: there is the word "Name:" and an empty box next to it. In HTML that word is a <label>, the box is an <input>, and the whole form is wrapped in <form>.
One field: a label and an input
Here is a form with one field:
<form>
<label for="name">Name</label>
<input type="text" id="name" name="name">
</form>
<form>wraps every field. All the fields go inside it.<input>is the box. This tag has no closing tag, so don't write</input>.id="name"is this box's unique name on the page.<label for="name">is the text next to the box. Theforvalue must match the box'sidexactly, capital letters included. This match is what connects the label to the box. Because they are connected, a click on the word "Name" moves the cursor into the box. A screen reader (a program that reads pages aloud for blind users) also says "Name" when the box is selected.name="name"is the name given to what the visitor types in this box when the form is sent to the server (the computer that receives the data). From that name, the server knows which field it has received.
Remember the pair: id is written in the input, for is written in the label, and their values are the same. name is for sending the data.
type decides what kind of box it is
Here are two fields with different type values:
<label for="email">Email</label>
<input type="email" id="email" name="email">
<label for="password">Password</label>
<input type="password" id="password" name="password">
The type values you will use most:
text: plain text. It is also what you get whentypeis not written.email: an email address. Before the form is sent, the browser checks that the text has an@.password: what you type shows as dots.number: for amounts, such as an age or a number of items, and phones show a number keyboard. It also accepts-5,2.5and1e3(a short way to write 1000).date: a date, picked from a small calendar.tel: a phone number.
Long text uses <textarea>
An <input> is a single line. For long answers like a bio or a message, use <textarea>. Unlike <input>, a <textarea> does have a closing tag:
<label for="bio">Bio</label>
<textarea id="bio" name="bio"></textarea>
The pairing stays the same: for on the label, id on the textarea.
⚠️ Fields that don't connect, don't send, or are the wrong kind
forandidspelled differently.for="name"andid="Name"don't connect, because capital letters count.- Two boxes with the same
id. Anidmust be unique on the page. If two boxes have the sameid, the label can connect to the wrong box. - A
placeholderinstead of a label. A placeholder (example text inside the box) disappears as soon as someone starts typing, so keep the<label>. - A missing
name. The box appears, but what was typed in it is not sent. type="number"for a phone number. A number written with dashes, such as020-7946-0958, makes the field count as empty, with no warning. The arrow keys or the mouse wheel can also change the number, and the zero at the front disappears. Usetype="tel". For number codes such as a postcode, usetype="text" inputmode="numeric": a plain text box, but phones show a number keyboard.- An opening tag without its
>. With<input type="text"and no>, the browser reads the next line as part of that tag.
TL;DR: <form> wraps every field. Each field is the text <label for="x"> plus the box <input id="x">, with matching for and id values. <input> has no closing tag, and its type decides the kind of box (text, email, password, number, date, tel). Long text uses <textarea>, which does have a closing tag. name is the name given to a field's content when the form is sent.
⚡ A label and its box
Write:
<form>
<label for="name">Name</label>
<input type="text" id="name" name="name">
</form>
Guess first: what happens when you click the word "Name"?
The cursor moves into the <input> box. That's because the label's for matches the input's id exactly.
Meaning: <form> wraps every field. <label> is the text, <input> is the box. The for in the label and the id in the input must match exactly, capital letters included. <input> has no closing tag. name is the name given to the field's content when the form is sent.
⚡ type decides the kind of box
Write:
<label for="email">Email</label>
<input type="email" id="email" name="email">
<label for="password">Password</label>
<input type="password" id="password" name="password">
Guess first: should a phone number use type="number"?
No. A number written with dashes, such as 020-7946-0958, makes the field count as empty. Use type="tel".
Meaning: text is plain text (also used when type is not written), email is checked for an @, password shows dots. number is for amounts (an age, a number of items), date is a date picker, tel is a phone number.
⚡ Long text
Write:
<label for="bio">Bio</label>
<textarea id="bio" name="bio"></textarea>
Meaning: An <input> is a single line. For a bio or a message, use <textarea>, which does have a closing tag.