0% completed
In this lesson, you'll learn how to properly use labels in your HTML forms and why they are important for accessibility and usability. Clear labels help all users understand what data belongs in each field, including those using assistive tools.
Clarity: Labels tell users what information to enter, making forms easier to understand.
Accessibility: Screen readers and other assistive tools use labels to read out the purpose of each input field. This helps users who rely on these tools navigate and fill out forms correctly.
Clickable Areas: When labels are correctly connected to inputs, clicking the label focuses on the corresponding input field. This improves the user experience, especially on small screens.
The <label>
element is used to define a text label for an <input>
element. There are two common ways to use <label>
:
You can associate a label with an input by using the for
attribute in the <label>
tag. The value of the for
attribute must match the id
of the corresponding input.
Syntax:
<label for="inputId">Label Text:</label> <input type="text" id="inputId" name="inputName">
<label for="inputId">
: Connects this label to the input with id="inputId"
.<input type="text" id="inputId">
: The input field that now has an associated label.Alternatively, you can place the <input>
element directly inside the <label>
element. This method automatically associates the label with the input.
Syntax:
<label> Label Text: <input type="text" name="inputName"> </label>
for
attribute and id
in this case.Explanation:
<label for="fullname">
: Associates the label with the input field that has id="fullname"
.Explanation:
for
attribute method, ensure each input has a unique id
that matches the label's for
attribute.Remember, incorporating well-structured labels into your forms makes them more accessible, easier to use, and ultimately more effective. Practice using both methods to see which works best for different situations.
.....
.....
.....