This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.

Smartphone icons created by Freepik - Flaticon

7.3 HTML Form Elements

This chapter describes all the different HTML form elements.

The HTML <form> Elements

The HTML <form> element can contain one or more of the following form elements:

The <input> Element

  • One of the most used form element is the <input> element.
  • The <input> element can be displayed in several ways, depending on the type attribute.
Example 1: input element
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">

All the different values of the type attribute are covered in the next chapter: HTML Input Types.

return to top of page

The <label> Element

  • The <label> element defines a label for several form elements.
  • The <label> element is useful for screen-reader users, because the screen-reader will read out loud the label when the user focus on the input element.
  • The <label> element also help users who have difficulty clicking on very small regions (such as radio buttons or checkboxes) - because when the user clicks the text within the <label> element, it toggles the radio button/checkbox.
  • The for attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.

Definition and Usage

The <label> tag defines a label for several elements:

  • <input type="checkbox">
  • <input type="color">
  • <input type="date">
  • <input type="datetime-local">
  • <input type="email">
  • <input type="file">
  • <input type="month">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="range">
  • <input type="search">
  • <input type="tel">
  • <input type="text">
  • <input type="time">
  • <input type="url">
  • <input type="week">
  • <meter>
  • <progress>
  • <select>
  • <textarea>

Proper use of labels with the elements above will benefit:

  • Screen reader users (will read out loud the label, when the user is focused on the element)
  • Users who have difficulty clicking on very small regions (such as checkboxes) - because when a user clicks the text within the <label> element, it toggles the input (this increases the hit area).

See HTML Reference ‐ <label> Tag on W3Schools.com for details

return to top of page

The <select> Element

The <select> element defines a drop-down list:

Example 2: HTML select element
 <label for="cars">Choose a car:</label>
<select id="cars" name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
  • The <option> elements defines an option that can be selected.
  • By default, the first item in the drop-down list is selected.
  • To define a pre-selected option, add the selected attribute to the option:
Example 3: HTML selected option
 <option value="fiat" selected>Fiat</option> 

Visible Values:

Use the size attribute to specify the number of visible values:

Example 4: HTML size attribute
<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="3">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>

Allow Multiple Selections:

Use the multiple attribute to allow the user to select more than one value:

Example 5: HTML select more that one
<label for="cars">Choose a car:</label>
<select id="cars" name="cars" size="4" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>

return to top of page

The <textarea> Elementī¸

The <textarea> element defines a multi-line input field (a text area):

Example 6: HTML textarea element
 <textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
<textarea> 
  • The rows attribute specifies the visible number of lines in a text area.
  • The cols attribute specifies the visible width of a text area.

This is how the HTML code above will be displayed in a browser:

You can also define the size of the text area by using CSS:

Example 7: HTML style textarea
 <textarea name="message" style="width:200px; height:100px;">
The cat was playing in the garden.
</textarea> 

return to top of page

The <button> Element

The <button> element defines a clickable button:

Example 8: HTML button element
 <button type="button" onclick="alert('Hello World!')">Click Me!</button> 

This is how the HTML code above will be displayed in a browser:

Note: Always specify the type attribute for the button element. Different browsers may use different default types for the button element.

return to top of page

The <fieldset> and <legend> Elements

  • The <fieldset> element is used to group related data in a form.
  • The <legend> element defines a caption for the <fieldset> element.
Example 9: HTML fieldset element
<form action="/action_page.php">
  <fieldset>
    <legend>Enter your name:</legend>
    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname" value="John"><br>
    <label for="lname">Last name:</label><br>
    <input type="text" id="lname" name="lname" value="Doe"><br><br>
    <input type="submit" value="Submit">
  </fieldset>
</form>

This is how the HTML code above will be displayed in a browser:

Enter your name:




return to top of page

The <datalist> Element

  • The <datalist> element specifies a list of pre-defined options for an <input> element.
  • Users will see a drop-down list of the pre-defined options as they input data.
  • The list attribute of the <input> element, must refer to the id attribute of the <datalist> element.
Example 10: HTML datalist element
<form action="/action_page.php">
  <input list="browsers">
  <datalist id="browsers">
    <option value="Internet Explorer">
    <option value="Firefox">
    <option value="Chrome">
    <option value="Opera">
    <option value="Safari">
  </datalist>
</form>

return to top of page

The <output> Element

The <output> element represents the result of a calculation (like one performed by a script).

Example 11: The output element

Perform a calculation and show the result in an <output> element:

<form action="/action_page.php"
  oninput="x.value=parseInt(a.value)+parseInt(b.value)">
  0
  <input type="range"  id="a" name="a" value="50">
  100 +
  <input type="number" id="b" name="b" value="50">
  =
  <output name="x" for="a b"></output>
  <br><br>
  <input type="submit">
</form> 

return to top of page

HTML <option> Tag

A drop-down list with four options:

<label for="cars">Choose a car:</label>

<select id="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

Displays in the browser like this:

Definition and Usage

  • The <option> tag defines an option in a select list.
  • <option> elements go inside a <select>, <optgroup>, or <datalist> element.

Note: The <option> tag can be used without any attributes, but you usually need the value attribute, which indicates what is sent to the server on form submission.

Tip: If you have a long list of options, you can group related options within the <optgroup> tag.

return to top of page

HTML <optgroup> Tag

Group related options with <optgroup> tags:

<label for="cars">Choose a car:</label>
<select  name="cars" id="cars">
  <ptgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select> 

Displays in the browser like this:

return to top of page

HTML Form Elements

Tag Description
<form> Defines an HTML form for user input
<input> Defines an input control
<textarea> Defines a multiline 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> Specifies a list of pre-defined options for input controls
<output> Defines the result of a calculation

For a complete list of all available HTML tags, visit the HTML Tag Reference.