Tutorials HTML HTML Inputs
Ch 11 / 40
Chapter 11 Beginner 9 min read HTML

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.

What you'll learn
Every key attribute — name, id, value, placeholder, autocomplete
Complete reference of all 22 HTML5 input types
Input states — default, focus, valid, invalid, disabled, readonly
The <datalist> element for autocomplete suggestions
The <output> element for calculated results
Styling inputs with CSS — focus ring, states, custom checkbox & radio

The <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:

<input type="email" id="email" name="email" value="" placeholder="you@example.com" autocomplete="email" required >
type Controls the input UI and browser validation behaviour
id Unique identifier — links <label for> to this input
name The key sent to the server — e.g. email=user@mail.com
value The current value of the input — pre-filled or empty
placeholder Ghost text shown when input is empty — disappears on type
autocomplete Hints the browser which saved data to suggest for autofill
required Boolean attribute — blocks submit if field is empty
name vs id
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:

Ghost hint text — shown when the field is empty, disappears on typing. Not a substitute for a label.
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 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
email 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
HTML — Date, time, search, url, week examples
<!-- 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 (Idle)
The resting state. No interaction has occurred. Shown with the default border and background colour.
Focus (:focus)
The field is active and receiving keyboard input. Show a clear focus ring — critical for keyboard and screen reader accessibility.
Valid (:valid)
The field's value passes validation rules (e.g. correct email format, within min/max range). Apply a green border to signal success.
Invalid (:invalid)
The value fails validation. Apply a red border and an error message. Only show after the user has interacted — not on page load.
Disabled
Field is completely inactive — greyed out, not focusable, not submitted. Use when a field is temporarily unavailable based on another selection.
Read-only
User can see and copy the value but not edit it. The value IS submitted with the form. Use for auto-filled reference numbers or pre-confirmed data.
CSS — Styling input states
/* 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.

💡 Start typing in the field above — your browser shows matching suggestions from the <datalist>
HTML — datalist for autocomplete
<!-- 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 vs select
Use <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.

₹15,000
HTML — output element with range slider
<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

CSS — Custom checkbox and radio button
/* 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:

Do — Correct
<!-- 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">
Don't — Wrong
<!-- 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 -->
Never Remove Focus Styles
Never write 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:

HTML — Complete inputs showcase
<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.

Live Editor
✎ Editor
👁 Preview
Try This
Type an invalid email in the Email field — notice the red border from :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!
Free Resource
HTML5 Cheat Sheet — All Tags & Attributes
One printable PDF covering all 22 input types, every attribute, states, datalist, output, and CSS styling rules.
Download PDF
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React, and Node.js in one industry-ready curriculum with real projects and placement support.
120+
Chapters
40 hrs
Content
10
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js MongoDB AI Tools

Quick Check — Chapter 11

Expand each question to check your understanding before moving on to the multiple choice quiz.

Questions & Answers
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.
The 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.
The <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.
The focus outline is the only visual indicator for keyboard-only users that shows which element is currently active. People who cannot use a mouse — due to motor disabilities, or those navigating with a keyboard or screen reader — rely entirely on the focus ring to know where they are on the page. Removing it with 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.
Multiple Choice Questions
MCQ 1 of 4
Which attribute on <input> determines the key sent to the server on form submission?
MCQ 2 of 4
How do you link an <input> to a <datalist> for autocomplete suggestions?
MCQ 3 of 4
Which CSS pseudo-class styles an input only when it contains a value that passes validation?
MCQ 4 of 4
Which input type is used to display an OS colour picker and returns a hex value?
Chapter Summary
  • 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 via list="id". Different from <select> which forces a strict choice.
  • <output> semantically displays a calculated or derived result — update it with JavaScript, typically via an oninput handler on a range or number input.
  • Never remove the focus outline without replacing it. Use inputmode to show the right mobile keyboard without changing validation behaviour.
~9 min read
Beginner level
Chapter 11 of 40

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
If this tutorial helped you, a 5-star Google review means a lot. It helps other learners discover our free content and motivates us to keep improving.
500+ Reviews (Target)
Write a Google Review
Join WhatsApp Channel