Example 1 — Text Input
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Enter your username"> How It Works
type="text" accepts any characters. placeholder shows a hint until the user types.

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.
Single line.
Validated.
Min / max.
Pick one.
Toggle.
required.
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.
<input> is a void element—it has no closing tag. Set attributes on the opening tag: <input type="text" name="username">.
HTML provides several basic input types for common data collection tasks:
For single-line text input such as usernames or search queries.
<input type="text" name="username" placeholder="Enter your username"> For entering passwords—characters are obscured as dots or asterisks.
<input type="password" name="password" placeholder="Enter your password"> For email addresses with built-in format validation in modern browsers.
<input type="email" name="email" placeholder="Enter your email"> For numeric input with optional min, max, and step attributes.
<input type="number" name="age" min="1" max="100"> 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.Input elements are customized with attributes that control behavior, validation, and appearance:
Identifies the field when form data is sent to a server.
<input type="text" name="username"> Sets the default or current value of the field.
<input type="text" value="Default Text"> Provides a hint about what to enter. Use alongside a real label, not instead of one.
<input type="text" placeholder="Enter your name"> Makes the field mandatory before the browser allows form submission.
<input type="text" name="username" required> Limits how many characters the user can type.
<input type="text" maxlength="10"> Set minimum and maximum values for number, date, and similar types.
<input type="number" min="1" max="100"> Specifies a regular expression the value must match before submission.
<input type="text" pattern="[A-Za-z]{3,}" title="At least 3 letters"> 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.
CSS enhances input appearance and usability—width, padding, borders, and focus states:
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.
JavaScript responds to user interactions on input fields:
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.| Type / Attribute | Purpose |
|---|---|
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 |
name | Form submission key |
required | Mandatory field |
placeholder | Hint text (not a label) |
Six examples from a simple text field to a full registration form. Each includes View Output and Try It Yourself.
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Enter your username"> type="text" accepts any characters. placeholder shows a hint until the user types.
<input type="email" name="email" placeholder="you@example.com" required>
<input type="password" name="password" placeholder="Enter password" required> Email validates format on submit. Password hides typed characters. Both use required.
<input type="number" name="age" min="1" max="100" value="25"> Spinner arrows appear in most browsers. Values outside min/max fail validation.
<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> Radios share a name so only one is selected. Checkboxes work independently.
<input type="date" name="birthdate">
<input type="file" name="resume" accept=".pdf,.doc,.docx"> Date shows a native calendar picker. File opens the OS file dialog; accept filters extensions.
Registration form combining text, email, number, date, radio, checkbox, file, and submit:
<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> Each field uses the right type and attributes. Labels improve accessibility and usability.
type for each fieldlabel via for and idrequired and pattern for client-side hints:focus styles for keyboard usersname attribute on submit fieldstype="text" when email or number fitsaccept on file uploads when types matterCore 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.
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.
Bottom line: Use semantic types; test date and file on mobile Safari and Chrome.
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.
Pick right.
CoreAlways.
a11ySubmit key.
FormsValidate.
HTML5Re-check.
SecurityThe 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.
Add text, email, date, and file fields, then preview your registration form live.
6 people found this page helpful