HTML Input Types Reference

The <input> element's type attribute determines the appearance and behavior of the input field. HTML provides 20+ input types covering text, numeric, date, selection, button, and other scenarios.

Text Input Types

Text input types are used to collect various forms of textual information:

Example

HTML
<form>
<label>Username: <input type="text" name="username" placeholder="Enter username"></label><br>
<label>Password: <input type="password" name="password" placeholder="At least 6 characters"></label><br>
<label>Email: <input type="email" name="email" placeholder="you@example.com"></label><br>
<label>Phone: <input type="tel" name="phone" placeholder="13800138000"></label><br>
<label>Website: <input type="url" name="website" placeholder="https://"></label><br>
<label>Search: <input type="search" name="q" placeholder="Search keywords"></label>
</form>
▶ Try it Yourself

Numeric Input Types

Numeric input types let users select or enter numbers and color values:

Example

HTML
<form>
<label>Quantity (1-10): <input type="number" name="qty" min="1" max="10" value="1"></label><br>
<label>Volume: <input type="range" name="volume" min="0" max="100" value="50"></label><br>
<label>Theme Color: <input type="color" name="theme" value="#ff6600"></label>
</form>

<!-- range and number sync (requires JS) -->
<input type="range" id="slider" min="0" max="100" value="50">
<input type="number" id="numDisplay" min="0" max="100" value="50">
<script>
const slider = document.getElementById('slider');
const num = document.getElementById('numDisplay');
slider.addEventListener('input', () => num.value = slider.value);
num.addEventListener('input', () => slider.value = num.value);
</script>
▶ Try it Yourself

Date and Time Input Types

Date and time input types let users quickly pick dates through a calendar or time picker:

Example

HTML
<form>
<label>Date: <input type="date" name="date"></label><br>
<label>Time: <input type="time" name="time"></label><br>
<label>Date + Time: <input type="datetime-local" name="datetime"></label><br>
<label>Month: <input type="month" name="month"></label><br>
<label>Week: <input type="week" name="week"></label>
</form>
▶ Try it Yourself

Selection Input Types

Selection input types let users pick from multiple options:

Example

HTML
<form>
<p>Hobbies (multiple selections allowed):</p>
<label><input type="checkbox" name="hobby" value="reading"> Reading</label>
<label><input type="checkbox" name="hobby" value="music"> Music</label>
<label><input type="checkbox" name="hobby" value="sports"> Sports</label>

<p>Gender (single selection):</p>
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>

<p>Upload avatar (images only):</p>
<input type="file" name="avatar" accept="image/png, image/jpeg, image/gif">
</form>
▶ Try it Yourself

Button Input Types

Button input types are used to trigger form actions:

Example

HTML
<form action="/submit" method="post">
<label>Name: <input type="text" name="name"></label><br>

<!-- Submit button -->
<input type="submit" value="Submit Form">

<!-- Reset button -->
<input type="reset" value="Reset">

<!-- Generic button -->
<input type="button" value="Click Me" onclick="alert('Hello!')">

<!-- Image button -->
<input type="image" src="btn-submit.png" alt="Submit">
</form>
▶ Try it Yourself

Hidden and Special Inputs

type="hidden" fields are not displayed on the page but are submitted with the form. They are commonly used for passing metadata such as user IDs and CSRF tokens:

Example

HTML
<form action="/update-profile" method="post">
<!-- Invisible to users, but submitted with the form -->
<input type="hidden" name="user_id" value="12345">
<input type="hidden" name="token" value="abc123xyz">

<label>Nickname: <input type="text" name="nickname"></label>
<input type="submit" value="Save">
</form>
▶ Try it Yourself
💡 Note: Hidden inputs are commonly used to pass user IDs, tokens, and other data that users don't need to see. However, this data is still visible to users (view the source code). Therefore, never use hidden fields to store sensitive information — real security validation should be done server-side.

Complete Input Type Reference Table

Type Description Common Attributes
text Single-line text placeholder, maxlength, pattern
password Password (masked display) placeholder, maxlength
email Email address placeholder, multiple
tel Telephone number placeholder, pattern
url URL / web address placeholder
search Search box placeholder
number Numeric input min, max, step
range Range slider min, max, step
color Color picker
date Date picker min, max, step
time Time picker min, max, step
datetime-local Local date + time min, max
month Month picker min, max
week Week picker min, max
checkbox Checkbox (multi-select) checked
radio Radio button checked
file File upload accept, multiple
submit Submit button value, formaction
reset Reset button value
button Generic button value, onclick
image Image button src, alt
hidden Hidden field value

❓ FAQ

Q Why use email, tel, url types instead of just text?
A Semantic input types offer three benefits: 1) Mobile devices show context-appropriate keyboards (email shows @ symbol, tel shows numeric keypad); 2) Browsers auto-validate format (e.g., email must contain @); 3) Screen readers tell users "this is an email field," improving accessibility. While text works, using the correct type provides a better user experience.
Q What about date type inconsistency across browsers?
A This is a known issue with HTML5 date pickers — Chrome, Firefox, and Safari each have different UIs. Solutions: 1) Accept differences and rely on native browser experience; 2) Use a JavaScript date library (like flatpickr) for unified UI; 3) Use text type with JavaScript validation for full customization. Recommendation: if design consistency matters, use option 2 or 3; for native experience, use option 1.
Q What's the difference between number and range? When should I use each?
A number is for precise numeric input — users can type exact values, ideal for quantities, ages, prices. range is a slider — users can only slide within a range, ideal for volume, brightness, ratings where precision isn't critical. Both support min, max, step to limit range. Common pattern: use range for rough adjustment, number for fine-tuning, and link them together.
Q Is hidden type secure? Can users see it?
A hidden inputs don't appear on the page, but users can see their values via "View Source" or DevTools. Never store passwords, API keys, or sensitive info in hidden. Its proper use is passing user IDs, CSRF tokens, session identifiers, and other metadata. Security principle: never trust any client-side data — real security validation must happen server-side.

📖 Summary

📝 Exercises

  1. Registration Form: Create a "Sign Up" form containing 5+ input types: text (username), email, password, date (birthday), radio (gender), checkbox (hobbies), file (avatar upload), submit, and reset.
  2. Range Sync: Create a range slider and a number input that sync their values in real time. Range: 0—100, step: 5.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏