Forms: Input & Label — HTML

<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…

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>

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:

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

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.

What you will learn