HTML provides a variety of input types to collect different types of user data. Understanding these input types helps in creating more effective and user-friendly forms.
Here are some of the most common input types used in HTML forms:
Input Type | Description | Example |
---|---|---|
text | Allows the user to enter a single-line text input. |
Try yourself
|
password | Allows the user to enter a password, where the characters are masked. |
Try yourself
|
Allows the user to enter an email address. |
Try yourself
|
|
number | Allows the user to enter a number. |
Try yourself
|
checkbox | Allows the user to select one or more options. |
Try yourself
|
radio | Allows the user to select one option from a group. |
Try yourself
|
submit | Creates a button to submit the form data to a server. |
Try yourself
|
reset | Creates a button to reset the form data to its initial values. |
Try yourself
|
date | Allows the user to select a date. |
Try yourself
|
color | Allows the user to select a color. |
Try yourself
|
range | Allows the user to select a value from a range of numbers. |
Try yourself
|
file | Allows the user to select a file from their device. |
Try yourself
|
hidden | Creates an input element that is not visible to the user. |
Try yourself
|
Here is how you can use different input types in an HTML form:
Example: Text Input
<input type="text" name="username" placeholder="Enter your username">
This example shows a single-line text input field.
<input type="password" name="password" placeholder="Enter your password">
This example shows a password input field with masked characters.
<input type="email" name="email" placeholder="Enter your email">
This example shows an email input field.
<input type="number" name="age" min="1" max="100">
This example shows a number input field.
<input type="checkbox" name="subscribe" value="newsletter"> Subscribe to newsletter
This example shows a checkbox input field.
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
This example shows a radio button input field.
<input type="submit" value="Submit">
This example shows a submit button.
<input type="reset" value="Reset">
This example shows a reset button.
<form>
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
</form>
This example shows a date input field.
<form>
<label for="favcolor">Favorite Color:</label>
<input type="color" id="favcolor" name="favcolor">
</form>
This example shows a color input field.
<form>
<label for="vol">Volume:</label>
<input type="range" id="vol" name="vol" min="0" max="100">
</form>
This example shows a range input field.
<form>
<label for="file">Upload a file:</label>
<input type="file" id="file" name="file">
</form>
This example shows a file input field.
<form>
<input type="hidden" id="userid" name="userid" value="12345">
<input type="submit" value="Submit">
</form>
This example shows a hidden input field.
Here are some important notes and best practices when using HTML input types: