Favorites

Form Controls - HTML Tutorial

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Text fields are just the beginning. HTML has controls for almost every kind of information, and choosing the right one makes forms faster and easier to fill in, especially on phones.

Input Types

Changing the type attribute of an input changes how it looks and behaves. Here are the most useful types:

type="text"

A single line of plain text, such as a name or a street address.

type="email"

An email address. Phones show a keyboard with an @ key, and the browser checks that the value looks like an email address.

type="tel"

A phone number. Phones show a number keypad. Because phone number formats vary around the world, the browser doesn't check the format.

type="url"

A web address. The browser checks that it looks like a full URL, such as https://www.example.com.

type="password"

A password. The characters are hidden as they're typed. Always send password forms with method="post".

type="number"

A number, with small up and down arrows. Use min, max and step to set the allowed range. It's best for quantities. For things that are made of digits but aren't really amounts, such as card numbers or postcodes, use type="text" with inputmode="numeric" instead.

type="date"

A date, with a calendar picker. Related types include time and datetime-local.

type="search"

A search box. Some browsers add a button to clear it.

type="range"

A slider for choosing a value where the exact number doesn't matter much, such as volume.

type="color"

A color picker.

type="file"

A button for choosing a file to upload. Forms that upload files need method="post" and enctype="multipart/form-data" on the form element.

Example: Different Input Types
<p>
  <label for="guests">Number of guests</label>
  <input type="number" id="guests" name="guests" min="1" max="12" value="2">
</p>

<p>
  <label for="booking-date">Date</label>
  <input type="date" id="booking-date" name="booking-date">
</p>

<p>
  <label for="phone">Phone number</label>
  <input type="tel" id="phone" name="phone" autocomplete="tel">
</p>

The value attribute sets a starting value for a field. Here, the number of guests starts at 2.

Checkboxes

Checkboxes let visitors switch options on or off. Each one is independent, so visitors can tick as many as they like. Put the label after the checkbox, which is the usual convention:

Example: Checkboxes
<fieldset>
  <legend>Dietary requirements</legend>

  <input type="checkbox" id="vegetarian" name="diet" value="vegetarian">
  <label for="vegetarian">Vegetarian</label>

  <input type="checkbox" id="gluten-free" name="diet" value="gluten-free">
  <label for="gluten-free">Gluten free</label>

  <input type="checkbox" id="nut-free" name="diet" value="nut-free">
  <label for="nut-free">Nut free</label>
</fieldset>

Each checkbox has a value, which is what gets sent when it's ticked. Unticked checkboxes aren't sent at all. Add the checked attribute to tick a box by default.

Radio Buttons

Radio buttons let visitors choose one option from a group. All the buttons in a group share the same name, which is what ties them together:

Example: Radio Buttons
<fieldset>
  <legend>Where would you like to sit?</legend>

  <input type="radio" id="seat-inside" name="seating" value="inside" checked>
  <label for="seat-inside">Inside</label>

  <input type="radio" id="seat-outside" name="seating" value="outside">
  <label for="seat-outside">Outside</label>

  <input type="radio" id="seat-any" name="seating" value="any">
  <label for="seat-any">No preference</label>
</fieldset>

Selecting one option automatically deselects the others. Always wrap a group of radio buttons (or related checkboxes) in a fieldset with a legend. The legend is the question, and screen readers announce it together with each option.

Drop-Down Menus

The select element creates a drop-down menu, with each choice in an option element. It's a good choice when there are many options and space is tight:

Example: A Drop-Down Menu
<label for="time">Arrival time</label>
<select id="time" name="time">
  <option value="">Choose a time</option>
  <optgroup label="Morning">
    <option value="09:00">9:00am</option>
    <option value="10:00">10:00am</option>
    <option value="11:00">11:00am</option>
  </optgroup>
  <optgroup label="Afternoon">
    <option value="12:00">12:00pm</option>
    <option value="13:00">1:00pm</option>
  </optgroup>
</select>
  • The value of the chosen option is what gets sent. The text between the tags is what the visitor sees.
  • optgroup groups options under a heading, which helps in long lists.
  • Add selected to an option to choose it by default.

If there are only a handful of options, radio buttons are often easier to use, because visitors can see every choice without opening a menu.

Text Areas

For longer text, such as a message or a comment, use textarea. The rows attribute sets its starting height in lines of text:

<label for="requests">Special requests</label>
<textarea id="requests" name="requests" rows="4"></textarea>

Any text between the opening and closing tags appears inside the box as its starting value, so make sure there are no stray spaces or line breaks between them.

Buttons

The button element has three types:

  • type="submit" sends the form. This is the default, so a button inside a form submits it unless you say otherwise.
  • type="reset" clears every field back to its starting value. Visitors rarely want this and can press it by accident, so it's usually best left out.
  • type="button" does nothing on its own. It's used with JavaScript.

You may also see <input type="submit" value="Send"> in older code. It works the same way, but button is more flexible, because it can contain other elements, such as an icon.

Putting It Together

Example: A Table Booking Form
<form action="" method="post">
  <h2>Book a Table</h2>

  <p>
    <label for="b-name">Name</label>
    <input type="text" id="b-name" name="name" autocomplete="name">
  </p>

  <p>
    <label for="b-email">Email address</label>
    <input type="email" id="b-email" name="email" autocomplete="email">
  </p>

  <p>
    <label for="b-date">Date</label>
    <input type="date" id="b-date" name="date">
  </p>

  <p>
    <label for="b-guests">Number of guests</label>
    <input type="number" id="b-guests" name="guests" min="1" max="12" value="2">
  </p>

  <fieldset>
    <legend>Where would you like to sit?</legend>
    <input type="radio" id="b-inside" name="seating" value="inside" checked>
    <label for="b-inside">Inside</label>
    <input type="radio" id="b-outside" name="seating" value="outside">
    <label for="b-outside">Outside</label>
  </fieldset>

  <p>
    <label for="b-requests">Special requests</label>
    <textarea id="b-requests" name="requests" rows="4"></textarea>
  </p>

  <button type="submit">Request booking</button>
</form>

If you use the CSS from Form Basics, note that it sets all labels to display: block. For the radio buttons, you'll want their labels to stay on the same line, so add a rule such as fieldset label { display: inline; }.

In the next section, you'll make sure visitors fill in this form correctly before it's sent.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee