<input>
A form field: text, email, password, number, date, slider, checkbox, file and many more.
- Display
- inline-block
- Closing tag
- None, void element
- Permitted content
- None, the element is void
Example
#<style>
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.fields label { display: grid; gap: 4px; }
.fields .inline { display: flex; align-items: center; gap: 6px; }
.fields fieldset { border: 0; margin: 0; padding: 0; }
.fields legend { padding: 0; margin-bottom: 4px; }
</style>
<form class="fields">
<label>First name <input type="text" name="name" placeholder="e.g. Ania"></label>
<label>Email <input type="email" name="email" placeholder="ania@example.com"></label>
<label>Password <input type="password" name="password"></label>
<label>Age <input type="number" name="age" min="7" max="99" value="16"></label>
<label>Study minutes per day <input type="range" name="minutes" min="10" max="120" step="10" value="30"></label>
<label>Course start <input type="date" name="start"></label>
<label>Theme color <input type="color" name="accent" value="#8b5cf6"></label>
<label>Search courses <input type="search" name="q" placeholder="HTML, CSS…"></label>
<label>Profile picture <input type="file" name="avatar" accept="image/*"></label>
<label class="inline"><input type="checkbox" name="newsletter" checked> I want to receive the newsletter</label>
<fieldset>
<legend>Level</legend>
<label class="inline"><input type="radio" name="level" value="beginner" checked> Beginner</label>
<label class="inline"><input type="radio" name="level" value="advanced"> Advanced</label>
</fieldset>
</form>Definition and usage
#The <input> element is the most versatile form element there is. Its appearance and behavior depend on the type attribute: the same <input> can be a text field, a password field, a slider, a color picker, a checkbox or a button for uploading files. It’s a void element, so it has no closing tag.
Picking the right type is a real convenience. On a phone, type="email" shows a keyboard with an @ key, type="tel" a numeric keypad, and type="date" a native calendar. The browser also checks the format on its own: an email field won’t let through an address without an @, and a number field will reject a value outside the min and max range.
Every field needs a <label>, because the text from the placeholder attribute disappears the moment you start typing and doesn’t replace a label. For a field’s value to reach the server, the field must have a name attribute.
Syntax
#<input type="text" name="name">Attributes
#text | email | password | number | range | date | time | datetime-local | month | week | color | checkbox | radio | file | search | tel | url | hidden | submit | reset | button | image
The kind of field,textby default. The type determines the appearance, the keyboard shown on a phone, and the validation rules. Ahiddenfield is invisible and stores a value sent along with the form. A type the browser doesn’t support falls back to a plain text field. That’s the case formonthandweek, for example, which don’t work in every browser.text
The name the field’s value is sent to the server under, for exampleemail=ania@example.com. Fields with nonamearen’t sent. Radio buttons with the same name form a group in which only one option can be selected.text
The field’s initial value. In text fields it’s the text filled in at the start, in checkboxes and radio buttons the value sent once checked, and insubmitandbuttonbuttons the caption on the button.text
A hint visible in an empty field, ideally a sample data format. It disappears once the user starts typing, so it never replaces a<label>.boolean attribute
The field must be filled in (or checked) before the form submits. If you try to submit an empty field, the browser shows a message and places the cursor in it. A field that meets the rules matches the:validselector, and an invalid one matches:invalid.boolean attribute
Disables the field: you can’t click it, tab to it or edit it, and its value won’t be sent with the form. The browser usually grays out a field like this.boolean attribute
A read-only field: you can select it, copy text out of it and tab to it, but you can’t change its value. Unlikedisabled, the value will be sent. It works on text, number and date fields, but not on checkboxes, radio buttons, sliders or color fields.boolean attribute
Checks a checkbox or selects a radio button from the start. In a group of radio buttons with the same name, at most one should havechecked.number, date or time
Set the range and the step of values innumber,range,date,timeand related fields.step="50"allows only values in increments of 50 (0, 50, 100 and so on), andstep="any"removes the step restriction. The arrows and the slider stick to these limits, and a value outside the range fails validation.number of characters
The smallest and largest number of characters allowed in a text field.maxlengthsimply won’t let you type more, whileminlengthis checked when the form is submitted. An empty field with norequiredpasses this validation.regular expression
A pattern the field’s entire value must match, for example a postal code in the00-000format. The browser matches the pattern against the whole text, so you don’t need to add^and$. Put a description of the required format next to the field or in thetitleattribute, which some browsers add to the error message.boolean attribute
Allows several values at once: in afilefield, pick several files, and in anemailfield, enter several addresses separated by commas.MIME types or file extensions
Hints to the file picker what to show, for exampleimage/*(any images),.pdforimage/png, image/jpeg. It’s only a filter in the dialog that the user can bypass, so check the file type on the server anyway.id of a `<datalist>` element
Connects the field to a<datalist>of suggestions. The user sees proposals while typing, but can still type their own value.on | off | the name of the data, e.g. email, username, current-password, new-password, one-time-code
Tells the browser and the password manager what kind of data fits the field. Withcurrent-password, the manager suggests the saved password; withnew-password, it offers to generate a strong one; andone-time-codelets you paste a code from a text message with one tap. In a login form, mark the field with the login asusername, even if what you type into it is an email.boolean attribute
Places the cursor in the field right after the page loads. Use it sparingly, at most once per page, and only where the field is the main reason for the visit, like a search box. A sudden jump in focus disorients screen reader users.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#type
text | email | password | number | range | date | time | datetime-local | month | week | color | checkbox | radio | file | search | tel | url | hidden | submit | reset | button | imageThe kind of field, text by default. The type determines the appearance, the keyboard shown on a phone, and the validation rules. A hidden field is invisible and stores a value sent along with the form. A type the browser doesn’t support falls back to a plain text field. That’s the case for month and week, for example, which don’t work in every browser.
<form>
<p><label>Phone <input type="tel" name="phone" placeholder="600 100 200"></label></p>
<p><label>Website <input type="url" name="website" placeholder="https://"></label></p>
<p><label>Lesson time <input type="time" name="time" value="18:30"></label></p>
<p><label>Deadline <input type="datetime-local" name="deadline"></label></p>
<p><label>Starting month <input type="month" name="month"></label></p>
<input type="hidden" name="course" value="html-101">
<p>
<input type="submit" value="Send">
<input type="reset" value="Clear">
<input type="button" value="Preview">
</p>
</form>name
textThe name the field’s value is sent to the server under, for example email=ania@example.com. Fields with no name aren’t sent. Radio buttons with the same name form a group in which only one option can be selected.
<p>How do you want to learn?</p>
<label><input type="radio" name="mode" value="video"> Video</label>
<label><input type="radio" name="mode" value="text"> Text</label>
<label><input type="radio" name="mode" value="practice"> Exercises</label>value
textThe field’s initial value. In text fields it’s the text filled in at the start, in checkboxes and radio buttons the value sent once checked, and in submit and button buttons the caption on the button.
<p><label>Username <input type="text" name="username" value="ania_codes"></label></p>
<p><label><input type="checkbox" name="topics" value="css" checked> CSS</label></p>
<input type="submit" value="Save changes">placeholder
textA hint visible in an empty field, ideally a sample data format. It disappears once the user starts typing, so it never replaces a <label>.
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone" placeholder="e.g. 600 100 200">required
boolean attributeThe field must be filled in (or checked) before the form submits. If you try to submit an empty field, the browser shows a message and places the cursor in it. A field that meets the rules matches the :valid selector, and an invalid one matches :invalid.
<form>
<label>Email <input type="email" name="email" required></label>
<button>Send</button>
</form>disabled
boolean attributeDisables the field: you can’t click it, tab to it or edit it, and its value won’t be sent with the form. The browser usually grays out a field like this.
<p><label>Promo code <input type="text" name="promo" value="FALL2026" disabled></label></p>
<p><label><input type="checkbox" name="certificate" disabled> Certificate (after finishing the course)</label></p>readonly
boolean attributeA read-only field: you can select it, copy text out of it and tab to it, but you can’t change its value. Unlike disabled, the value will be sent. It works on text, number and date fields, but not on checkboxes, radio buttons, sliders or color fields.
<label>Your referral link
<input type="url" name="referral" value="https://www.codeworlds.com/?ref=ania" size="34" readonly>
</label>checked
boolean attributeChecks a checkbox or selects a radio button from the start. In a group of radio buttons with the same name, at most one should have checked.
<p><label><input type="checkbox" name="reminders" checked> Study reminders</label></p>
<p>
<label><input type="radio" name="plan" value="monthly"> Monthly</label>
<label><input type="radio" name="plan" value="yearly" checked> Yearly</label>
</p>min / max / step
number, date or timeSet the range and the step of values in number, range, date, time and related fields. step="50" allows only values in increments of 50 (0, 50, 100 and so on), and step="any" removes the step restriction. The arrows and the slider stick to these limits, and a value outside the range fails validation.
<p><label>Number of participants <input type="number" name="seats" min="1" max="10" value="1"></label></p>
<p><label>Budget in USD <input type="range" name="budget" min="0" max="500" step="50" value="150"></label></p>
<p><label>Deadline <input type="date" name="date" min="2026-10-01" max="2026-12-31"></label></p>minlength / maxlength
number of charactersThe smallest and largest number of characters allowed in a text field. maxlength simply won’t let you type more, while minlength is checked when the form is submitted. An empty field with no required passes this validation.
<form>
<label>Username
<input type="text" name="username" minlength="3" maxlength="16" required>
</label>
<button>Create account</button>
</form>pattern
regular expressionA pattern the field’s entire value must match, for example a postal code in the 00-000 format. The browser matches the pattern against the whole text, so you don’t need to add ^ and $. Put a description of the required format next to the field or in the title attribute, which some browsers add to the error message.
<form>
<label>Postal code
<input type="text" name="postal" pattern="[0-9]{2}-[0-9]{3}" placeholder="00-000" title="Two digits, a dash and three digits">
</label>
<button>Check</button>
</form>multiple
boolean attributeAllows several values at once: in a file field, pick several files, and in an email field, enter several addresses separated by commas.
<p><label>Project screenshots <input type="file" name="screens" accept="image/*" multiple></label></p>
<p><label>Invite friends <input type="email" name="friends" size="34" placeholder="ola@example.com, kuba@example.com" multiple></label></p>accept
MIME types or file extensionsHints to the file picker what to show, for example image/* (any images), .pdf or image/png, image/jpeg. It’s only a filter in the dialog that the user can bypass, so check the file type on the server anyway.
<p><label>Avatar (PNG or JPG) <input type="file" name="avatar" accept="image/png, image/jpeg"></label></p>
<p><label>Resume in PDF <input type="file" name="cv" accept=".pdf,application/pdf"></label></p>list
id of a `<datalist>` elementConnects the field to a <datalist> of suggestions. The user sees proposals while typing, but can still type their own value.
<label>Favorite language
<input type="text" name="language" list="languages">
</label>
<datalist id="languages">
<option value="JavaScript">
<option value="Python">
<option value="TypeScript">
<option value="Go">
</datalist>autocomplete
on | off | the name of the data, e.g. email, username, current-password, new-password, one-time-codeTells the browser and the password manager what kind of data fits the field. With current-password, the manager suggests the saved password; with new-password, it offers to generate a strong one; and one-time-code lets you paste a code from a text message with one tap. In a login form, mark the field with the login as username, even if what you type into it is an email.
<form>
<p><label>Email <input type="email" name="email" autocomplete="username"></label></p>
<p><label>Password <input type="password" name="password" autocomplete="current-password"></label></p>
<p><label>Code from text message <input type="text" name="code" inputmode="numeric" autocomplete="one-time-code"></label></p>
</form>autofocus
boolean attributePlaces the cursor in the field right after the page loads. Use it sparingly, at most once per page, and only where the field is the main reason for the visit, like a search box. A sudden jump in focus disorients screen reader users.
<label>Search lessons <input type="search" name="q" autofocus></label>In a preview embedded in a frame, the browser may ignore autofocus.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Connect every field to a label: wrap it in
<label>, or give the field anidand point to it in<label for="…">. Clicking the label will then place the cursor in the field. - Don’t use
type="number"for phone numbers, postal codes or card numbers. This type adds arrows for increasing the value and is meant for numbers you do arithmetic with, not for strings of digits. Choosetype="tel"ortype="text"withinputmode="numeric". - Browser validation (
required,pattern,min) helps the user, but doesn’t protect the server. Always validate data on the server too.
Related entries
#- <label>A form field’s label. Clicking the label moves the cursor to the field.
- <form>A form that collects data from the user and sends it to a given address.
- <button>A clickable button: submits a form, clears it, or runs an action on the page.
- <datalist>A list of suggestions for an `<input>` field. You can pick a proposal or type your own value.
- <textarea>A multi-line text field for longer input: messages, reviews, descriptions.
- <select>A dropdown list the user picks one or more options from.
Spotted a mistake or missing an example? Write to us.