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

Single-line text inputs for names, emails, passwords, URLs, phone numbers, and search. Each type triggers the appropriate mobile keyboard and built-in validation.

Numeric Input Types

Use type="number" for values with increment/decrement controls, and type="range" for slider selection. Constrain values with min, max, and step.

Date and Time Input Types

Native date/time pickers vary by browser. Consider progressive enhancement. Use min and max to constrain date ranges.

Checkbox and Radio Inputs

Use type="checkbox" for boolean or multi-select choices, and type="radio" for mutually exclusive selections within a group (same name). Always wrap radio groups in a fieldset for accessibility.

Other Input Types

type="file"

File upload. Use accept to restrict file types and multiple for multiple files. See also the data-upload enhancement below.

type="color"

Color picker returning a hex value. See also the data-color enhancement below.

type="hidden"

Invisible form data. Used for tokens, IDs, or tracking.

Input States

Inputs support disabled, readonly, required, and invalid states. Disabled inputs prevent interaction and are not submitted with the form. Readonly inputs allow selection but not editing, and their values are submitted.

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.

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 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

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

ValuePatternExample
phone(###) ###-####(555) 123-4567
credit-card#### #### #### ####1234 5678 9012 3456
date##/##/####01/15/2026
ssn###-##-####123-45-6789
zip#####90210

Custom Mask Tokens

TokenAccepts
#Any digit (0-9)
AAny letter (a-z, A-Z)
*Any alphanumeric character

Attributes

AttributeDescription
data-maskMask type: phone, credit-card, date, ssn, zip, or custom.
data-patternCustom pattern string when data-mask="custom".

Behavior

  • Auto-sets inputmode="numeric" for digit-only masks
  • Auto-sets maxlength from the pattern length
  • Stores the unformatted value in dataset.rawValue for 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-describedby to 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

CSS Reference

Related

  • label - Essential for accessibility
  • form - Container for inputs
  • datalist - Autocomplete suggestions
  • fieldset - Group related inputs (especially radios)
  • textarea - Multi-line text input
  • select - Dropdown selection
  • form-field - Enhanced form field with validation
  • combo-box - Searchable dropdown with filtering
  • data-table - Sortable, filterable data with input controls
  • dialog - Modal forms via method="dialog"