By WebNest Studio Engineering Team
HTML Tutorial
HTML Input Types and Labels
The <input> element is the most versatile form control in HTML, and its behavior changes entirely based on its type attribute. Choosing the right input type is not cosmetic — it changes the on-screen keyboard shown on mobile devices, enables built-in validation, and improves accessibility.
Why it matters
Choosing the right input type gives you a great deal for free: a phone shows a numeric keypad for tel and an @ key for email, a date field opens a calendar, and the browser checks the format. A label tied to its input is the single most important thing for form accessibility, and its absence is one of the most common faults found in audits.
Common Input Types
HTML defines many input types beyond the generic text box, each tailored to a specific kind of data.
text— a single-line free text field.email— validates a basic email address pattern and shows an email-optimized mobile keyboard.password— masks entered characters.number— accepts only numeric input, often paired with step, min, and max.checkbox— an independent on/off toggle; multiple checkboxes with the same name can all be checked at once.radio— one selection from a mutually exclusive group; radios sharing the same name attribute form a single group.date,tel,url,search,file— specialized inputs for dates, phone numbers, URLs, search queries, and file uploads.
Associating Labels with Inputs
Every input needs an associated <label> to be usable and accessible. The most reliable way is connecting a label's for attribute to the input's id — this lets clicking the label text focus (or toggle) the input, and lets screen readers announce the label when the input receives focus. Wrapping the input directly inside the label element is a valid alternative that doesn't require matching id/for values.
Example
Various input types with properly associated labels
<label for="email">Email Address</label>
<input type="email" id="email" name="email">
<label for="age">Age</label>
<input type="number" id="age" name="age" min="1" max="120">
<fieldset>
<legend>Preferred Contact Method</legend>
<label><input type="radio" name="contact" value="email" checked> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
Renders an email field with an email-style mobile keyboard, a number field restricted between 1 and 120, and a radio button group where selecting one option automatically deselects the other.
Common Mistakes
- Using type="text" for every field instead of purpose-built types like email, number, or tel.
- Leaving out <label> elements entirely, relying only on placeholder text, which disappears once the user starts typing and isn't reliably read by all screen readers.
- Giving radio buttons in the same logical group different name attributes, breaking the mutual-exclusivity behavior.
- Mismatching a label's for attribute with the input's id (or a typo in either), silently breaking the association.
Exercise
Add four labelled controls to the form: an email field named email; a number field named age that accepts 18 to 99; a checkbox named newsletter with the label Send me news; and two radio buttons named plan with the values free and pro.
Expected result: four labelled controls; only one of the two radio buttons can be selected at a time.
<form>
<!-- TODO: email -->
<!-- TODO: age, 18 to 99 -->
<!-- TODO: newsletter checkbox -->
<!-- TODO: plan radio buttons: free and pro -->
</form>
Show hints
- Radio buttons form one group, where only one can be chosen, when they share the same
name. - A range for a number is set with
minandmax.
Show solution
<form>
<p>
<label for="email">Email</label>
<input type="email" id="email" name="email">
</p>
<p>
<label for="age">Age</label>
<input type="number" id="age" name="age" min="18" max="99">
</p>
<p>
<input type="checkbox" id="newsletter" name="newsletter">
<label for="newsletter">Send me news</label>
</p>
<p>
<input type="radio" id="plan-free" name="plan" value="free">
<label for="plan-free">Free</label>
<input type="radio" id="plan-pro" name="plan" value="pro">
<label for="plan-pro">Pro</label>
</p>
</form>
Quick quiz
1. How is a label connected to its input?
- A. By placing them on the same line
- B. The label's name matches the input's name
- C. The label's for attribute matches the input's id
- D. By giving both the same class
Show answer
C. Alternatively the input can be placed inside the label element.
2. What makes several radio buttons act as one group?
- A. Being in the same paragraph
- B. Having the same id
- C. Having the same value
- D. Having the same name
Show answer
D. Within one name, selecting a radio button deselects the others.
3. Why is a placeholder not a substitute for a label?
- A. It disappears when the user types, has low contrast and is not reliably announced
- B. It is not supported by browsers
- C. It is submitted with the form
- D. It only works for passwords
Show answer
A. A placeholder is a hint about the format, used in addition to a visible label.
Interview questions
Why does every form control need a label?
Show answer
A label gives the control an accessible name, so a screen reader announces what the field is for when it receives focus. Clicking or tapping the label also focuses or toggles the control, which enlarges the target, a real help for small checkboxes and radio buttons on touch screens. A field with no label is announced only as "edit text", leaving the user to guess.
What is the benefit of using specific input types such as email, tel and date?
Show answer
The browser adapts to the type. Mobile devices show a keyboard suited to the data, such as digits for tel and number. Built-in validation checks the format of email and url. Types such as date and color provide a native picker. Password managers and autofill also use the type, together with the autocomplete attribute, to fill in fields correctly.
Key Points to Remember
- The type attribute determines an input's behavior, validation, and mobile keyboard.
- Radio buttons sharing the same name form one mutually exclusive group; checkboxes are independent.
- Always associate a label with its input via matching for/id, or by wrapping the input in the label.
- Use fieldset and legend to group and describe related controls like a set of radio buttons.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering