Code has been added to clipboard!

Introduction to HTML Form Elements

Reading time 4 min
Published Jun 23, 2017
Updated Oct 2, 2019

TL;DR – HTML form elements let the developer collect user input of various types.

Using HTML <form> Tags

To create an HTML form, you need to use <form> tags:

Example
<form> 
  <br>First name:<br>
  <input type="text" name="first name" placeholder="firstname"> 
  <br>Last name:<br>
  <input type="text" name="lastname" placeholder="lastname"> 
</form>

However, the tags don't create the form itself. The <form> element acts as a container for the form elements that create and define the functionalities you expect from your HTML form. Input types and other information can also be defined using attributes.

New Form Elements in HTML5

The <datalist> element defines a list of options for the <input> element. The user will see an HTML drop down list and will be able to select an option:

Example
<input list="names">
  <datalist id="names">
    <option value="Tom">
    <option value="Sam">
    <option value="Jack">
    <option value="Annie">
    <option value="Rich">
  </datalist>
</input>

Note: the id attribute of the <datalist> element must match the list attribute of the <input> element.

The <output> element defines a result of a calculation. If you include two or more numeric inputs, this element will be able to generate the result:

Example
<form oninput="result.value=parseInt(x.value)+parseInt(y.value)">
  0<input type="range" id="x" value="75">100
  +<input type="number" id="y" value="72">
  =<output name="result" for="x y"></output>
</form>

The <keygen> element generated a private and public key pair to be used while submitting a form. However, this element is no longer supported in HTML5:

Example
<form action="login" method="POST">
  Username: <input type="text" name="user_name">
  Password: <input type="password" name="user_password">
  Keygen Strength: <keygen name="login_form">
  <input type="submit" value="Login">
</form>

Common Native Form Elements

The <input> element is arguably the most important of all the form elements. It defines a place for the user input, which can come it various formats specified by the type attribute. In the example below, you can see type="text", so the user will be shown a box in which they have to type using their keyboard:

Example
Email: <input type="text" name="email"><br>
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">

See all the available HTML5 input types in the table below:

button checkbox color date datetime-local email
file hidden image month number password
radio range reset search submit tel
text time url week

The <textarea> element comes in handy when we need textual data but a single line that a text field offers is not enough. It provides a multiline area for the user input:

Example
<textarea rows="2" cols="50">
  I am writing multiline text here. Some more text. Text. Text. Text. Text.
</textarea>

The <label> tag sets a name for the <input> element. It gives the user a clear indication on what information they have to type in. It also improves user experience by increasing the clickable area:

Example
<form>
  <input type="radio" name="day" value="monday" checked>Monday<br>
  <input type="radio" name="day" value="tuesday">Tuesday<br>
  <input type="radio" name="day" value="wednesday">Wednesday
</form>

The <fieldset> element groups related elements which are then displayed in one separated box. It is usually used for elements that make no sense being used alone, e.g., radio buttons:

Example
<form>
  <fieldset>
    <legend>Student:</legend>
    Name: <input type="text"><br>
    Surname: <input type="text"><br>
    Class: <input type="text"><br>
    Favourite food: <input type="text">
  </fieldset>
</form>

Tip: if you want your user to be able to select multiple options in your HTML form, a group of checkboxes can be a great option.

The <legend> element creates a title for the <fieldset> element:

Example
<form>
  <fieldset>
    <legend>Student:</legend>
    Name: <input type="text"><br>
    Surname: <input type="text"><br>
    Favourite class: <input type="text"><br>
  </fieldset>
</form>

The <select> element generates an HTML drop down list:

Example
<select name="housepets">
  <option value="cat">Cat</option>
  <option value="dog">Dog</option>
  <option value="llama">Llama</option>
  <option value="rabbit">Rabbit</option>
  <option value="animal">Animal</option>
</select>

You must include at least one option for the HTML drop down list to work properly. For that, you will have to use the <option> tags. One pair of tags define a single option, but you can add as many as you need:

Example
<p>Which pet would you like to keep at home?</p>
  <select>
  <option value="cat">Cat</option>
  <option value="wolf">Wolf</option>
  <option value="llama">Llama</option>
</select>

You can also group related options in an HTML drop down list using <optgroup> tags:

Example
<select>
  <optgroup label="Sports">
    <option value="baseball">Baseball</option>
    <option value="basketball">Basketball</option>
  </optgroup>
  <optgroup label="Shopping">
    <option value="food">Food</option>
    <option value="gift">Gift</option>
  </optgroup>
</select>

HTML Form Elements: Useful Tips

  • Don't forget to include the submit button at the end of your form so the user can send the data to the server. You can do that using the <input> element with the type attribute and its value defined as submit.
  • Defining the name for a certain form control using the name attribute will help the server identify the inputted value.
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>