Form Control Attributes

Beyond type and name, form controls accept attributes that set default values, give hints, help browsers autofill, choose the right on-screen keyboard and control editing. Used well, they make forms much faster and less frustrating to fill in.

value and placeholder

Try it yourself
Result

value is a real pre-filled value that will be submitted. placeholder is a hint shown only while the field is empty. Placeholders aren't labels: they vanish as soon as someone types, often have low contrast, and are skipped by some assistive technologies. Use them for examples of format, alongside a proper label.

autocomplete

The autocomplete attribute tells the browser exactly what a field is for, so it can offer saved names, addresses, emails and passwords — saving users a lot of typing:

Try it yourself
Result
ValueField
name, given-name, family-nameNames
email, tel, usernameContact and account
address-line1, address-line2, address-level2 (city), postal-code, country — or street-address on a multi-line <textarea>Address
current-password, new-password, one-time-codeLogin and security
cc-name, cc-number, cc-exp, cc-cscPayment cards
bday, organizationOther personal details
offDon't suggest anything (for unique values like a search for a new product code)

new-password lets password managers suggest a strong password; current-password lets them fill a saved one.

inputmode and enterkeyhint

On phones, inputmode chooses the on-screen keyboard without changing validation, and enterkeyhint changes the label on the Enter key:

Try it yourself
Result

Values include numeric, decimal, tel, email, url and search.

readonly and disabled

Try it yourself
Result
readonlydisabled
User can editNoNo
Can be focused and copiedYesNo
Submitted with the formYesNo
LooksNormalGreyed out

Size and length

Try it yourself
Result

size and cols set the visible width in characters (CSS width is usually better). maxlength stops typing at a limit; minlength is checked on submit.

Focus and spelling

  • autofocus focuses a field when the page loads. Use it only when the form is clearly the page's sole purpose (like a search page) — otherwise it disorients screen-reader users.
  • spellcheck="false" turns off spell-checking for codes, usernames and email addresses.
  • autocapitalize="off" stops phones capitalising the first letter of usernames.

Controls outside the form

The form attribute associates a control with a form by its id, even if the control sits elsewhere in the page:

Try it yourself
Result

Buttons can also override the form's settings with formaction, formmethod and formnovalidate — handy for a "Save draft" button next to "Publish".

Try it yourself

Try it yourself
Result