Definition
The <input> element is a field used in HTML Forms enabling the user input data.
Example
<form action="" method="get" class="form">
<label for="name">Name: </label>
<input type="text" name="name" id="name" />
</form>Usage
- The
<input>element must have an opening and a closing tag. - The
<input>element is one of the most important form elements. - The
typeattribute will determines the type of input field displayed in the form; for example, a button, text field, radio button, or checkbox.
type Attribute
The type attribute affects the type of <input> element. The other attributes available for use by the <input> element will depend on its type.
It takes the following values:
button- A button. The text shown on the button is equal to thevalueattribute.checkbox- A checkbox allowing items to be selected or deselected.color- A control for choosing a color via a color picker.date- A control for entering a date using a date picker.datetime-local- A control for entering a date and time using a date picker and scroll for time.email- A text field for entering an email address with additional validation parameters.file- A control for choosing a file.hidden- A hidden control, but whose value is still submitted to the server.image- A graphical submit button.month- A control for entering a month and year.number- A control for entering a number.password- A control for entering a password. The value of the control will be obscured. Will alert the user if the site is not secured via HTTPS.radio- A radio button used in multiple-choice questions.range- A control that displays as a range widget or slider.reset- A button that resets the form to default values.search- A single-line text field for entering search strings. Line breaks are automatically removed.submit- A button that submits the form. The text shown on the button is equal to thevalueattribute.tel- A control for entering a telephone number.text- This is the default<input>type. It displays a single line of text, with line breaks removed automatically.url- A control for entering an URL. It has additional validation parameters.week- A control for entering a week, consisted of week number and year.
Other Attributes
The <input> element supports the Global Attributes, as well as the following:
acceptUsed with:
fileThe
acceptattribute specifies the file types the user can pick with the control.Values can include:
file_extensionaudio/*video/*image/*media_type
altUsed with:
imageThe
altattribute specifies the alternative text to display if the button image does not load.autocompleteUsed with: All
The
autocompleteattribute specifies whether an element should have autocomplete enabled.Values can include:
onoff
autofocusUsed with: All
The
autofocusattribute is a boolean that specifies the<input>element should get focus automatically when the page loads.captureUsed with:
fileThe
captureattribute specifies the media capture input method, such as a microphone, video, or camera.checkedUsed with:
radio,checkboxThe
checkedattribute is a boolean that specifies whether the<input>control should be pre-selected.dirnameUsed with:
text,searchThe
dirnameattribute enables the submission of the field text’s direction. It takes thenameof the input field followed by “.dir” as the value.disabledUsed with: All
The
disabledattribute specifies the<input>element should be disabled.formUsed with: All
The
formattribute associates the element with a form. The value should match theidattribute of the<form>element.formactionUsed with:
image,submitThe
formactionattribute specifies the URL to submit the form. This will override the form’sactionattribute.formenctypeUsed with:
image,submitThe
formenctypeattribute specifies a string specifying the encoding type to use for submission of form data.Possible values include:
getpost
formmethodUsed with:
image,submitThe
formmethodattribute specifies the HTTP method used to submit a form where thetypeattribute is notbutton.Possible values include:
get- This is where the submitted data is included in the HTTP request body.post- This is where the submitted data is appended to a formsactionURL with a?. This is often used for search forms.
If specified, it overrides the
methodattribute of the<form>element.formnovalidateUsed with:
image,submitThe
formnovalidateattribute is a Boolean attribute. It specifies whether to validate the form when submitted.If specified, it overrides the
novalidateattribute of the<form>element.formtargetUsed with:
image,submitThe
formtargetattribute specifies where to display the response after submitting the form where thetype="submit".Values can include:
_self_blank_parent_self_top- Frame, tab, or window name.
If specified, it overrides the
targetattribute of the<form>element.heightUsed with:
imageThe
heightattribute specifies the height in pixels of the<input>element.listUsed with: Most
The
listattribute specifies theidof the<datalist>element that contains a list pre-defined options for an<input>element.maxUsed with: numeric types
The
maxattribute specifies the maximum value for an<input>element.Values can include:
- A number
- A date
maxlengthUsed with:
password,search,tel,text,urlThe
maxlengthattribute specifies the maximum number of characters allowed.minUsed with: numeric types
The
minattribute specifies the minimum value for an<input>element.Values can include:
- A number
- A date
minlengthUsed with:
password,search,tel,text,urlThe
minlengthattribute specifies the minimum number of characters allowed.multipleUsed with:
email,fileThe
multipleattribute specifies that more than one value may be entered.nameUsed with: All
The
nameattribute specifies the name of the<input>element.patternUsed with:
password,text,telThe
patternattribute specifies a regular expression that the value of the<input>element will be validated against.placeholderUsed with:
password,search,tel,text,urlThe
placeholderattribute specifies an expected value that will display in the<input>field.readonlyUsed with: Most
The
readonlyattribute is a boolean that specifies that the<input>element is read-only.requiredUsed with: Most
The
requiredattribute is a boolean that specifies that the<input>element is required.sizeUsed with:
email,password,tel,textThe
sizeattribute specifies the width of the<input>element in characters.srcUsed with:
imageThe
srcattribute specifies the URL of the image to be used.stepUsed with: numeric types
The
stepattribute specifies an interval in numbers of an<input>field.valueUsed with: All
The
valueattribute specifies the value of an<input>element.widthUsed with:
imageThe
widthattribute specifies the height in pixels of the<input>element.
Best Practices
- You can use the
:validand:invalidCSS pseudo-classes to style a<form>element based their validation state. For example:input:invalid { background-color: red; } input:valid { background-color: green; }
Browser Support
Desktop
| Chrome | Edge | Firefox | IE | Opera | Safari |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Mobile
| Android Webview | Chrome Android | Firefox Android | Opera Android | iOS Safari | Samsung Internet |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
