<input>
Definition
The HTML <input>
element is a self-closing tag used to create various form controls that allow users to enter data. It is one of the most commonly used elements in HTML forms.
The <input>
element has several types, including text, password, checkbox, radio, submit, reset, button, email, url, date, and more. Each type corresponds to a different input control that allows users to provide different types of data. For example, the text type is used for text input fields, while the checkbox type is used for checkboxes.
Examples
Here's an example of a simple text input field created using the <input> element:
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your name">
In this example, the input field has a type of "text", an ID of "name", a name of "name", and a placeholder of "Enter your name". When the user types their name into the field and submits the form, the value of the input field will be sent to the server with a name of "name".
Attributes
This element's attributes include the global attributes.
Attribute | Description | Deprecated? |
---|---|---|
accept | Specifies the types of files that are accepted by the server when the user submits a form with a file input control | No |
alt | Specifies an alternative text for an image input control | No |
autocomplete | Specifies whether the browser should autocomplete the input value. Possible values are "on" and "off" | No |
autofocus | Specifies that the input control should have focus when the page loads | No |
checked | Specifies that the input control should be checked by default. Used for checkbox and radio input controls | No |
disabled | Specifies that the input control should be disabled and cannot be edited or selected by the user | No |
form | Specifies the id of the form the input control belongs to. Used for associating inputs outside the form tag | No |
formaction | Specifies the URL to submit the form data to when the input control is used as a submit button | No |
formenctype | Specifies the encoding type to use when the form data is submitted. Used in conjunction with the formaction attribute | No |
formmethod | Specifies the HTTP method to use when the form data is submitted. Used in conjunction with the formaction attribute | No |
formnovalidate | Specifies that the form data should not be validated when the input control is used as a submit button | No |
formtarget | Specifies the target window or frame to open the response to the form submission in. Used in conjunction with the formaction attribute | No |
height | Specifies the height of an image input control | No |
list | Specifies the id of a datalist element that provides a list of suggestions for the input control | No |
max | Specifies the maximum value for an input control. Used for number, date, and time input controls | No |
maxlength | Specifies the maximum number of characters that can be entered in an input control. Used for text and password input controls | No |
min | Specifies the minimum value for an input control. Used for number, date, and time input controls | No |
multiple | Specifies that the user can select multiple files when using a file input control | No |
name | Specifies the name of the input control, which is used to identify the input value when the form is submitted | No |
pattern | Specifies a regular expression pattern that the input value must match. Used for text and password input controls | No |
placeholder | Specifies a short hint that describes the expected input value for the input control | No |
readonly | Specifies that the input value cannot be edited by the user | No |
required | Specifies that the input control must have a value before the form can be submitted | No |
size | Specifies the width of the input control in characters. Used for text and password input controls | No |
src | Specifies the URL of an image input control | No |
step | Specifies the increment or decrement amount for an input control. Used for number, date, and time input controls | No |
type | Specifies the type of input control to display, such as text, password, checkbox, radio, submit, reset, button, email, url, date, and more | No |
value | Specifies the default value for an input control. Used for text, password, and hidden input controls | No |
width | Specifies the width of an image input control | No |
Best Practices
- Depending on the information you want the user to provide, make sure to use the correct type attribute. For example, use
text
for regular text inputs,email
for email addresses,number
for numerical input, andpassword
for password input. - Always associate the input element with a label element using the
for
attribute. This helps users with disabilities who may use screen readers to navigate your website. - If the input field is expecting a certain format or pattern, provide a default value or a placeholder to guide the user.
- Make sure to validate the user's input on the server side. Client-side validation using JavaScript can be useful for providing immediate feedback to the user, but it should not be relied upon as the only form of validation.
- Use the
autocomplete
attribute to help users complete forms more quickly. For example, useautocomplete='email'
for an email input field. - Use the
required
attribute only when necessary, as it can be frustrating for users to encounter an error message after filling out a form. - For date inputs, consider using a date picker to ensure that users enter dates in the correct format.
- Use the
pattern
attribute to enforce a specific pattern or format for user input. For example, usepattern='[0-9]{3}-[0-9]{2}-[0-9]{4}'
for a social security number input field. - Use the
minlength
andmaxlength
attributes to specify the minimum and maximum length of input, respectively. - For numerical input fields, consider using a range input to allow users to select a value within a specific range.
Accessibility Considerations
- Always associate the input element with a label element using the
for
attribute, and make sure the label text clearly describes the input's purpose. - Use the
aria-describedby
attribute to provide additional context for the input, such as instructions or hints. - Use the appropriate type attribute for each input field, as some assistive technologies may have difficulty interpreting certain types of input.
- Make sure there is a visible focus indicator around the input element when it receives focus, as some users with disabilities may rely on keyboard navigation.
- If an error occurs, make sure the error message is clear and describes how to fix the error.
- Ensure there is enough contrast between the input and its background, as low contrast can make it difficult for users with low vision to read.
- If an image is used within the input, make sure to provide a descriptive alt text that conveys its purpose.
- Ensure that users can navigate and interact with input fields using only the keyboard, as some users may not be able to use a mouse or other pointing device.
- Use the
autocomplete
attribute to help users with disabilities complete forms more quickly. - Test your input fields with a screen reader to ensure they are compatible and can be easily understood by users with visual impairments.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |