Week 5: Introduction to HTML Forms
Learn how to collect user input using basic form elements.
Explore Chapter 5Labels for Inputs (<label>)
The <label> element provides a caption for an item in a user interface. It's crucial for accessibility.
Associating a <label> with an <input> element allows users to click on the label to focus the corresponding input field. Screen readers also use labels to announce the purpose of the input field.
How to Associate Labels:
Use the `for` attribute on the <label>, making its value match the `id` attribute of the corresponding <input> element.
<form action="/submit-page" method="post">
<!-- Method 1: Using 'for' and 'id' -->
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstName">
<br><br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="lastName">
<br><br>
<input type="submit" value="Submit">
</form>
Always use <label> elements for your form inputs!