This site is mobile accessible. Press the "Tap Here" button to use a smaller font-size.
Smartphone icons created by Freepik - Flaticon
This chapter describes all the different HTML form elements.
The HTML
element can contain one or more of the following form elements:<label for="fname">First name:</label> <input type="text" id="fname" name="fname">
All the different values of the
attribute are covered in the next chapter: HTML Input Types.The
tag defines a label for several elements:Proper use of labels with the elements above will benefit:
See HTML Reference ‐ <label> Tag on W3Schools.com for details
The
element defines a drop-down list:<label for="cars">Choose a car:</label> <select id="cars" name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>
<option value="fiat" selected>Fiat</option>
Use the
attribute to specify the number of visible values:<label for="cars">Choose a car:</label> <select id="cars" name="cars" size="3"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>
Use the multiple attribute to allow the user to select more than one value:
<label for="cars">Choose a car:</label> <select id="cars" name="cars" size="4" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>
The
element defines a multi-line input field (a text area):<textarea name="message" rows="10" cols="30"> The cat was playing in the garden. <textarea>
This is how the HTML code above will be displayed in a browser:
You can also define the size of the text area by using CSS:
<textarea name="message" style="width:200px; height:100px;"> The cat was playing in the garden. </textarea>
The
element defines a clickable button:<button type="button" onclick="alert('Hello World!')">Click Me!</button>
This is how the HTML code above will be displayed in a browser:
Note: Always specify the
attribute for the button element. Different browsers may use different default types for the button element.<form action="/action_page.php"> <fieldset> <legend>Enter your name:</legend> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="John"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Doe"><br><br> <input type="submit" value="Submit"> </fieldset> </form>
This is how the HTML code above will be displayed in a browser:
<form action="/action_page.php"> <input list="browsers"> <datalist id="browsers"> <option value="Internet Explorer"> <option value="Firefox"> <option value="Chrome"> <option value="Opera"> <option value="Safari"> </datalist> </form>
The
element represents the result of a calculation (like one performed by a script).Perform a calculation and show the result in an
element:<form action="/action_page.php" oninput="x.value=parseInt(a.value)+parseInt(b.value)"> 0 <input type="range" id="a" name="a" value="50"> 100 + <input type="number" id="b" name="b" value="50"> = <output name="x" for="a b"></output> <br><br> <input type="submit"> </form>
A drop-down list with four options:
<label for="cars">Choose a car:</label> <select id="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</option> <option value="audi">Audi</option> </select>
Displays in the browser like this:
Note: The
tag can be used without any attributes, but you usually need the value attribute, which indicates what is sent to the server on form submission.Tip: If you have a long list of options, you can group related options within the
tag.Group related options with
tags:<label for="cars">Choose a car:</label> <select name="cars" id="cars"> <ptgroup label="Swedish Cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> </optgroup> <optgroup label="German Cars"> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </optgroup> </select>
Displays in the browser like this:
Tag | Description |
---|---|
<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 | element
<fieldset> | Groups related elements in a form |
<legend> | Defines a caption for a | 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 the HTML Tag Reference.