HTML 5 <input> Tag
The HTML <input>
tag is used within a form to declare an input element - a control that allows the user to input data.
Demo
Attributes
HTML tags can contain one or more attributes. Attributes are added to a tag to provide the browser with more information about how the tag should appear or behave. Attributes consist of a name and a value separated by an equals (=) sign, with the value surrounded by double quotes. Here's an example, style="color:black;"
.
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
The attributes that you can add to this tag are listed below.
Element-Specific Attributes
The following table shows the attributes that are specific to this tag/element.
Attribute | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
accept | Specifies a comma-separated list of content types that the server accepts.
Possible values:
You can specify both any MIME types and any corresponding extensions. For example, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
alt | Alternate text. This specifies text to be used in case the browser/user agent can't render the input control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
autocomplete | Prevents the browser from using "autocomplete" with this control. Can be useful for sensitive data, such as banking details etc.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
autofocus | Automatically gives focus to this control when the page loads. This allows the user to start using the control without having to select it first. There must not be more than one element in the document with the autofocus attribute specified.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
checked | The input control will already be selected when the page loads. Used with type="radio" or type="checkbox" . |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
disabled | Disables the input control. The control won't accept changes from the user. It also cannot receive focus and will be skipped when tabbing.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dirname | Determines the direction of the text as submitted in the input field. The value of this attribute can be a string of text such as a name for the field. For example, if you specify the attribute as dirname="text_dir" , once the form has been submitted, the data might look like this: text_dir=ltr .
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
form | Specifies the ID of a form to which this control belongs.
Possible values: [The ID of a form element in the element's owner |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formaction | Specifies the URL of the file that will process the control when submitted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formenctype | Specifies the content type used to encode the form data set when it's submitted to the server.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formmethod | Specifies the HTTP method to use when the control is submitted.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formnovalidate | Specifies that the form is not to be validated during submission.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formtarget | Specifies the browsing context to load the destination indicated in the action attribute.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
height | Specifies the height of the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputmode | Specifies what kind of input mechanism would be most helpful for users entering content into the form control.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
list | Specifies a datalist element to use for a list of predefined options for the user. The value must be the ID of a datalist element in the same document. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
max | Specifies the maximum value for the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
maxlength | Specifies the maximum number of characters that the user is allowed to enter into the text field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
min | Specifies the minimum value for the control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
minlength | Specifies a lower bound on the number of characters a user can input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
multiple | Specifies whether the user is allowed to enter more than one value.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name | Assigns a name to the input control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pattern | Specifies a regular expression against which the control's value is to be checked. Value must match the Pattern production of ECMA 262's grammar. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
placeholder | Specifies a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be a sample value or a brief description of the expected format.
Note: For a longer hint or other advisory text, the title global attribute is more appropriate. Also note that the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
readonly | Sets the input control to read-only - it won't allow the user to change the value. The control however, can receive focus and are included when tabbing through the form controls.
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
required | Specifies that the input field is a required field (the user must complete this field).
This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
size | Specifies the width of the control, in characters. Value that is a valid non-negative integer greater than zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
src | If type="image" , this attribute specifies the location of the image. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
step | Specifies the granularity that is expected (and required) of the value, by limiting the allowed values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type | Specifies the type of control.
Possible values:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value | Specifies a default value for the control. Note: If type="checkbox" or type="radio" this attribute is required. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
width | Specifies the width of the control. |
Global Attributes
The following attributes are standard across all HTML 5 tags.
- accesskey
- class
- contenteditable
- contextmenu
- dir
- draggable
- dropzone
- hidden
- id
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- lang
- spellcheck
- style
- tabindex
- title
- translate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handler Content Attributes
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.
Here are the standard HTML 5 event handler content attributes.
- onabort
- oncancel
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onformchange
- onforminput
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onpause
- onplay
- onplaying
- onprogress
- onratechange
- onreadystatechange
- onscroll
- onseeked
- onseeking
- onselect
- onshow
- onstalled
- onsubmit
- onsuspend
- ontimeupdate
- onvolumechange
- onwaiting
For a full explanation of these attributes, see HTML 5 event handler content attributes.