The <input> element changes completely depending on its type attribute. type="date" shows a calendar, type="color" shows a colour picker, and type="email" gives phone users a keyboard with an @ key.
Picking the right type is one of the easiest ways to make a form nicer to use. Visitors type less, make fewer mistakes and get help from their browser, such as autofill and clear error messages. You also get basic validation without writing any JavaScript.
This lesson is a tour of the input types you'll use most and the attributes that fine-tune them.
Text-like input types
| Type | What it's for | Bonus |
|---|---|---|
text | Anything short | The default |
email | Email addresses | Checks for @, email keyboard on phones |
password | Passwords | Hides the characters |
tel | Phone numbers | Number pad on phones |
url | Web addresses | Must start with http:// or https:// |
search | Search boxes | Clear (×) button in some browsers |
number | Numbers | Arrows, min, max, step |
If you leave out type, the browser uses text. That works, but you lose all the extra help the more specific types provide.
Picker input types
| Type | Shows |
|---|---|
date | A calendar |
time | A time picker |
datetime-local | Date and time |
month / week | Month or week picker |
color | A colour picker |
range | A slider |
file | A "Choose file" button (accept limits file types, multiple allows several) |
Each browser draws these pickers in its own style, and phones often show a full-screen picker that is much easier to use with a thumb than typing a date.
Useful input attributes
min,maxandstepset limits for numbers, ranges and dates.minlengthandmaxlengthlimit the length of text.patternsets a rule. For example,pattern="[A-Z]{3}"means exactly three capital letters.autocompletegives hints such asemail,nameornew-passwordso browsers can autofill.inputmodechooses the phone keyboard, for exampleinputmode="numeric".readonlyanddisabledstop editing. Disabled values are not sent with the form.
HTML Input Types Syntax
Input with a type
<input type="email" id="e" name="email" autocomplete="email" required>
<input type="number" name="qty" min="1" max="10" step="1" value="1">HTML Input Types Summary
- The
typeattribute decides how an<input>looks and behaves. - Specific types such as
email,telanddategive better keyboards and pickers. - Radio buttons in one group share a
name; checkboxes can be ticked independently. - Attributes like
min,max,patternandautocompletefine-tune each input. - Use
numberonly for real quantities, not for phone or card numbers.
HTML Input Types Examples
Each example runs in the page. Edit the code and press Run to see what changes.
A gallery of input types
Try each one in the result. On a phone, notice how the keyboard changes.
<p><label>Email <input type="email" name="email"></label></p>
<p><label>Password <input type="password" name="pw" minlength="8"></label></p>
<p><label>Tickets <input type="number" name="tickets" min="1" max="6" value="2"></label></p>
<p><label>Birthday <input type="date" name="bday"></label></p>
<p><label>Alarm <input type="time" name="alarm"></label></p>
<p><label>Favourite colour <input type="color" name="fav" value="#17a865"></label></p>
<p><label>Volume <input type="range" name="vol" min="0" max="10"></label></p>
<p><label>Photo <input type="file" name="photo" accept="image/*"></label></p>Line-by-line explanation
- Line 1Shows an email keyboard on phones and checks the format on submit.
- Line 2Hides what you type.
minlengthasks for 8+ characters. - Line 3Number with arrows, limited from 1 to 6.
- Lines 4 to 5Native date and time pickers, with no JavaScript needed.
- Line 6A colour picker, starting on a fresh green.
- Line 7A slider from 0 to 10.
- Line 8File upload limited to images.
Pattern and autocomplete
Try submitting with ab12 as the code, then with ABC.
<form>
<p>
<label for="code">Discount code (3 capital letters)</label><br>
<input id="code" name="code" pattern="[A-Z]{3}" title="Three capital letters, like ABC" required>
</p>
<p>
<label for="card">Card number</label><br>
<input id="card" name="card" type="text" inputmode="numeric" autocomplete="cc-number">
</p>
<button>Apply</button>
</form>Line-by-line explanation
- Line 4
patternis a regular expression the value must match.titleexplains the rule in the error bubble. - Line 8Card numbers aren't for maths, so use text with a numeric keyboard and the right
autocompletehint.
Common HTML Input Types Mistakes
Using text for everything
<input type="text" name="email">
<input type="text" name="age"><input type="email" name="email">
<input type="number" name="age" min="0" max="120">Specific types give better keyboards, better autofill and built-in validation.
Number type for phone numbers
<input type="number" name="phone"><input type="tel" name="phone" autocomplete="tel">Number inputs drop leading zeros (07… becomes 7…) and don't allow + or spaces.
HTML Input Types in Real Projects
Better forms convert better
Online shops measure how many people finish checkout. Using type="email", autocomplete hints and the right phone keyboard measurably reduces abandoned forms: fewer typos, faster typing, and browsers can fill in addresses automatically.
HTML Input Types Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Use the input type that shows a calendar.
<label>Check-in <input type="" name="checkin"></label>
type="date" shows a native date picker.Which type hides the characters you type?
type="password" masks input. type="hidden" is an invisible field.Which input type should you use for a UK mobile number like 07700 900123?
tel keeps leading zeros and spaces and shows a phone keypad.The "Guests" field should only allow 1 to 8 guests, and the colour field should be a colour picker. Fix both.
<label>Guests <input type="text" name="guests"></label>
<label>Theme <input type="text" name="theme" value="#ff6600"></label>- Guests is a number input
- Guests min is 1 and max is 8
- Theme is a colour picker
number with min/max limits the range; color shows a picker.min and max.HTML Input Types Quiz
4 questions to check what you learned.
Which input type shows a slider?
- range
- slider
- scale
- number
What does the
stepattribute do on a number input?- Sets how many inputs appear
- Sets the allowed increments, like 0.5
- Moves the cursor
- Limits the number of characters
Which attribute limits a file input to images?
type="image"only="images"filter="img"accept="image/*"
What's the difference between
readonlyanddisabled?- Read-only values are still submitted; disabled values are not
- No difference
- Disabled values are submitted; read-only aren't
- Read-only only works on checkboxes
HTML Input Types Challenge
Event booking
Build a form with these inputs (each with a label):
type="email",name="email", requiredtype="number",name="age"withmin="13"andmax="99"type="date",name="day"type="range",name="excitement"from 1 to 10
<form>
<button>Book a place</button>
</form>- Required email input
- Age is a number from 13 to 99
- A date input named day
- A range named excitement from 1 to 10
- Every input has a label
<label> is the quickest way to label them.