HTML input types provide specialized controls for different kinds of user input. From basic text fields to advanced date pickers and color selectors, understanding input types is essential for creating effective and user-friendly forms. This comprehensive guide covers all HTML5 input types, their attributes, and best practices for building robust web applications that work seamlessly across different devices and browsers.
Basic Text Input Types
Here are some of the most commonly used text input types:
Text Input
The <input type="text"> is the most basic input field for single-line text entry.
<!-- Basic text input -->
<input type="text" name="username" placeholder="Enter username">
<!-- Text input with validation -->
<input type="text"
name="firstname"
placeholder="First name"
required
minlength="2"
maxlength="50">
<!-- Text input with pattern -->
<input type="text"
name="zipcode"
placeholder="ZIP Code"
pattern="[0-9]{5}"
title="5-digit ZIP code">
Password Input
The <input type="password"> masks entered characters for security.
<!-- Basic password input -->
<input type="password" name="password" placeholder="Enter password">
<!-- Password with validation -->
<input type="password"
name="password"
placeholder="Password"
required
minlength="8"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="Must contain at least one number, one uppercase and lowercase letter, and at least 8 characters">
Email Input
The <input type="email"> provides email validation and optimized mobile keyboards.
<!-- Basic email input -->
<input type="email" name="email" placeholder="email@example.com">
<!-- Email with validation -->
<input type="email"
name="email"
placeholder="Enter your email"
required>
<!-- Multiple email addresses -->
<input type="email"
name="recipients"
placeholder="recipient1@example.com, recipient2@example.com"
multiple>
URL and Search Inputs
<!-- URL input -->
<input type="url" name="website" placeholder="https://example.com">
<!-- Search input -->
<input type="search" name="query" placeholder="Search...">
<!-- Telephone input -->
<input type="tel" name="phone" placeholder="(123) 456-7890">
Numeric Input Types
HTML5 introduced several input types for numeric data, providing built-in validation and user-friendly interfaces.
Number Input
The <input type="number"> provides numeric input with validation and increment/decrement controls.
<!-- Basic number input -->
<input type="number" name="quantity" placeholder="Enter quantity">
<!-- Number with constraints -->
<input type="number"
name="age"
min="18"
max="120"
step="1"
placeholder="Age">
<!-- Decimal number input -->
<input type="number"
name="price"
min="0"
max="9999.99"
step="0.01"
placeholder="Price">
<!-- Integer input -->
<input type="number"
name="items"
min="1"
max="10"
step="1"
value="1">
Range Input
The <input type="range"> creates a slider control for selecting values within a range.
<!-- Basic range slider -->
<input type="range" name="volume" min="0" max="100">
<!-- Range with step -->
<input type="range"
name="rating"
min="1"
max="5"
step="1"
value="3">
<!-- Range with display -->
<label for="brightness">Brightness: <span id="brightnessValue">50</span></label>
<input type="range"
id="brightness"
name="brightness"
min="0"
max="100"
value="50">
Date and Time Input Types
HTML5 provides specialized input types for selecting dates and times, enhancing user experience and data validation.
Date Input
The <input type="date"> provides a date picker interface.
<!-- Basic date input -->
<input type="date" name="birthdate">
<!-- Date with constraints -->
<input type="date"
name="appointment"
min="2024-01-01"
max="2024-12-31">
<!-- Date with default value -->
<input type="date"
name="event"
value="2024-06-15">
<!-- Date for future dates only -->
<input type="date"
name="deadline"
min=""
required>
Time and DateTime Inputs
The <input type="time"> and <input type="datetime-local"> provide interfaces for selecting times and combined date/time values.
<!-- Time input -->
<input type="time" name="meeting">
<!-- Time with step -->
<input type="time"
name="alarm"
step="900"><!-- 15-minute intervals -->
<!-- DateTime-local input -->
<input type="datetime-local" name="event">
<!-- DateTime with constraints -->
<input type="datetime-local"
name="flight"
min="2024-01-01T00:00"
max="2024-12-31T23:59">
Month and Week Inputs
The <input type="month"> and <input type="week"> allow users to select specific months or weeks.
<!-- Month input -->
<input type="month" name="expiry">
<!-- Week input -->
<input type="week" name="week">
<!-- Month with constraints -->
<input type="month"
name="subscription"
min="2024-01"
max="2024-12">
Selection Input Types
Selection input types allow users to choose one or more options from a predefined set.
Checkbox Input
The <input type="checkbox"> allows users to select multiple options from a group.
<!-- Single checkbox -->
<input type="checkbox" id="newsletter" name="newsletter" value="yes">
<label for="newsletter">Subscribe to newsletter</label>
<!-- Multiple checkboxes -->
<fieldset>
<legend>Interests:</legend>
<input type="checkbox" id="sports" name="interests" value="sports">
<label for="sports">Sports</label><br>
<input type="checkbox" id="music" name="interests" value="music">
<label for="music">Music</label><br>
<input type="checkbox" id="movies" name="interests" value="movies">
<label for="movies">Movies</label>
</fieldset>
<!-- Required checkbox -->
<input type="checkbox" id="terms" name="terms" required>
<label for="terms">I agree to the terms and conditions</label>
Radio Input
The <input type="radio"> allows users to select one option from a group.
<!-- Radio button group -->
<fieldset>
<legend>Gender:</legend>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</fieldset>
<!-- Radio with default selection -->
<input type="radio" id="yes" name="agree" value="yes" checked>
<label for="yes">Yes</label>
<input type="radio" id="no" name="agree" value="no">
<label for="no">No</label>
Specialized Input Types
These input types provide specialized functionality for specific use cases.
Color Input
The <input type="color"> provides a color picker interface.
<!-- Basic color picker -->
<input type="color" name="theme">
<!-- Color with default value -->
<input type="color" name="background" value="#3498db">
<!-- Color with label -->
<label for="textColor">Text Color:</label>
<input type="color" id="textColor" name="textColor" value="#333333">
File Input
The <input type="file"> allows users to upload files from their device.
<!-- Basic file upload -->
<input type="file" name="document">
<!-- Multiple file upload -->
<input type="file" name="photos" multiple>
<!-- File with accept restrictions -->
<input type="file" name="image" accept="image/*">
<!-- Specific file types -->
<input type="file" name="resume" accept=".pdf,.doc,.docx">
<!-- File with capture (mobile) -->
<input type="file" name="camera" accept="image/*" capture="environment">
Hidden Input
The <input type="hidden"> stores data that isn't visible to users but is submitted with the form.
<!-- Hidden field for CSRF token -->
<input type="hidden" name="csrf_token" value="abc123xyz789">
<!-- Hidden field for user ID -->
<input type="hidden" name="user_id" value="12345">
<!-- Hidden field for form timestamp -->
<input type="hidden" name="timestamp" value="1640995200">
Button Input Types
These input types provide different ways to create buttons within a form.
Submit Button
The <input type="submit"> submits the form data to the server.
<!-- Basic submit button -->
<input type="submit" value="Submit Form">
<!-- Submit with custom text -->
<input type="submit" value="Send Message">
<!-- Submit with formaction -->
<input type="submit" value="Save Draft" formaction="/save-draft">
Reset and Button Inputs
The <input type="reset"> resets form fields to their default values, while <input type="button"> creates a generic button for custom actions.
<!-- Reset button -->
<input type="reset" value="Clear Form">
<!-- Generic button -->
<input type="button" value="Click Me" onclick="alert('Button clicked!')">
<!-- Button for custom action -->
<input type="button" value="Calculate" onclick="calculateTotal()">
Image Input
The <input type="image"> creates a submit button that uses an image instead of text.
<!-- Image submit button -->
<input type="image" src="submit.png" alt="Submit">
<!-- Image with dimensions -->
<input type="image"
src="search-button.png"
alt="Search"
width="100"
height="30">
Advanced Input Features
These features enhance the functionality and user experience of form inputs.
Datalist Integration
Combine inputs with datalists for autocomplete suggestions.
<!-- Text input with suggestions -->
<input type="text" name="browser" list="browsers">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
<option value="Opera">
</datalist>
<!-- Color input with predefined colors -->
<input type="color" name="theme" list="colors">
<datalist id="colors">
<option value="#ff0000">
<option value="#00ff00">
<option value="#0000ff">
<option value="#ffff00">
<option value="#ff00ff">
</datalist>
Input Validation Attributes
These attributes work across multiple input types to provide validation.
<!-- Required field -->
<input type="email" name="email" required>
<!-- Minimum/maximum values -->
<input type="number" name="age" min="18" max="120">
<!-- Minimum/maximum length -->
<input type="text" name="username" minlength="3" maxlength="20">
<!-- Pattern matching -->
<input type="text" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<!-- Step validation -->
<input type="number" name="quantity" step="5">
Mobile Optimization Attributes
<!-- Input mode for mobile keyboards -->
<input type="text" name="creditcard" inputmode="numeric">
<input type="text" name="email" inputmode="email">
<input type="text" name="phone" inputmode="tel">
<!-- Autocomplete hints -->
<input type="text" name="name" autocomplete="name">
<input type="email" name="email" autocomplete="email">
<input type="tel" name="phone" autocomplete="tel">
<input type="text" name="address" autocomplete="street-address">
Complete Form Examples
Here are some complete form examples using various input types:
Registration Form
This form demonstrates a typical registration process with various input types:
<form action="/register" method="post">
<fieldset>
<legend>Personal Information</legend>
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password"
required minlength="8">
<label for="birthdate">Birth Date:</label>
<input type="date" id="birthdate" name="birthdate"
max="2006-01-01" required>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</fieldset>
<fieldset>
<legend>Preferences</legend>
<label for="theme">Theme Color:</label>
<input type="color" id="theme" name="theme" value="#3498db">
<label for="notifications">Email Notifications:</label>
<input type="checkbox" id="notifications" name="notifications" checked>
<fieldset>
<legend>Account Type:</legend>
<input type="radio" id="basic" name="account" value="basic" checked>
<label for="basic">Basic</label>
<input type="radio" id="premium" name="account" value="premium">
<label for="premium">Premium</label>
</fieldset>
</fieldset>
<input type="hidden" name="csrf_token" value="abc123">
<input type="submit" value="Register">
<input type="reset" value="Clear">
</form>
Product Configuration Form
This form allows users to configure a product with various options:
<form action="/configure" method="post">
<fieldset>
<legend>Product Configuration</legend>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity"
min="1" max="100" value="1">
<label for="priority">Priority Level: <span id="priorityValue">5</span></label>
<input type="range" id="priority" name="priority"
min="1" max="10" value="5" step="1">
<label for="delivery">Delivery Date:</label>
<input type="date" id="delivery" name="delivery"
min="" required>
<label for="delivery-time">Delivery Time:</label>
<input type="time" id="delivery-time" name="delivery-time">
<label for="instructions">Special Instructions:</label>
<input type="text" id="instructions" name="instructions"
placeholder="Any special requirements...">
</fieldset>
<fieldset>
<legend>Additional Options</legend>
<input type="checkbox" id="giftwrap" name="options" value="giftwrap">
<label for="giftwrap">Gift Wrapping (+$5)</label><br>
<input type="checkbox" id="insurance" name="options" value="insurance">
<label for="insurance">Shipping Insurance (+$2)</label><br>
<input type="checkbox" id="express" name="options" value="express">
<label for="express">Express Shipping (+$10)</label>
</fieldset>
<input type="submit" value="Configure Product">
</form>