- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Form Elements
HTML Forms
HTML Form Elements
A form is built from a small set of elements: input for most fields, label to name them, textarea for longer text, select for drop-downs, button to send. Learn these and you can build almost any form.
How the form itself works, where the data goes and how, is covered in HTML form basics.
Common Form Elements
| Element | Description |
|---|---|
<input> | Used to get user input in various formats (text, password, email, etc.). |
<label> | Defines a text label for form controls to improve accessibility. |
<textarea> | Creates a multi-line text field for long input such as comments. |
<select> | Defines a dropdown list of options. |
<option> | Defines each selectable item in a dropdown. |
<button> | Adds clickable buttons (submit, reset, or custom actions). |
<fieldset> | Groups related form controls together. |
<legend> | Provides a caption or title for a fieldset. |
<datalist> | Offers pre-defined suggestions while typing in an input. |
<output> | Displays the result of a calculation or script. |
<optgroup> | Groups related options within a <select> dropdown. |
Basic Example
html
<form action="/samples/form_processor.php" method="post">
<label for="name">Full Name:</label>
<input type="text" id="name" name="fullname"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message" rows="4" cols="30"></textarea><br><br>
<label for="country">Select Country:</label>
<select id="country" name="country">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">UK</option>
</select><br><br>
<button type="submit">Submit</button>
</form>The form has labelled fields for name and email, a message box, a country drop-down and a submit button. Click a label and the cursor jumps into its field.
The elements in a sentence each
<input>is the workhorse: text, email, password, number, checkbox, radio, file and more, chosen withtype. See HTML Input Types.<label>names a field. Always use one.<textarea>is for several lines of text, like a message.<select>with<option>makes a drop-down list.<button>sends the form, or does something else withtype="button".<fieldset>and<legend>group related fields under a title.
Example with <datalist> and <output>
Form Example
html
<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
<label for="browser">Choose your browser:</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Edge">
<option value="Safari">
</datalist>
<br><br>
<input type="number" id="a" value="5"> +
<input type="number" id="b" value="3"> =
<output name="result" for="a b"></output>
</form>Type in the browser field and suggestions appear from the <datalist>. Change the two numbers and the <output> shows their sum straight away, because the form's oninput recalculates it on every change.
Good to know
- Every field that should be sent needs a
name. Without it, the value is silently left out. - Put a
<label>on every field; placeholders are not labels. - A
<button>inside a form is a submit button by default. Addtype="button"to buttons that should not send the form.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
HTML Form Elements exercises
A drop-down list
Make a select named size with the options Small and Large.
Result
Check your answer to see it here.
