HTML Inputs

Beginner
⏱️ 13 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
input / form / label

Introduction

HTML provides a wide variety of input types that let users interact with web forms. From basic text fields to date pickers and file uploads, inputs are essential for collecting data and building interactive pages.

This tutorial covers common input types, attributes, validation, styling, events, best practices, and six hands-on examples you can run in the editor.

What You’ll Learn

01

text

Single line.

02

email

Validated.

03

number

Min / max.

04

radio

Pick one.

05

checkbox

Toggle.

06

validate

required.

What Are HTML Inputs?

HTML inputs are interactive elements that let users enter data in a web form. They are created with the <input> tag and support many type values, each designed for a specific kind of data.

Inputs usually live inside a <form> element and should always be paired with a <label> for accessibility. See the input tag reference for the full attribute list.

💡
Beginner Tip

<input> is a void element—it has no closing tag. Set attributes on the opening tag: <input type="text" name="username">.

Basic Input Types

HTML provides several basic input types for common data collection tasks:

Text

For single-line text input such as usernames or search queries.

html
<input type="text" name="username" placeholder="Enter your username">

Password

For entering passwords—characters are obscured as dots or asterisks.

html
<input type="password" name="password" placeholder="Enter your password">

Email

For email addresses with built-in format validation in modern browsers.

html
<input type="email" name="email" placeholder="Enter your email">

Number

For numeric input with optional min, max, and step attributes.

html
<input type="number" name="age" min="1" max="100">

Other Common Types

  • type="date" — date picker for birthdates and deadlines.
  • type="radio" — one choice from a group (same name).
  • type="checkbox" — independent on/off toggles.
  • type="file" — file upload control.
  • type="submit" — button that submits the form.

Attributes of Input Elements

Input elements are customized with attributes that control behavior, validation, and appearance:

name

Identifies the field when form data is sent to a server.

html
<input type="text" name="username">

value

Sets the default or current value of the field.

html
<input type="text" value="Default Text">

placeholder

Provides a hint about what to enter. Use alongside a real label, not instead of one.

html
<input type="text" placeholder="Enter your name">

required

Makes the field mandatory before the browser allows form submission.

html
<input type="text" name="username" required>

maxlength

Limits how many characters the user can type.

html
<input type="text" maxlength="10">

min and max

Set minimum and maximum values for number, date, and similar types.

html
<input type="number" min="1" max="100">

pattern

Specifies a regular expression the value must match before submission.

html
<input type="text" pattern="[A-Za-z]{3,}" title="At least 3 letters">

Form Validation

HTML5 includes built-in validation that checks data before submission:

  • required — ensures the field is not left empty.
  • type — validates format (email, url, number).
  • pattern — uses a regular expression for custom rules.
  • min / max — bounds numeric and date values.

Browser validation improves UX but is not a security measure. Always validate again on the server.

Styling Input Elements

CSS enhances input appearance and usability—width, padding, borders, and focus states:

css
input {
  width: 100%;
  max-width: 320px;
  padding: 10px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}

input:focus {
  outline: 2px solid #2563eb;
  border-color: #2563eb;
}

Use :focus styles so keyboard users can see which field is active. Match your site’s design system for consistent forms.

Handling Input Events

JavaScript responds to user interactions on input fields:

js
const field = document.getElementById("username");

field.addEventListener("input", function () {
  console.log("Current value:", field.value);
});

field.addEventListener("change", function () {
  console.log("Committed value:", field.value);
});
  • input — fires on every keystroke or change.
  • change — fires when the user commits a new value (blur or Enter).
  • focus / blur — when the field gains or loses focus.

⚡ Quick Reference

Type / AttributePurpose
type="text"Single-line text
type="password"Hidden characters
type="email"Email with validation
type="number"Numeric values
type="date"Date picker
type="radio"One of a group
type="checkbox"On/off toggle
nameForm submission key
requiredMandatory field
placeholderHint text (not a label)

Examples Gallery

Six examples from a simple text field to a full registration form. Each includes View Output and Try It Yourself.

Example 1 — Text Input

html
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Enter your username">
Try It Yourself

How It Works

type="text" accepts any characters. placeholder shows a hint until the user types.

Example 2 — Email and Password

html
<input type="email" name="email" placeholder="you@example.com" required>
<input type="password" name="password" placeholder="Enter password" required>
Try It Yourself

How It Works

Email validates format on submit. Password hides typed characters. Both use required.

Example 3 — Number Input

html
<input type="number" name="age" min="1" max="100" value="25">
Try It Yourself

How It Works

Spinner arrows appear in most browsers. Values outside min/max fail validation.

Example 4 — Radio and Checkbox

html
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="checkbox" id="subscribe" name="subscribe" value="newsletter">
<label for="subscribe">Subscribe</label>
Try It Yourself

How It Works

Radios share a name so only one is selected. Checkboxes work independently.

Example 5 — Date and File

html
<input type="date" name="birthdate">
<input type="file" name="resume" accept=".pdf,.doc,.docx">
Try It Yourself

How It Works

Date shows a native calendar picker. File opens the OS file dialog; accept filters extensions.

Example 6 — Complete Inputs Form

Registration form combining text, email, number, date, radio, checkbox, file, and submit:

html
<form>
  <label for="username">Username</label>
  <input type="text" id="username" name="username" required>

  <label for="email">Email</label>
  <input type="email" id="email" name="email" required>

  <label for="age">Age</label>
  <input type="number" id="age" name="age" min="0" max="120">

  <label for="birthdate">Birthdate</label>
  <input type="date" id="birthdate" name="birthdate">

  <p>Gender:
    <input type="radio" name="gender" value="male"> Male
    <input type="radio" name="gender" value="female"> Female
  </p>
  <p><input type="checkbox" name="subscribe"> Subscribe</p>
  <input type="file" name="resume">
  <input type="submit" value="Register">
</form>
Try It Yourself

How It Works

Each field uses the right type and attributes. Labels improve accessibility and usability.

Best Practices

✅ Do

  • Choose the correct type for each field
  • Pair every input with a label via for and id
  • Use required and pattern for client-side hints
  • Validate again on the server before saving data
  • Add visible :focus styles for keyboard users

❌ Don’t

  • Use placeholder as the only label
  • Skip the name attribute on submit fields
  • Rely on browser validation alone for security
  • Use type="text" when email or number fits
  • Forget accept on file uploads when types matter

Universal Browser Support

Core types (text, password, checkbox, radio, submit) work everywhere. Newer types like date, email, and number are supported in all modern browsers; very old browsers may fall back to plain text.

Baseline · Since HTML

HTML5 input types

Core types (text, password, checkbox, radio, submit) work everywhere. Newer types like date, email, and number are supported in all modern browsers; very old browsers may fall back to plain text.

98% Modern browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML5 input types Modern browsers

Bottom line: Use semantic types; test date and file on mobile Safari and Chrome.

Conclusion

HTML input elements are versatile tools for collecting user data. By choosing the right types, attributes, and validation rules—and pairing fields with labels—you build forms that are clear, accessible, and easy to use.

Next, style text inside labels and messages with HTML Form to wrap fields and submit data, or explore the input tag reference and form tag reference.

Key Takeaways

🏷️ 02

label

Always.

a11y
🔑 03

name

Submit key.

Forms
04

required

Validate.

HTML5
🔒 05

Server

Re-check.

Security

❓ Frequently Asked Questions

The input element creates an interactive form control where users enter data. It is self-closing and uses the type attribute to define behavior—text field, password, checkbox, date picker, and more.
Both accept typed characters, but email adds browser validation for a valid email format and may show an email keyboard on mobile. Use email when you expect an address.
When a form is submitted, the name becomes the key sent to the server. Without name, the field value is not included in the submission data.
It makes the field mandatory. The browser blocks form submission and shows a message if the user leaves it empty. Combine with server-side validation for security.
No. Placeholders disappear when the user types and are not reliably announced by screen readers. Always use a visible label linked with for and id.
Radio buttons with the same name let the user pick one option from a group. Checkboxes are independent toggles—each can be checked or unchecked separately.
Did you know?

The inputmode attribute (separate from type) hints which keyboard to show on mobile—e.g. inputmode="numeric" on a text field for PIN codes without triggering number spinners.

Build a form in the editor

Add text, email, date, and file fields, then preview your registration form live.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful