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:

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

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:

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

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:

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

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:

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

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:

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:

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

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:

  <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

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:

    Name: <input type="text"><br>
    Surname: <input type="text"><br>
    Class: <input type="text"><br>
    Favourite food: <input type="text">

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:

    Name: <input type="text"><br>
    Surname: <input type="text"><br>
    Favourite class: <input type="text"><br>

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

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

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:

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

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

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

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.

Latest Coupon Found:



Learn the skills of tomorrow

The best time to learn programming is now - follow this link to access 100+ coding courses and enjoy learning at a very low cost!

Expiration date: 15/05/2021
2287 People Used
Only 92 Left