HTML Reference ยท Forms
The <input> element
Reference for the HTML <input> element: every attribute with the input types it applies to, ARIA roles per type, labeling, validation notes and technical details.
The <input> element is the most versatile form control. Its type attribute decides what it is: a one-line text field, a checkbox, a date picker, a slider, a file chooser, a hidden value or a button. With no type (or an unknown one) it is a plain text field. Each type is described on the <input> types page; this page covers the element and every attribute.
<input> is a void element: it has no content and no end tag. Give each input a name so its value is submitted, and a <label> so people know what it is for.
<label for="guests">Number of guests</label> <input type="number" id="guests" name="guests" min="1" max="12" value="2">
Try it
The browser checks required, minlength, pattern, type="email" and min/max before the submit event fires; the form only "sends" once every field is valid. The read-only and hidden values are submitted too.
Attributes
The "Applies to" column lists the type values for which the attribute has an effect. On other types it is ignored (and invalid to write).
| Attribute | Values | Applies to | Description |
|---|---|---|---|
type |
One of the type keywords | all | The kind of control. Defaults to text. |
name |
Text | all | Name submitted with the value. Inputs without a name are not submitted. The special name _charset_ on a hidden input is filled with the submission's encoding. |
value |
Text | all except file and image |
The initial value. For checkbox and radio it is the value sent when checked (default on); for buttons it is the visible label. |
disabled |
Boolean | all | The control can't be focused, edited or clicked, and is not submitted. |
form |
ID of a <form> |
all | Associates the input with a form elsewhere in the document. |
autocomplete |
on, off, or autofill tokens such as name, email, username, new-password, current-password, tel, street-address, postal-code, bday, one-time-code |
hidden, text, search, url, tel, email, password, date, month, week, time, datetime-local, number, range, color |
Tells the browser what kind of data the field expects so it can autofill it. |
required |
Boolean | text, search, url, tel, email, password, date, month, week, time, datetime-local, number, checkbox, radio, file |
The form can't be submitted until the control has a value (or, for radios, one in the group is checked). |
readonly |
Boolean | text, search, url, tel, email, password, date, month, week, time, datetime-local, number |
The value can't be edited but can be focused, selected and is still submitted. Read-only fields are not validated. |
placeholder |
Text | text, search, url, tel, email, password, number |
A short hint shown while the field is empty. Not a replacement for a label. |
maxlength |
Non-negative integer | text, search, url, tel, email, password |
Maximum length of the value, in UTF-16 code units. The browser stops further typing. |
minlength |
Non-negative integer | text, search, url, tel, email, password |
Minimum length. Checked only once the user has edited the value. |
pattern |
Regular expression | text, search, url, tel, email, password |
The whole value must match. Browsers compile it with the v flag, so some characters such as - in classes need escaping. Explain the format in the label or a title. |
size |
Positive integer | text, search, url, tel, email, password |
Visible width in characters (default 20). CSS width is usually better. |
min |
Number, date or time in the type's format | date, month, week, time, datetime-local, number, range |
Lowest allowed value. |
max |
Number, date or time in the type's format | date, month, week, time, datetime-local, number, range |
Highest allowed value. |
step |
Positive number or any |
date, month, week, time, datetime-local, number, range |
Granularity of allowed values, counted from min (or the default value). Defaults: 1 for numbers, 1 day, 1 week, 1 month, 60 seconds for times. |
list |
ID of a <datalist> |
text, search, url, tel, email, date, month, week, time, datetime-local, number, range, color |
Suggested values shown while the user types or picks. |
multiple |
Boolean | email, file |
Allows a comma-separated list of addresses, or several files. |
checked |
Boolean | checkbox, radio |
The control is checked when the page loads (or when the form is reset). |
accept |
Comma-separated MIME types or extensions, such as image/*, .pdf |
file |
Hints which file types the picker should offer. Not enforced; check on the server. |
capture |
user, environment |
file |
On mobile devices, asks to capture a new photo or video with the front (user) or back (environment) camera. |
alt |
Text | image |
Text alternative for the image button. Required for type="image". |
src |
URL | image |
The image to show as the button. |
width, height |
Non-negative integer (pixels) | image |
Dimensions of the image button. |
dirname |
Text | text, search and other text-entry types |
Also submits the text direction (ltr or rtl) of the value, under this name. |
formaction |
URL | submit, image |
Overrides the form's action. |
formmethod |
get, post, dialog |
submit, image |
Overrides the form's method. |
formenctype |
application/x-www-form-urlencoded, multipart/form-data, text/plain |
submit, image |
Overrides the form's enctype. |
formnovalidate |
Boolean | submit, image |
Submits without constraint validation. |
formtarget |
_self, _blank, _parent, _top, or a name |
submit, image |
Overrides the form's target. |
popovertarget |
ID of an element with popover |
button, submit, reset, image |
Makes the button show, hide or toggle that popover. |
popovertargetaction |
toggle (default), show, hide |
button, submit, reset, image |
What popovertarget does. |
alpha |
Boolean | color |
Lets the user pick a transparency value too (newer feature with limited support). |
colorspace |
limited-srgb (default), display-p3 |
color |
Color space for the picked value (newer feature with limited support). |
It also accepts the global attributes. Three of them are especially useful on inputs: autofocus (focus on page load), inputmode (which on-screen keyboard to show, such as numeric or decimal) and enterkeyhint (the label of the Enter key on virtual keyboards, such as search or next).
ARIA role by type
type |
Implicit ARIA role |
|---|---|
text, email, tel, url (no list) |
textbox |
search (no list) |
searchbox |
text, search, email, tel, url with list |
combobox |
number |
spinbutton |
range |
slider |
checkbox |
checkbox |
radio |
radio |
button, submit, reset, image |
button |
password, color, date, month, week, time, datetime-local, file |
No corresponding role |
hidden |
None (not exposed) |
Usage notes
- Every input needs a label. Use a
<label>withformatching the input'sid, or wrap the input in the label. Aplaceholderdisappears as soon as the user types and is not a label. - Pick the most specific type.
email,tel,urlandnumbergive mobile users the right keyboard and add free validation. See<input>types. - Numbers that aren't quantities (card numbers, postal codes, phone numbers) should use
type="text"withinputmode="numeric", nottype="number", which adds spinners and drops leading zeros. - Autocomplete helps everyone. Correct
autocompletetokens let browsers and password managers fill forms quickly and reduce typing errors. Usenew-passwordon sign-up forms andcurrent-passwordon sign-in forms. readonlyvsdisabled. A read-only value is focusable and submitted; a disabled one is skipped by the keyboard and not submitted.- Validation is a convenience. Attributes such as
required,patternandmaxstop honest mistakes. Anyone can bypass them, so validate again on the server. - Style validation carefully.
:invalidmatches immediately on page load;:user-invalidonly matches after the user has interacted, which is usually what you want. - Self-closing slash is optional.
<input>and<input />are both fine in HTML; never write</input>.
Technical summary
| Content categories | Flow content, phrasing content; if type is not hidden: interactive content, palpable content; form-associated: listed, labelable (unless hidden), submittable, resettable |
| Permitted content | None; it is a void element |
| Tag omission | Must have a start tag and must not have an end tag |
| Permitted parents | Any element that accepts phrasing content |
| Implicit ARIA role | Depends on type; see the table above |
| DOM interface | HTMLInputElement |
| Default display | inline-block (hidden inputs are not rendered) |
Browser support
Supported in every browser. All the types and attributes above work in current browsers except alpha and colorspace, which are recent additions; the look of pickers (date, time, color, file) differs between browsers and platforms.
Related
<input>types โ everytypevalue in detail<label>โ naming an input<datalist>โ suggestions throughlist<form>โ where the values are sent<textarea>and<select>โ other common controls- Lesson: Input Types
- Lesson: Input Attributes
- Lesson: Form Validation
- Lesson: Forms