HTML Tags: Forms

These are the HTML tags you use to make forms and their content.

TagTypeDescription
<form>BlockRepresents a form
Input/output
<label>InlineRepresents the caption of a form field
<input>InlineCreates an interactive input field
<output>InlineCreates an output container
<select>InlineCreates a dropdown selection
<optgroup>OtherRepresents a group of <option> in <select>
<option>OtherRepresents an option inside <select>
<button>InlineCreates a button.
<textarea>InlineCreates a multiline textbox.
<meter>InlineCreates a meter
<progress>InlineCreates a progress bar
<datalist>OtherContains a set of <option> elements that represents permissible/recommended options for a field
Fieldset
<fieldset>BlockGroups fields together
<legend>BlockRepresents the caption of a <fieldset>

<form>

Represents a form.

Attributes related to submission
  • action=[...] — the URL to which the form is submitted (which URL performs some action on the form data)
  • method=[...] — the HTTP method with which the form is submitted; specify either post or get
  • enctype=[...] — if method="post", this specifies the MIME type of the form submission. Can be:
    • application/x-www-form-urlencoded (the default value)
    • multipart/form-data (use if there is any <input type="file">)
  • novalidate — don't validate form before being submitted
  • target=[...] — where to display the response (read here)
Other attributes
  • accept-charset=[...] — space-separated character encodings the server accepts, e.g. utf-8
  • autocomplete=[...] — indicates whether input fields can be autocompleted by the browser; specify either on or off
  • name=[...] — name of the form (must be unique)
  • rel=[...] read here
| LIVE PREVIEW
Username:
Password:

<label>

Represents the caption of a form field.

It's not a normal text if you click on a label, the associated form field will receive focus.

Possible attributes
  • for=[...]id of a form item to be associated with.
| LIVE PREVIEW

<input>

Creates an interactive input field.

Common attributes (apply to most, if not all)
  • name=[...] — name of this input
  • value=[...] — value of this input
  • type=[...] — type of this input, read next section
  • autofocus — sets focus on input when the page has finished loading
  • disabled — disables input (the user cannot interact with it)
  • readonly — makes the user unable to change this input's value
  • required — forces the user to specify this input's value before submitting the form
  • autocomplete=[...] — hints if the browser can autofill this input; specify either on or off
  • form=[...]id of a <form id="..."> this element is associated with (read here)
  • list=[...]id of a <datalist id="..."> this element is associated with
Possible type values
It's advised to see the following table using Chrome for the best support.
TypeDescriptionPreview of <input type="...">
text

Single-line text input (default)

Additional attributes
  • minlength=[...], maxlength=[...]
  • dirname=[...] (like name, but this would be the directionality field name of the input; the directionaly field value would be ltr or rtl, set by the browser)
  • pattern=[...] (regex)
  • placeholder=[...]
password

Password input

Additional attributes
  • minlength=[...], maxlength=[...]
  • pattern=[...] (regex)
  • placeholder=[...]
url

Like text, but input pattern is validated

Additional attributes
  • minlength=[...], maxlength=[...]
  • placeholder=[...]
email

Like text, but input pattern is validated

Additional attributes
  • multiple (allow multiple values)
tel

Like text, but for phone input (uses phone keypad in some devices)

Additional attributes
  • minlength=[...], maxlength=[...]
  • pattern=[...] (regex)
  • placeholder=[...]
search

Like text, but for search query (displays search icon in some devices)

  • minlength=[...], maxlength=[...]
  • placeholder=[...]
  • dirname=[...] (like name, but this would be the directionality field name of the input; the directionaly field value would be ltr or rtl, set by the browser)
checkbox

Checkbox input

Additional attributes
  • checked
radio

Radio input (only one radio input with the same name can be selected)

Additional attributes
  • checked
color

Color selector

number

Number selector

Additional attributes
  • min=[...], max=[...], step=[...]
range

Range selector

Additional attributes
  • min=[...], max=[...], step=[...]
file

File selector

Additional attributes
  • accept=[...] (comma-separated items like: .mp3, audio/mp3, or audio/*)
  • capture=[...] (which camera to use, e.g. user for front camera, environment for back camera)
  • multiple (allow multiple values)
date

Date selector

time

Time selector

datetime-local

Date and time selector

month

Month and year selector

week

Week and year selector

button

Renders a button (use of <button> tag is preferred)

reset

Renders a reset button (resets the form to its default values)

submit

Renders a submit button

Additional attributes
  • formaction=[...], formmethod=[...], formtarget=[...], formenctype=[...], formnovalidate
image

Renders a graphical submit button

Additional attributes
  • src=[...], alt=[...], height=[...], width=[...]
  • formaction=[...], formmethod=[...], formtarget=[...], formenctype=[...], formnovalidate
hidden

Renders a hidden input (the control is not displayed to the user, but the value will still be sent on submission)

<output>

Creates an output container. Its value/content is not submitted during form submission.

Does nothing by itself; you will need to implement the behavior using JavaScript.

Common attributes (apply to most, if not all)
  • name=[...] — name of this output
  • for=[...] — space-separated id's of <input id="..."> elements that contribute to the output
  • form=[...] read here
| LIVE PREVIEW
+ = 60

<select>

Creates a dropdown selection.

Possible attributes
  • name=[...] — name of this input
  • autofocus — sets focus on input when the page has finished loading
  • disabled — disables input (the user cannot interact with it)
  • required — forces the user to specify this input's value before submitting the form
  • autocomplete=[...] — hints if the browser can autofill this input; specify either on or off
  • form=[...]id of a <form id="..."> this element is associated with (read here)
  • multiple — allows selecting multiple values
  • size=[...] — if multiple is specified, controls how many options in the scrolling list box are visible at one time
| LIVE PREVIEW

<optgroup>

Represents grouped options in <select>.

Possible attributes
  • label=[...] — label of this group
  • disabled — disables all options in this group
| LIVE PREVIEW

<option>

Represents an option inside <select> or <datalist>.

Possible attributes
  • value=[...] — value of the option (to be sent if the option is selected). If omitted, the value is taken from the tag's content.
  • label=[...] — label of the option (the one displayed to the user). If omitted, the value is taken from the tag's content.
  • disabled — makes option unselectable
  • selected — makes option selected by default

<button>

Creates a button.

Common attributes
  • type=[...] — type of the button. Can be:
    • submit (default: behaves like <input type="submit">)
    • reset (behaves like <input type="reset">)
    • button (behaves like normal button)
  • name=[...] — if type="submit", this is name of the button when the form is submitted using this button
  • value=[...] — if type="submit", this is value of the button when the form is submitted using this button
  • autofocus — sets focus on button when the page has finished loading
  • disabled — disables button (the user cannot interact with it)
Attributes related to form
  • form=[...]id of a <form id="..."> this element is associated with (read here)
  • formaction=[...] — overrides <form action="...">
  • formmethod=[...] — overrides <form method="...">
  • formenctype=[...] — overrides <form enctype="...">
  • formnovalidate — overrides <form novalidate>
  • formtarget=[...] — overrides <form target="...">
| LIVE PREVIEW

<textarea>

Creates a multiline textbox.

Common attributes
  • name=[...] — name of this input
  • placeholder=[...] — hint text to show when input is empty
  • cols=[...] — width of input in character width
  • rows=[...] — height of input in lines
  • disabled — disables input (the user cannot interact with it)
  • readonly — makes the user unable to change this input's value
Less common attributes
  • autofocus — sets focus on input when the page has finished loading
  • autocomplete=[...] — hints if the browser can autofill this input; specify either on or off
  • spellcheck=[...] — controls spellcheck; specify either on, off, or default
  • wrap=[...] — controls line wrap. For both values, the browser ensures all line breaks consist of CR+LF pair. Can be:
    • hard (insert line breaks for lines exceeding cols)
    • soft (do not insert additional line breaks)
  • form=[...]id of a <form id="..."> this element is associated with (read here)
Attributes related to validation
  • required — forces the user to specify this input's value before submitting the form
  • minlength — minimum number of characters
  • maxlength — maximum number of characters
| LIVE PREVIEW

<meter>

Creates a meter.

Possible attributes
  • value — current meter value
  • min — minimum value
  • max — maximum value
  • low — threshold to indicate low meter value
  • high — threshold to indicate low meter value
  • optimum — optimum meter value
  • form=[...]id of a <form id="..."> this element is associated with (read here)
| LIVE PREVIEW
50/100

<progress>

Creates a progress bar.

Possible attributes
  • value — current progres bar value
  • max — maximum progress bar value
| LIVE PREVIEW
70%

<datalist>

Contains a set of <option> elements that represents permissible/recommended options for a field.

You refer to this element by giving this element's id in <input list="...">.

| LIVE PREVIEW

<fieldset> and <legend>

<fieldset> groups fields together.

<legend> represents the caption of a <fieldset>.

| LIVE PREVIEW
Choose your move

References