Skip to main content

The HTML <form> Elements

This chapter describes all the different HTML form elements.

The <form> element can hold one or more of these form elements: <input>, <label>, <select>, <textarea>, <button>, <fieldset>, <legend>, <datalist>, <output>, <option>, and <optgroup>.


The <input> Element

The <input> element is one of the most used form elements. It can be shown in several ways, depending on its type attribute. All the different type values are covered in HTML Input Types.

Example

A single text input with a label.

Prompt
A single labeled text input for "First name" using the input element with a matching label. Keep it plain so it shows how input renders depending on its type.
Output: complete HTML page

The <label> Element

The <label> element defines a label for a form element.

It helps screen-reader users, because the screen reader reads the label out loud when the user moves to the input. It also helps people who find it hard to click very small areas, like radio buttons or checkboxes: clicking the label text toggles the matching control. Set the label's for attribute to the same value as the input's id to bind them together.


The <select> Element

The <select> element makes a drop-down list. Each choice is an <option> element. By default the first option is selected.

To pre-select an option, add the selected attribute to it. Use the size attribute to set how many options are visible at once, and the multiple attribute to let the user pick more than one.

Drop-down list

A basic drop-down list of cars.

Prompt
A drop-down list using the select element labeled "Choose a car:" with options Volvo, Saab, Fiat, and Audi. Each choice is an option element with its own value.
Output: complete HTML page

Pre-selected option

The same list with Fiat pre-selected using the selected attribute.

Prompt
A car drop-down list (Volvo, Saab, Fiat, Audi) where Fiat is pre-selected using the selected attribute on its option.
Output: complete HTML page

Visible values

Using size="3" to show three options at once.

Prompt
A car drop-down list (Volvo, Saab, Fiat, Audi) that shows three visible options at once using size="3" on the select element.
Output: complete HTML page

Multiple selections

Using the multiple attribute so the user can pick more than one car.

Prompt
A car list box (Volvo, Saab, Fiat, Audi) that lets the user pick more than one option using the multiple attribute and size="4" on the select element.
Output: complete HTML page

The <textarea> Element

The <textarea> element makes a multi-line text field.

The rows attribute sets how many lines are visible, and the cols attribute sets the visible width. You can also set the size with CSS instead.

Using rows and cols

A text area sized with rows and cols.

Prompt
A multi-line text input using the textarea element with rows="10" and cols="30", pre-filled with the text "The cat was playing in the garden."
Output: complete HTML page

Sized with CSS

A text area sized with CSS width and height instead.

Prompt
A textarea whose size is set with CSS (width 200px, height 100px) instead of rows and cols, pre-filled with "The cat was playing in the garden."
Output: complete HTML page

The <button> Element

The <button> element makes a clickable button.

Note: Always set the type attribute on a button, because different browsers may use different default types.

Example

A button that shows an alert when clicked.

Prompt
A clickable button made with the button element, type="button", that shows an alert saying "Hello World!" when clicked.
Output: complete HTML page

The <fieldset> and <legend> Elements

The <fieldset> element groups related fields in a form. The <legend> element gives that group a caption.

Example

A form whose fields are grouped in a <fieldset> with a <legend> caption.

Prompt
A form that groups related fields with a fieldset element and gives the group a caption using legend ("Personalia:"). Inside, include First name and Last name text fields pre-filled with John and Doe, plus a submit button. Form action is /action_page.php.
Output: complete HTML page

The <datalist> Element

The <datalist> element gives an <input> a list of pre-defined options. The user sees a drop-down of these options while typing.

The input's list attribute must match the id of the <datalist> to connect them.

Example

A text input with a datalist of browser suggestions.

Prompt
A form with a text input tied to a datalist of pre-defined browser options (Edge, Firefox, Chrome, Opera, Safari). The input's list attribute must match the datalist id so suggestions appear as the user types. Form action is /action_page.php.
Output: complete HTML page

The <output> Element

The <output> element shows the result of a calculation, such as one done by a script.

Example

A form that adds two numbers and shows the result in an <output> element.

Prompt
A form that adds two numbers and shows the result in an output element. Use a range input and a number input, both starting at 50, and update the output with oninput. Form action is /action_page.php and include a submit button.
Output: complete HTML page

HTML Form Elements

These are the HTML form elements and what each one does:

  • <form> — defines a form for user input.
  • <input> — defines an input control.
  • <textarea> — defines a multi-line input control (text area).
  • <label> — defines a label for an <input> element.
  • <fieldset> — groups related elements in a form.
  • <legend> — defines a caption for a <fieldset> element.
  • <select> — defines a drop-down list.
  • <optgroup> — defines a group of related options in a drop-down list.
  • <option> — defines an option in a drop-down list.
  • <button> — defines a clickable button.
  • <datalist> — gives a list of pre-defined options for input controls.
  • <output> — defines the result of a calculation.