HTML Inputs — Deep Dive
The <input> element is the most versatile
tag in all of HTML — one self-closing tag with over 20 different type values and dozens of attributes. In
this chapter you'll explore every key attribute, all input states, the <datalist> element for autocomplete suggestions, the <output> element, and how to style inputs professionally with CSS.
name,
id, value, placeholder, autocomplete
<datalist> element
for autocomplete suggestions<output> element for
calculated resultsThe <input> Element — Anatomy
The <input> element is
self-closing — it has no content and no closing tag. All its behaviour is controlled
through attributes. Here are all the key attributes labelled:
<label for>
to this input
email=user@mail.com
id is used by the browser and CSS/JS — it links the label, targets the
element in JavaScript, and anchors CSS styles. name is what gets sent to
the server — without it, the field's value is never submitted. You need both: id for front-end, name for back-end.
Key Input Attributes — Complete Reference
Here is a complete reference of every important attribute the <input> element supports — beyond what was covered in Chapter 10:
| Attribute | Purpose | Example |
|---|---|---|
| type | Defines the input behaviour and UI widget — text, email, password, number, date, etc. | type="email" |
| name | The key submitted to the server. Without it the value is never sent. Use snake_case or camelCase consistently. | name="user_email" |
| id | Unique identifier on the page — links <label for>,
enables CSS targeting, used by JavaScript. |
id="email" |
| value | Sets the pre-filled default value of the input. On submit, this is the value sent to the server. | value="India" |
| placeholder | Ghost hint text — shown when the field is empty, disappears on typing. Not a substitute for a label. | placeholder="e.g. Rahul" |
| autocomplete | Tells the browser what kind of data to offer for autofill. Values: on, off, name, email, tel, new-password, etc. |
autocomplete="email" |
| list | Links the input to a <datalist> by its id — provides autocomplete suggestion dropdown. |
list="cities" |
| form | Associates an input placed outside a <form> tag with a
form by that form's id. |
form="myForm" |
| autofocus | Boolean — automatically focuses this field when the page loads. Use on at most one input per page. | autofocus |
| spellcheck | Controls browser spell-checking on text inputs. Set false for
code inputs or name fields. |
spellcheck="false" |
| tabindex | Controls the order in which Tab key moves focus between elements. 0 = natural order, -1 = skip.
|
tabindex="2" |
| inputmode | Hints the mobile keyboard type — numeric, decimal, tel, email, url, search. |
inputmode="numeric" |
All 22 HTML5 Input Types — Complete Reference
HTML5 defines 22 distinct type values for <input>. Here is a complete reference grouped by category:
| type="..." | Category | What it Does / Renders | Mobile Keyboard |
|---|---|---|---|
| text | Text | Single-line plain text — the default when type is omitted | Standard |
| Text | Validates email format on submit; browser auto-checks for @ symbol | Shows @ key | |
| password | Text | Masks characters with dots; browser may offer password manager | Standard |
| search | Text | Styled like text but semantically a search field; may show clear × button | Shows Search key |
| url | Text | Validates that input starts with a valid URL scheme (http://, https://) | Shows . / : keys |
| tel | Text | No format validation — use pattern. Opens numeric dial pad on mobile | Numeric dial pad |
| number | Numeric | Numeric spinner with up/down arrows; supports min, max, step | Numeric keyboard |
| range | Numeric | Horizontal slider for selecting a value within a range | N/A |
| date | Date & Time | Calendar date picker — returns YYYY-MM-DD format | Date wheel |
| time | Date & Time | Time picker — returns HH:MM format (24-hour or 12-hour by locale) | Time wheel |
| datetime-local | Date & Time | Combined date and time picker — no timezone info | Combined wheel |
| month | Date & Time | Month and year picker — returns YYYY-MM format | Month wheel |
| week | Date & Time | Week and year picker — returns YYYY-W## format | Week wheel |
| checkbox | Selection | Independent on/off toggle — multiple can be selected simultaneously | N/A |
| radio | Selection | Mutually exclusive — only one in a named group can be selected | N/A |
| file | File | File browser dialog; use accept to restrict types; requires enctype="multipart/form-data" | File browser |
| color | Picker | OS colour picker — returns hex value e.g. #e44d26 | Colour wheel |
| hidden | Hidden | Not visible — value submitted silently with form (e.g. CSRF token, user ID) | N/A |
| submit | Button | Renders a submit button — same as <button type="submit">
|
N/A |
| reset | Button | Renders a reset button that clears all form fields to defaults | N/A |
| button | Button | Renders a clickable button — no default behaviour, attach JS onclick | N/A |
| image | Button | Renders an image that acts as a submit button; uses src and alt attributes | N/A |
<!-- Date picker -->
<input type="date" name="dob" min="1990-01-01" max="2010-12-31">
<!-- Time picker -->
<input type="time" name="start_time" min="09:00" max="18:00">
<!-- Date + Time (no timezone) -->
<input type="datetime-local" name="event_at">
<!-- Search input -->
<input type="search" name="q" placeholder="Search courses...">
<!-- URL input -->
<input type="url" name="website" placeholder="https://yoursite.com">
<!-- File — restrict to images only -->
<input type="file" name="avatar" accept="image/*" multiple>
Input States — Default, Focus, Valid, Invalid, Disabled
Every input field can be in one of several visual and functional states at any time. Understanding and styling these states is essential for building professional, accessible forms.
/* Default */
input {
border: 1px solid #d1d5db;
border-radius: 8px;
padding: 10px 14px;
font-size: 15px;
transition: border-color 0.2s, box-shadow 0.2s;
}
/* Focus — always show a visible focus ring */
input:focus {
outline: none;
border-color: #e44d26;
box-shadow: 0 0 0 3px rgba(228, 77, 38, 0.15);
}
/* Valid — only show after user has interacted */
input:not(:placeholder-shown):valid {
border-color: #22c55e;
background: rgba(34, 197, 94, 0.04);
}
/* Invalid — only show after user has interacted */
input:not(:placeholder-shown):invalid {
border-color: #ef4444;
background: rgba(239, 68, 68, 0.04);
}
/* Disabled */
input:disabled {
opacity: 0.5;
cursor: not-allowed;
background: #f3f4f6;
}
/* Read-only */
input:read-only {
background: #f9fafb;
color: #6b7280;
cursor: default;
}
/* Placeholder colour */
input::placeholder {
color: #9ca3af;
font-size: 15px;
}
The <datalist> Element — Autocomplete
Suggestions
<datalist> provides a list of predefined
suggestions for a text input. Unlike a <select>, the user can
still type anything — the datalist just offers suggestions as a dropdown. Link it to an input using the
list attribute matching the datalist's id.
<datalist>
<!-- Input links to datalist via list="id" -->
<label for="city">City</label>
<input
type="text"
id="city"
name="city"
list="city-options"
placeholder="Type to search..."
>
<!-- datalist must have a unique id -->
<datalist id="city-options">
<option value="Vadodara">
<option value="Ahmedabad">
<option value="Surat">
<option value="Mumbai">
<option value="Delhi">
</datalist>
<datalist> when the user should be able to type freely but you
want to suggest options — like a city name search. Use <select>
when the user must choose from a fixed list of options and free text is not allowed.
The <output> Element — Calculated
Results
The <output> element displays the result of
a calculation or user action. It is semantically linked to form inputs using the for attribute and is updated via JavaScript. A common use case is a live
price calculator or range slider display.
<form>
<label for="budget">Monthly Budget:</label>
<input
type="range"
id="budget"
name="budget"
min="5000"
max="50000"
step="1000"
value="15000"
oninput="result.value = '₹' + this.value"
>
<!-- for links output to the input(s) it reflects -->
<output id="result" for="budget">₹15000</output>
</form>
Styling Inputs with CSS
Browsers apply default styling to inputs that varies across platforms. Here is a live demo of styled inputs and the CSS behind them:
Styled Input Demo — try clicking each field
/* Modern accent-color for checkbox and radio */
input[type="checkbox"],
input[type="radio"] {
accent-color: #e44d26;
width: 18px;
height: 18px;
cursor: pointer;
}
/* Style all text-like inputs together */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"] {
width: 100%;
padding: 11px 14px;
font-size: 15px;
border: 1.5px solid #d1d5db;
border-radius: 8px;
background: #fff;
color: #111827;
transition: border-color 0.2s, box-shadow 0.2s;
font-family: inherit;
}
input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus {
outline: none;
border-color: #e44d26;
box-shadow: 0 0 0 3px rgba(228, 77, 38, 0.15);
}
/* Style the range slider track (Chrome/Safari) */
input[type="range"] {
accent-color: #e44d26;
width: 100%;
height: 6px;
cursor: pointer;
}
Common Mistakes — Do's & Don'ts
These are the most frequent input-related mistakes beginners make:
<!-- Both name AND id on every input -->
<label for="phone">Phone</label>
<input
type="tel"
id="phone"
name="phone"
inputmode="numeric"
pattern="[0-9]{10}"
autocomplete="tel"
>
<!-- type="email" for email fields -->
<input type="email" name="email">
<!-- Missing name — value never submitted -->
<input
type="tel"
id="phone"
>
<!-- Using type="text" for email -->
<input
type="text"
name="email"
placeholder="Email"
>
<!-- loses email validation + mobile keyboard -->
outline: none or outline: 0 on an input without replacing it with a custom focus ring.
Removing the focus outline makes the form completely unusable for keyboard users and people with motor
disabilities — failing WCAG 2.4.7 accessibility requirements.
Complete Example — Inputs Showcase
A single page demonstrating all key input types, states, datalist, and the output element together:
<form action="/submit" method="POST">
<!-- Text with autocomplete hint -->
<label for="name">Full Name</label>
<input type="text" id="name" name="name"
autocomplete="name" required autofocus>
<!-- Email -->
<label for="email">Email</label>
<input type="email" id="email" name="email"
autocomplete="email" required>
<!-- Date with min/max -->
<label for="dob">Date of Birth</label>
<input type="date" id="dob" name="dob"
min="1980-01-01" max="2010-12-31">
<!-- Datalist autocomplete -->
<label for="city">City</label>
<input type="text" id="city" name="city"
list="city-list" placeholder="Type to search...">
<datalist id="city-list">
<option value="Vadodara">
<option value="Ahmedabad">
<option value="Surat">
</datalist>
<!-- Range + output -->
<label for="fee">Budget: <output for="fee" id="feeOut">₹10000</output></label>
<input type="range" id="fee" name="fee"
min="5000" max="50000" step="1000" value="10000"
oninput="feeOut.value='₹'+this.value">
<!-- Hidden field (CSRF token) -->
<input type="hidden" name="_csrf" value="abc123token">
<button type="submit">Submit</button>
</form>
Try It Yourself
Edit the inputs below and click Open in Editor to experiment with all input types and the datalist live in full screen.
:invalid CSS. Type a valid one — it turns green using :valid. Then drag the range slider and watch the <output> update live. No JavaScript framework needed!
Quick Check — Chapter 11
Expand each question to check your understanding before moving on to the multiple choice quiz.
id is a client-side identifier — it must be unique
on the page and is used by the browser to link a <label for>
to the input, target the element with CSS, and access it with JavaScript via document.getElementById(). name is
the server-side key — it is the key included in the form submission data (e.g. email=user@mail.com). Without name,
the field's value is never sent to the server. You need both: id for front-end work, name for
back-end submission.
autocomplete attribute tells the browser what type of
data to suggest from the user's saved autofill data. When set to a specific token like email, name, tel, or street-address, the browser
knows exactly which saved value to offer. Key values: on (default,
browser decides), off (disables autofill), new-password (signals a new password field — prevents autofill of saved
passwords), current-password, email, tel, name, given-name, family-name. Correct hints significantly speed up form filling for
users.
<select> is a strict dropdown — the user can
only choose from the predefined options and cannot type freely. <datalist> is an autocomplete suggestion list —
it offers suggestions as the user types, but they can still enter any value they want. Use <datalist> for search-style inputs where you want to suggest
common options but allow custom entries (city names, tag names). Use <select> when the user must choose from a fixed list of valid
options.
<output> element is a semantic container for the
result of a calculation or user action. It is typically linked to one or more input elements
using its for attribute (matching the input's id). It is updated dynamically via JavaScript — commonly via oninput on a range slider or calculation form. Screen readers announce
it as a live region. It is purely presentational in terms of the result display, but semantically
correct for showing derived values.
outline: none without providing a custom
alternative makes the form completely unusable for them. WCAG 2.4.7 requires that keyboard focus is
always visually discernible. Always replace the default outline with a custom focus ring (box-shadow, coloured border, or outline with offset).
inputmode hints to mobile browsers which keyboard layout to
show without changing the input's type or validation behaviour. For example, inputmode="numeric" shows a number pad on mobile for a type="text" OTP field — giving you a number keyboard without the
browser's built-in number validation (spinners, min/max). Common values: numeric (digits 0–9), decimal
(numbers with decimal point), tel (phone dial pad), email (@ key visible), url (/ and .
keys visible), search (Return shows Search). Use it whenever the
mobile keyboard for type="text" isn't optimal for your expected
input.
<input> determines
the key sent to the server on form submission?<input> to a <datalist> for autocomplete suggestions?name= server key (must be present for submission).id= front-end identifier (links label, enables CSS & JS). Always include both on every input.value= pre-filled default.placeholder= ghost hint (not a label).autocomplete= autofill hint for the browser.- HTML5 defines 22 input types — each triggers the correct mobile keyboard and browser validation behaviour. Always use the most specific type.
- Input states: default, focus, valid (
:valid), invalid (:invalid), disabled (not submitted), readonly (submitted). Style all states visibly. <datalist>provides autocomplete suggestions while still allowing free-text input. Link vialist="id". Different from<select>which forces a strict choice.<output>semantically displays a calculated or derived result — update it with JavaScript, typically via anoninputhandler on a range or number input.- Never remove the focus outline without replacing it. Use
inputmodeto show the right mobile keyboard without changing validation behaviour.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.