Skip to content

02. Labels and Inputs

Labels and inputs work together to create accessible, usable form controls. The <label> element associates text with form controls, improving accessibility and user experience. The <input> element provides various input types for collecting different kinds of data. Understanding how to properly connect labels and inputs is essential for creating accessible forms.

<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label>
Username:
<input type="text" name="username">
</label>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<label>
<input type="checkbox" name="agree"> I agree to the terms
</label>
<label>
<input type="radio" name="gender" value="male"> Male
</label>
<label>
<input type="radio" name="gender" value="female"> Female
</label>
<!-- Good: Labeled input -->
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<!-- Avoid: Unlabeled input -->
<input type="email" name="email" placeholder="Email">

Use for attribute:

<label for="username">Username:</label>
<input type="text" id="username" name="username">