HTML <label> Tag
The <label> tag defines a label for form elements like inputs, textareas, and selects. It improves accessibility by associating text with form controls, making forms easier to use for everyone, especially screen reader users.
Connecting Labels to Inputs
Connect a label to an input using the for attribute on the label matching the id attribute on the input. When users click the label, the associated input receives focus. This is essential for accessibility and better UX.
Explicit vs Implicit Association
You can also wrap the input inside the label (implicit association). Both methods work, but explicit association with for and id is preferred for clarity and better control over styling and layout.
Accessibility Benefits
Labels improve accessibility by associating text with form controls. Screen readers announce the label when the input receives focus. Clicking the label focuses the input, making forms easier to use, especially on mobile devices.
Best Practices
Always include labels for all form inputs to ensure accessibility. Use explicit association with for and id when possible. Make label text descriptive and clear. Never use placeholder text as a replacement for labels.
