HTML <input> Tag

The HTML <input> tag represents a form input control in an HTML document. A form input control is a control that allows the user to input data and interact with a website or application.

Syntax

The <input> tag is written as <input> (no end tag). An <input> tag is typically given a type attribute to specify the type of control, and a name attribute so that the form processor can make a reference to it. Often a value attribute is used to specify the default value of the form control.

There are a lot of other attributes that can be used with the <input> tag. These are listed below under "Template".

Here's a basic example of the syntax for the <input> tag with some attributes:

Here's an example that contains three <input> elements within a typical form:

Examples

Basic Form

Here's a form that is used to collect the user's name details. Three <input> tags are used: two for the name details, one for the submit button.

The form Attribute

It is possible to associate the <input> element with a form by using the <input> element's form attribute.

If using the <input> element's form attribute, you must specify the id of the form you wish to associate the element with.

In the following example, I've placed all the form-associated elements outside of the <input> element. But I've purposely associated only two elements with the form (to demonstrate the effect of the form attribute). The first name has been associated with a form but the last name has not. Therefore, the last name is not submitted with the form and the action page doesn't acknowledge the users' last name.

Radio Buttons

In this example, we create radio buttons. We do this by giving the type attribute a value of radio. Each radio button in a group must have the same name value - that's how they are associated. The value is what distinguishes the different options.

Form-Associated Elements

The <input> tag is referred to as a form associated element. This is because it can have a form-owner.

Here are the form-associated elements in HTML.

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <input> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <input> tag/element.

AttributeDescription
acceptSpecifies a comma-separated list of content types that the server accepts.

Possible values:

  • audio/*
  • video/*
  • image/*
  • [A valid MIME type, with no parameters]
  • [A string whose first character is a "." (U+002E) character] (for specifying a file extension)

You can specify both any MIME types and any corresponding extensions. For example, <input type="file" accept=".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">

altAlternate text. This specifies text to be used in case the browser/user agent can't render the input control.
autocompletePrevents the browser from using "autocomplete" with this control. Can be useful for sensitive data, such as banking details etc.

Possible values:

  • on (default value)
  • off
autofocusAutomatically 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 autofocus or autofocus="autofocus").

Possible values:

  • [Empty string]
  • autofocus
checkedThe input control will already be selected when the page loads. Used with type="radio" or type="checkbox".
disabledDisables 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 disabled or disabled="disabled").

Possible values:

  • [Empty string]
  • disabled
dirnameDetermines 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.

formSpecifies the ID of a form to which this control belongs.

Possible values:

[The ID of a form element in the element's owner Document]

formactionSpecifies the URL of the file that will process the control when submitted.
formenctypeSpecifies the content type used to encode the form data set when it's submitted to the server.

Possible values:

  • application/x-www-form-urlencoded (default)
  • multipart/form-data (use this when uploading files)
  • text/plain (use this when uploading files)
formmethodSpecifies the HTTP method to use when the control is submitted.

Possible values:

  • get (the form data is appended to the URL when submitted)
  • post (the form data is not appended to the URL)
  • dialog (Closes the dialog box in which the form finds itself, if any, and otherwise does not submit.)
formnovalidateSpecifies 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 novalidate or novalidate="novalidate").

Possible values:

  • [Empty string]
  • novalidate
formtargetSpecifies the browsing context to load the destination indicated in the action attribute.

Possible values:

  • _blank
  • _self
  • _top
  • _parent
heightSpecifies the height of the control.
listSpecifies 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.
maxSpecifies the maximum value for the control.
maxlengthSpecifies the maximum number of characters that the user is allowed to enter into the text field.
minSpecifies the minimum value for the control.
minlengthSpecifies a lower bound on the number of characters a user can input.
multipleSpecifies 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 multiple or multiple="multiple").

Possible values:

  • [Empty string]
  • multiple
nameAssigns a name to the input control.
patternSpecifies a regular expression against which the control's value is to be checked. Value must match the Pattern production of ECMA 262's grammar.
placeholderSpecifies 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 placeholder attribute should not be used as an alternative to the <label> element.

readonlySets 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 readonly or readonly="readonly").

Possible values:

  • [Empty string]
  • readonly
requiredSpecifies 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 required or required="required").

Possible values:

  • [Empty string]
  • required
sizeSpecifies the width of the control, in characters. Value that is a valid non-negative integer greater than zero.
srcIf type="image", this attribute specifies the location of the image.
stepSpecifies the granularity that is expected (and required) of the value, by limiting the allowed values.
typeSpecifies the type of control.

Possible values:

ValueData TypeControl Type
hiddenAn arbitrary stringN/A
textText with no line breaksText field
searchText with no line breaksText field
telText with no line breaksText field
urlAn absolute URLText field
emailAn email address or list of email addressesText field
passwordText with no line breaks (sensitive information)Text field that obscures data entry (eg, hides the password by using asterisks (******) or similar)
datetimeA date and time (year, month, day, hour, minute, second, fraction of a second) with the time zone set to UTCDate and time control
dateA date (year, month, day) with no time zoneDate control
monthA date consisting of a year and a month with no time zoneA month control
weekA date consisting of a week-year number and a week number with no time zoneA week control
timeA time (hour, minute, seconds, fractional seconds) with no time zoneA time control
datetime-localA date and time (year, month, day, hour, minute, second, fraction of a second) with no time zoneDate and time control
numberA numerical valueText field or spinner control
rangeA numerical value, with the extra semantic that the exact value is not importantSlider control or similar
colorAn sRGB color with 8-bit red, green, and blue componentsA color well. Enables the user to select a color.
checkboxA set of zero or more values from a predefined listCheckbox
radioAn enumerated valueRadio button
fileZero or more files each with a MIME type and optionally a file nameA label and a button
submitAn enumerated value, with the extra semantic that it must be the last value selected and initiates form submissionButton
imageA coordinate, relative to a particular image's size, with the extra semantic that it must be the last value selected and initiates form submissionEither a clickable image, or a button
resetN/AButton
buttonN/AButton
valueSpecifies a default value for the control. Note: If type="checkbox" or type="radio" this attribute is required.
widthSpecifies the width of the control.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <input> tag , as well as with all other HTML tags.

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.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <input> element, as well as any other HTML5 element.

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

The <input> element has a number of differences between HTML 4 and HTML5.

In HTML5, the usemap, ismap, and align attributes are obsolete (they were supported in HTML 4).

HTML5 introduced the following attributes:

Also, in HTML5 the global title attribute has special meaning on the <input> element.

HTML 5.1 and the WHATWG HTML Living Standard introduced the following attribute:

But the attribute was later dropped from both specs.

To see more detail on the two versions see HTML5 <input> Tag and HTML4 <input> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <input> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <input> Tag and HTML4 <input> Tag.

Tag Details

For more details about the <input> tag, see HTML5 <input> Tag and HTML4 <input> Tag.

Specifications

Here are the official specifications for the <input> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.