HTML Input Attributes


Lesson 30 of 58

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.

Value Attribute Example

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.

Readonly Attribute Example

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.

Disabled Attribute Example
Note: 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.

Size Attribute Example

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.

Maxlength Attribute Example

The min and max Attributes

The min and max attributes set the minimum and maximum values for number, range, date, and time inputs.

Min and Max Attributes Example

The placeholder Attribute

The placeholder attribute shows a hint inside the input field that disappears when the user starts typing.

Placeholder Attribute Example

The required Attribute

The required attribute makes an input field mandatory. The form cannot be submitted until the field is filled out.

Required Attribute Example

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.

Pattern Attribute Example

The step Attribute

The step attribute specifies the legal number intervals. For example, step=”3″ means the value must be multiples of 3.

Step Attribute Example

The autofocus Attribute

The autofocus attribute automatically puts the cursor in the specified field when the page loads.

Autofocus Attribute Example

The multiple Attribute

The multiple attribute allows users to select multiple files when uploading or enter multiple email addresses.

Multiple Attribute Example

The list and datalist Attributes

The list attribute connects an input field to a <datalist> element, providing auto-complete suggestions as the user types.

Datalist Example

The autocomplete Attribute

The autocomplete attribute lets the browser predict and suggest previously entered values.

Autocomplete Example

Quick Summary

  • value – Sets an initial/default value for the field
  • readonly – Field cannot be changed but value is submitted
  • disabled – Field is inactive and value is NOT submitted
  • size – Visible width in characters
  • maxlength – Maximum number of characters allowed
  • min and max – Minimum and maximum values for numbers/dates
  • placeholder – Hint text inside the field
  • required – Field must be filled out before submitting
  • pattern – Regular expression that the input must match
  • step – Legal number intervals (e.g., step=”3″)
  • autofocus – Automatically focus on this field when page loads
  • multiple – Allow multiple file selections or multiple emails
  • list – Connects to a datalist for suggestions
  • autocomplete – 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.
Tip: Always validate data on the server too. These attributes make the user experience better, but they can be bypassed. Never trust client-side validation alone.

HTML Input Attributes Reference

Attribute Description
valueSpecifies an initial value for an input field
readonlyMakes the input field read-only (value is submitted)
disabledDisables the input field (value is NOT submitted)
sizeSpecifies the width of an input field (in characters)
maxlengthSets the maximum number of characters allowed
min and maxSets the minimum and maximum values for numbers/dates
placeholderShows a hint text inside the input field
requiredMakes the field mandatory before form submission
patternSpecifies a regular expression that the input must match
stepSpecifies the legal number intervals
autofocusAutomatically focuses on the field when the page loads
multipleAllows multiple file selections or multiple emails
listConnects to a datalist for auto-complete suggestions
autocompleteSpecifies whether the browser should predict input

Lesson 30 of 58