Labels and placeholders help people understand what to type in a form. They make forms easier and faster to fill out.
Labels and placeholders in HTML
<label for="inputId">Label text</label> <input id="inputId" type="text" placeholder="Example text">
The for attribute in <label> must match the id of the input.
The placeholder attribute shows example text inside the input box.
<label for="name">Name:</label> <input id="name" type="text" placeholder="Enter your name">
<label for="email">Email:</label> <input id="email" type="email" placeholder="you@example.com">
<label for="password">Password:</label> <input id="password" type="password" placeholder="At least 8 characters">
This is a simple sign-up form. Each input has a label linked by for and id. Placeholders show example text inside inputs. Small text below inputs gives extra help. The form is easy to read and use.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Labels and Placeholders Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; max-width: 400px; margin: auto; } label { display: block; margin-top: 1rem; font-weight: 600; } input { width: 100%; padding: 0.5rem; margin-top: 0.25rem; font-size: 1rem; border: 1px solid #ccc; border-radius: 4px; } </style> </head> <body> <h1>Sign Up Form</h1> <form> <label for="username">Username</label> <input id="username" type="text" placeholder="Choose a username" aria-describedby="usernameHelp"> <small id="usernameHelp">Your unique username to log in.</small> <label for="email">Email Address</label> <input id="email" type="email" placeholder="you@example.com" aria-describedby="emailHelp"> <small id="emailHelp">We will never share your email.</small> <label for="password">Password</label> <input id="password" type="password" placeholder="At least 8 characters" aria-describedby="passwordHelp"> <small id="passwordHelp">Make it strong and secure.</small> <button type="submit" style="margin-top:1rem; padding:0.5rem 1rem; font-size:1rem;">Sign Up</button> </form> </body> </html>
Always use <label> with the for attribute to improve accessibility.
Placeholders should be short hints, not full instructions.
Labels stay visible, placeholders disappear when typing starts.
Labels describe what the input is for and help screen readers.
Placeholders show example text inside input fields as a hint.
Use both together to make forms clear and easy to use.
Practice
<label> element in an HTML form?Solution
Step 1: Understand the role of <label>
The <label> element connects text to an input field, helping users know what to enter.Step 2: Recognize accessibility benefits
Labels also help screen readers announce the input purpose, making forms accessible.Final Answer:
To describe what the input field is for and improve accessibility -> Option AQuick Check:
Label = Describe input + accessibility [OK]
- Confusing labels with placeholders
- Thinking labels style inputs
- Assuming labels submit forms
for attribute?Solution
Step 1: Check label's for attribute and input's id
The label's for attribute must match the input's id to link them.Step 2: Verify correct syntax
<label for="email">Email</label><input id="email" type="text"> usesfor="email"in label andid="email"in input, which is correct.Final Answer:
<label for="email">Email</label><input id="email" type="text"> -> Option DQuick Check:
Label for = Input id [OK]
- Using for on input instead of id
- Mismatching for and id values
- Using id on label instead of for
<label for="username">Username</label> <input id="username" type="text" placeholder="Enter your username">
Solution
Step 1: Understand placeholder attribute
The placeholder attribute shows example text inside the input as a hint, not actual value.Step 2: Check label vs placeholder roles
The label text appears outside the input box; placeholder text appears inside as a hint.Final Answer:
The text 'Enter your username' inside the input box as a hint -> Option BQuick Check:
Placeholder = hint text inside input [OK]
- Thinking label text appears inside input
- Confusing placeholder with input value
- Expecting input to be pre-filled
<label for="phone">Phone Number</label> <input placeholder="Enter phone number">
Solution
Step 1: Check label's for attribute and input's id
The label'sforattribute value "phone" does not match anyidon the input (input has noid).Step 2: Verify other attributes
Placeholder is valid on input; type defaults totext.Final Answer:
The label's for attribute does not match input's id -> Option AQuick Check:
Label for = Input id [OK]
- Omitting id on input
- Mismatching for and id values
- Using id on label instead of for
Solution
Step 1: Ensure label is linked to input
Usingfor="email"in label and matchingid="email"in input links them for accessibility.Step 2: Use placeholder as helpful hint
The placeholder gives a clear example inside the input without replacing the label.Step 3: Confirm input type is correct
Usingtype="email"helps browsers validate and show correct keyboard on mobile.Final Answer:
<label for="email">Email</label><input id="email" type="email" placeholder="Enter your email address"> -> Option CQuick Check:
Label linked + placeholder hint + correct type = best practice [OK]
- Not linking label and input properly
- Placing label after input without for/id
- Omitting input type or placeholder
Ready to prove it?
Pass the test and this pattern moves unknown
