<input> Tag in HTML

Quick facts

CategoryFlow content, phrasing content, interactive content (except type="hidden")
Content modelEmpty — it’s a void element with no children
Tag omissionVoid element — no end tag, ever
Permitted parentsAny element that accepts phrasing content
DOM interfaceHTMLInputElement

Syntax

<input type="text" name="fieldname">

Live example

Edit the code below and watch the preview update instantly.

index.html
Preview

Attributes

AttributeDescription
typeThe kind of control — text, email, password, checkbox, radio, number, date, file, and more
nameThe key this field’s value is submitted under
valueThe control’s current or default value
placeholderFaint hint text shown when the field is empty
requiredBlocks form submission until the field is filled in
disabledGreys out the control and excludes it from submission
min / max / patternConstrain numeric ranges or match a text pattern for validation

Common examples

An email field with built-in validation

The browser checks the format and shows the email keyboard on phones.

<label for="email">Email</label> <input type="email" id="email" name="email" placeholder="you@example.com" required>

Checkboxes and a radio group

Radio buttons that share a name form one group, so only one can be chosen.

<label><input type="checkbox" name="news"> Send me news</label> <label><input type="radio" name="size" value="s"> Small</label> <label><input type="radio" name="size" value="m" checked> Medium</label>

Numbers and dates

Number fields accept limits, and date fields give visitors a calendar picker.

<label for="qty">Quantity</label> <input type="number" id="qty" name="qty" min="1" max="10" step="1" value="1"> <label for="day">Delivery date</label> <input type="date" id="day" name="day">

<input> vs <textarea>

An <input> is a single-line field, and its type turns it into many specialised controls: email, password, number, date, checkbox, slider and more. A <textarea> is a multi-line box for longer free text and has no type.

Choose an input with the most specific type you can — it gives visitors validation and the right mobile keyboard for free. Choose a textarea when people need to write more than one line, such as a message, review or address notes. Note that an input sets its starting text with the value attribute, while a textarea puts it between its tags.

<input><textarea>
Lines of textOneMany
Types20+ (email, date, checkbox…)None
Default textvalue=”…”Between the tags

Common mistakes to avoid

  • Using the placeholder as the label. It disappears when typing starts and is often low-contrast. Add a real <label>.
  • Using type=”text” for everything. The right type (email, tel, number, url) gives free validation and the right mobile keyboard.
  • Giving radio buttons the same id. Every id must be unique; it’s the name that should be shared within a group.
  • Forgetting name. A field without a name is not included when the form is sent.

Accessibility & SEO tips

  • Add autocomplete values (email, name, tel, street-address…) so browsers can fill forms for visitors.
  • Use inputmode to fine-tune the mobile keyboard, e.g. inputmode="numeric" for codes that aren’t real numbers.
  • Mark invalid fields with aria-invalid="true" and explain the problem in text that screen readers can reach.
  • Group related checkboxes or radio buttons in a <fieldset> with a <legend> so the question is read together with the options.

Browser support

Supported in every browser, including Chrome, Edge, Firefox, Safari and their mobile versions — it has been part of HTML since the very first versions. All modern input types work in current browsers; older browsers simply treat unknown types as text.

Related tags

Frequently asked questions

How many input types are there?
Over 20, including text, email, password, number, date, checkbox, radio, range, color, and file — each renders a purpose-built browser control.
How do I make a field mandatory?
Add the required boolean attribute. The browser will block form submission and show a prompt until the field is filled in.
How do I style placeholder text?
Use the ::placeholder pseudo-element in CSS, for example input::placeholder { color: #999; }. Keep enough contrast that it is still readable.
What is the difference between disabled and readonly?
A readonly field can be focused and copied, and its value is submitted. A disabled field can't be focused, looks greyed out, and its value is not submitted with the form.
How do I limit how many characters can be typed?
Add maxlength, for example maxlength="50". Use minlength for a minimum. Both work on text-like types such as text, email, password and tel.
How do I set a default value in an input?
Add the value attribute, for example <input name="city" value="London">. For checkboxes and radio buttons, add the checked attribute to the option that should be selected by default.