input
The most versatile form element, accepting text, numbers, dates, files, and more through the type attribute. Always pair with a label for accessibility.
Input Type Overview
| Category | Types | Purpose |
|---|---|---|
| Text | text, email, password, url, tel, search |
Single-line text input |
| Numeric | number, range |
Numeric values with constraints |
| Date/Time | date, time, datetime-local, month, week |
Temporal values with pickers |
| Selection | checkbox, radio |
Boolean or mutually exclusive choices |
| Other | file, color, hidden |
Specialized inputs |
Text Input Types
type="text"
Default single-line text input. Use for names, titles, and general text.
type="email"
Email address with built-in validation. Mobile keyboards show @ symbol.
type="password"
Masked text input. Use autocomplete to distinguish new vs current passwords.
type="url"
URL input with validation. Mobile keyboards show common URL characters.
type="tel"
Telephone number. Shows numeric keyboard on mobile but allows any characters.
type="search"
Search input with clear button in some browsers.
Code
Numeric Input Types
type="number"
Numeric input with increment/decrement buttons. Use min, max, and step.
type="range"
Slider for selecting a value within a range.
Code
Date and Time Input Types
Native date/time pickers vary by browser. Consider progressive enhancement.
type="date"
type="time"
type="datetime-local"
type="month"
type="week"
With Constraints
Code
Checkbox and Radio Inputs
type="checkbox"
Boolean selection. Can be checked or unchecked independently.
Indeterminate Checkbox
Set via JavaScript for "partially selected" states (e.g., select all).
type="radio"
Mutually exclusive selection within a group (same name). Wrap in fieldset for accessibility.
Code
Other Input Types
type="file"
File upload. Use accept to restrict file types and multiple for multiple files.
type="color"
Color picker. Returns hex value.
type="hidden"
Invisible form data. Used for tokens, IDs, or tracking.
Code
Input States
Disabled
Prevents interaction. Value not submitted with form.
Read-only
Prevents editing but allows selection. Value is submitted with form.
Code
Validation States
Required
Field must have a value before form submission.
Invalid State
Shown when validation fails. Use aria-invalid="true" for explicit invalid styling.
Pattern Validation
Use regex patterns for custom validation.
Min/Max Length
Validation Attributes
| Attribute | Purpose | Example |
|---|---|---|
required |
Field must have a value | <input required /> |
minlength |
Minimum character count | minlength="2" |
maxlength |
Maximum character count | maxlength="100" |
min |
Minimum numeric/date value | min="0" |
max |
Maximum numeric/date value | max="100" |
step |
Increment for numeric values | step="0.01" |
pattern |
Regex pattern for validation | pattern="[A-Za-z]+" |
type |
Built-in validation (email, url) | type="email" |
With Datalist
Add autocomplete suggestions using datalist.
Code
With form-field
Use the form-field custom element for CSS-only validation feedback.
Autocomplete Attribute
Help browsers autofill forms correctly with specific autocomplete values.
| Field Type | Autocomplete Value |
|---|---|
| Full name | name |
email |
|
| Phone | tel |
| Street address | street-address |
| City | address-level2 |
| State/Province | address-level1 |
| Postal code | postal-code |
| Country | country-name |
| Credit card number | cc-number |
| Current password | current-password |
| New password | new-password |
| One-time code | one-time-code |
Code
Toggle Switch (data-switch)
Add data-switch to a checkbox for on/off toggle switch styling. Uses CSS appearance: none with a sliding knob. JS adds role="switch" for screen readers.
| Attribute | Values | Description |
|---|---|---|
data-switch |
"", "sm", "lg" |
Enable switch styling with optional size variant. |
Password Strength (data-strength)
Add data-strength and data-rules to a password input inside <form-field> to show a real-time strength meter and rules checklist. Works alongside the existing password show/hide toggle.
| Attribute | Description |
|---|---|
data-strength |
Enable strength meter and rules checklist. |
data-rules |
Comma-separated rules: length:N, uppercase, lowercase, number, special. |
Strength Levels
- Weak — Less than 40% of rules met
- Fair — 40–74% of rules met
- Good — 75–99% of rules met
- Strong — All rules met
Range Enhancement (data-range)
Add data-range to a range input for cross-browser styled track and thumb. Add data-bubble for a floating value display. Pair with <datalist> for tick labels.
| Attribute | Description |
|---|---|
data-range |
Enable cross-browser track/thumb styling and fill. |
data-bubble |
Show floating value bubble above the thumb. |
data-prefix |
Text before the value (e.g., "$"). |
data-suffix |
Text after the value (e.g., "%", "°F"). |
File Upload Zone (data-upload)
Add data-upload to a file input to wrap it in a drag-and-drop zone with file list display.
| Attribute | Description |
|---|---|
data-upload |
Enable drop zone enhancement on a file input. |
The native accept and multiple attributes work as normal. Without JS, the standard file input is shown.
Input Masking (data-mask)
Add data-mask to a text input to format the value as the user types. Preset masks handle common patterns; use data-mask="custom" with data-pattern for arbitrary formats.
Preset Masks
| Value | Pattern | Example |
|---|---|---|
phone | (###) ###-#### | (555) 123-4567 |
credit-card | #### #### #### #### | 1234 5678 9012 3456 |
date | ##/##/#### | 01/15/2026 |
ssn | ###-##-#### | 123-45-6789 |
zip | ##### | 90210 |
Custom Mask Tokens
| Token | Accepts |
|---|---|
# | Any digit (0-9) |
A | Any letter (a-z, A-Z) |
* | Any alphanumeric character |
Attributes
| Attribute | Description |
|---|---|
data-mask | Mask type: phone, credit-card, date, ssn, zip, or custom. |
data-pattern | Custom pattern string when data-mask="custom". |
Behavior
- Auto-sets
inputmode="numeric"for digit-only masks - Auto-sets
maxlengthfrom the pattern length - Stores the unformatted value in
dataset.rawValuefor form processing - Without JS, the input works as a standard text field
Range Markers (data-markers)
Add data-markers to a data-range input to display tick marks at each step value. Combines with data-bubble and <datalist> labels.
| Attribute | Description |
|---|---|
data-markers |
Show tick marks at each step position along the track. |
Toggle Tags (data-toggle-tags)
Wrap checkboxes in a <fieldset data-toggle-tags> for pill-chip styling. Pure CSS — no JS needed. Add data-max to limit the number of selections.
| Attribute | Description |
|---|---|
data-toggle-tags |
Enable pill-chip styling on a checkbox fieldset. |
data-max |
Maximum number of selections. Unchecked boxes are disabled at the limit. |
Why CSS-Only
Native checkboxes give you form participation, validation, reset, keyboard navigation, and accessibility for free. The visual transformation uses :has(:checked) to style labels. JS is only loaded when data-max is set.
Number Stepper (data-stepper)
Add data-stepper to a number input for custom +/− buttons. Hides native spinners and disables buttons at min/max boundaries.
| Attribute | Description |
|---|---|
data-stepper |
Enable +/− stepper buttons on a number input. |
Without JS, the native number input with browser spinners works fine.
Enhanced Color Input (data-color)
Add data-color to a color input for a styled swatch circle and hex code display. Clicking opens the native color picker.
| Attribute | Description |
|---|---|
data-color |
Enable swatch + hex display enhancement on a color input. |
Without JS, the native color picker renders (functional but unstyled).
Accessibility Notes
- Always use labels: Every input needs an associated label
- Use autocomplete: Helps users fill forms faster and more accurately
- Provide error messages: Use
aria-describedbyto link error text to inputs - Mark invalid fields: Use
aria-invalid="true"for explicit invalid state - Avoid placeholder as label: Placeholders disappear when typing
- Visible focus: Don't remove focus outlines
- Touch targets: Minimum 44x44px for checkboxes and radios
- Fieldsets for radio groups: Always wrap radio groups in fieldset