HTML FormsLesson #44

HTML Form Elements

This chapter describes all the different HTML form elements.

This chapter describes all the different HTML form elements.


The HTML Elements

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

  • <input>
  • <label>
  • <select>
  • <textarea>
  • <button>
  • <fieldset>
  • <legend>
  • <datalist>
  • <output>
  • <option>
  • <optgroup>

The Element

One of the most used form elements is the <input> element.

The <input> element can be displayed in several ways, depending on the type attribute.

Example

Example Code

html
Try it Yourself
Live Output Preview:

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


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


The Element

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

The <option> element 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

Example Code

html
Try it Yourself
<option value="fiat" selected>Fiat</option>

Visible Values:

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

Allow Multiple Selections:

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

The Element

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

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:

The cat was playing in the garden.

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

The ** Element

The <button> element defines a clickable button:

Example

Example Code

html
Try it Yourself
Live Output Preview:

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

Click Me!

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


The and Elements

The <fieldset> element is used to group related data in a form.

The <legend> element defines a caption for the <fieldset> element.

Example

Example Code

html
Try it Yourself
Live Output Preview:

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

Personalia: First name:

Last name:


The 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

Example Code

html
Try it Yourself
Live Output Preview:

The Element

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

Example

Example Code

html
Try it Yourself
Live Output Preview:

HTML Form Elements

TagDescription
<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 our HTML Tag Reference.

Interactive Code Examples

HTML Example 1

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 2

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 3

html
Try it Yourself
HTML tag syntax and structure demonstration.
<option value="fiat" selected>Fiat</option>

HTML Example 4

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 5

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview:

HTML Example 6

html
Try it Yourself
Interactive runnable HTML example from EA Academy.
Live Output Preview: