Creating a Simple Form with Labels and Placeholders
📖 Scenario: You are building a simple contact form for a website. The form needs clear labels and placeholders to help users understand what information to enter.
🎯 Goal: Create a form with two input fields: one for the user's name and one for their email. Each input must have a proper label and a placeholder text inside the input box.
📋 What You'll Learn
Use semantic HTML form elements
Add a label for each input field
Use the for attribute in labels linked to input ids
Add placeholder text inside each input field
Ensure accessibility by linking labels and inputs correctly
💡 Why This Matters
🌍 Real World
Forms are everywhere on websites for collecting user information. Proper labels and placeholders improve user experience and accessibility.
💼 Career
Knowing how to build accessible forms is essential for web developers and front-end engineers.
Progress0 / 4 steps
1
Create the form skeleton
Write the basic HTML form structure with a <form> element containing two input fields: one with id="name" and one with id="email". Do not add labels or placeholders yet.
HTML
Hint
Start by writing a <form> tag. Inside it, add two <input> tags with the exact id attributes name and email.
2
Add labels for inputs
Add a <label> for each input. The label for the name input should have for="name" and the text "Name:". The label for the email input should have for="email" and the text "Email:".
HTML
Hint
Use the <label> tag with the for attribute matching the input's id. Write the label text exactly as "Name:" and "Email:".
3
Add placeholder text inside inputs
Add a placeholder attribute to the name input with the value "Enter your full name" and to the email input with the value "Enter your email address".
HTML
Hint
Add the placeholder attribute inside each <input> tag with the exact text given.
4
Complete the form with a submit button
Add a <button> inside the form with type="submit" and the text "Send".
HTML
Hint
Add a <button> tag with type="submit" and the text "Send" inside the form.
Practice
(1/5)
1. What is the main purpose of a <label> element in an HTML form?
easy
A. To describe what the input field is for and improve accessibility
B. To provide example text inside the input field
C. To style the input field with colors
D. To submit the form data to the server
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 A
Quick Check:
Label = Describe input + accessibility [OK]
Hint: Labels describe inputs clearly and help screen readers [OK]
Common Mistakes:
Confusing labels with placeholders
Thinking labels style inputs
Assuming labels submit forms
2. Which of the following is the correct way to associate a label with an input field using the for attribute?
easy
A. Email
B. Email
C. Email
D. Email
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"> uses for="email" in label and id="email" in input, which is correct.
Final Answer:
<label for="email">Email</label><input id="email" type="text"> -> Option D
Quick Check:
Label for = Input id [OK]
Hint: Label's for must match input's id exactly [OK]
Common Mistakes:
Using for on input instead of id
Mismatching for and id values
Using id on label instead of for
3. What will the following HTML code display inside the input box when the page loads?
<label for="username">Username</label>
<input id="username" type="text" placeholder="Enter your username">
medium
A. The text 'Username' inside the input box
B. The text 'Enter your username' inside the input box as a hint
C. An empty input box with no text
D. The label 'Username' inside the input box
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 B
Quick Check:
Placeholder = hint text inside input [OK]
Hint: Placeholder shows hint inside input, label is outside [OK]