HTML input elements can have many attributes that control how they behave. These attributes can set default values, restrict input, add validation, and improve user experience.
The value Attribute
The value attribute sets an initial (default) value for an input field. This value appears when the page loads and can be changed by the user.
The readonly Attribute
The readonly attribute makes an input field read-only. Users can see and copy the value, but cannot change it. The value is still submitted with the form.
The disabled Attribute
The disabled attribute makes an input field disabled. Users cannot click on it or change it. The value is NOT submitted with the form.
readonly vs disabled: The value of a readonly field is submitted. The value of a disabled field is NOT submitted.
The size Attribute
The size attribute specifies the visible width of an input field in characters. It works with text, search, tel, url, email, and password fields.
The maxlength Attribute
The maxlength attribute limits the maximum number of characters a user can enter. The field will not accept more than the specified number.
The min and max Attributes
The min and max attributes set the minimum and maximum values for number, range, date, and time inputs.
The placeholder Attribute
The placeholder attribute shows a hint inside the input field that disappears when the user starts typing.
The required Attribute
The required attribute makes an input field mandatory. The form cannot be submitted until the field is filled out.
The pattern Attribute
The pattern attribute uses a regular expression to validate the input. The form will not submit if the input does not match the pattern.
The step Attribute
The step attribute specifies the legal number intervals. For example, step=”3″ means the value must be multiples of 3.
The autofocus Attribute
The autofocus attribute automatically puts the cursor in the specified field when the page loads.
The multiple Attribute
The multiple attribute allows users to select multiple files when uploading or enter multiple email addresses.
The list and datalist Attributes
The list attribute connects an input field to a <datalist> element, providing auto-complete suggestions as the user types.
The autocomplete Attribute
The autocomplete attribute lets the browser predict and suggest previously entered values.
Quick Summary
value– Sets an initial/default value for the fieldreadonly– Field cannot be changed but value is submitteddisabled– Field is inactive and value is NOT submittedsize– Visible width in charactersmaxlength– Maximum number of characters allowedminandmax– Minimum and maximum values for numbers/datesplaceholder– Hint text inside the fieldrequired– Field must be filled out before submittingpattern– Regular expression that the input must matchstep– Legal number intervals (e.g., step=”3″)autofocus– Automatically focus on this field when page loadsmultiple– Allow multiple file selections or multiple emailslist– Connects to a datalist for suggestionsautocomplete– Browser prediction on/off
What Just Happened? Let Me Explain
- readonly vs disabled: Readonly fields are like printed text. You can see it but not change it. Disabled fields are like grayed out options. The big difference is that readonly values get sent to the server, disabled values do not.
- size vs maxlength: Size controls how wide the field looks. Maxlength controls how many characters the user can actually type.
- placeholder vs value: Placeholder is hint text that disappears when typing. Value is actual data that stays until changed.
- pattern: Regular expressions can seem complicated, but they are very powerful. [A-Za-z]{3} means “exactly three letters, uppercase or lowercase”.
- autofocus: Use this carefully. Only one field per page should have autofocus. Having multiple can confuse users.
HTML Input Attributes Reference
| Attribute | Description |
|---|---|
value | Specifies an initial value for an input field |
readonly | Makes the input field read-only (value is submitted) |
disabled | Disables the input field (value is NOT submitted) |
size | Specifies the width of an input field (in characters) |
maxlength | Sets the maximum number of characters allowed |
min and max | Sets the minimum and maximum values for numbers/dates |
placeholder | Shows a hint text inside the input field |
required | Makes the field mandatory before form submission |
pattern | Specifies a regular expression that the input must match |
step | Specifies the legal number intervals |
autofocus | Automatically focuses on the field when the page loads |
multiple | Allows multiple file selections or multiple emails |
list | Connects to a datalist for auto-complete suggestions |
autocomplete | Specifies whether the browser should predict input |