Input with Label
Every form input must have a programmatically associated label. Here's how to do it right with the <label> element.
The Bad (Inaccessible)
<span>Email Address</span>
<input type="email" id="email">
Accessibility-Ready Code
<label for="email">Email Address</label>
<input type="email" id="email" name="email" autocomplete="email">
label(for="email") Email Address
input(type="email" id="email" name="email" autocomplete="email")
<label htmlFor="email">Email Address</label>
<input type="email" id="email" name="email" autoFocus={false} />
<label for="email">Email Address</label>
<input type="email" id="email" name="email" />
<div class="space-y-2">
<label for="email" class="block text-sm font-medium text-slate-700">Email Address</label>
<input
type="email" id="email" name="email"
class="block w-full rounded-md border-0 py-1.5 text-slate-900 shadow-sm ring-1 ring-inset ring-slate-300 placeholder:text-slate-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:line-height-6"
/>
</div>
The Standard
A <label> element tells users (and assistive technologies) what a form field is for. Without it, a screen reader user lands on an input and hears⦠silence.
WCAG Criteria
- 1.3.1 Info and Relationships (Level A): Information and relationships conveyed through presentation must be programmatically determined.
- 3.3.2 Labels or Instructions (Level A): Labels or instructions are provided when content requires user input.
β The Bad (Inaccessible)
Whatβs Wrong?
- No Association: The
<span>is visually near the input, but thereβs no programmatic link. - Screen Reader Silence: Users hear βedit textβ with no indication of what to type.
- Click Target: Clicking the text doesnβt focus the input.
β The Good (Accessibility-Ready Code)
Why This Works
- Programmatic Link: The
for="email"attribute connects the label to theid="email"input. - Screen Reader Clarity: Users hear: βEmail Address, edit text.β
- Click Target: Clicking the label focuses the inputβa free usability win.
Accessibility Checklist
- Every
<input>,<select>, and<textarea>must have a<label>. - The
forattribute on the label must match theidof the input. - Use
autocompleteattributes for common fields (email, password, name). - Do NOT use
placeholderas a replacement for a label.
<label for="username">Username</label>
<input type="text" id="username" autocomplete="username">
Technical Deep Dive
The Accessibility Tree
Role: textbox
Name: "Email Address"
State: focusable Interactive Behavioral Lab
Interactive Sandbox
π¬ Technical Internals
Understand how this component is processed by the browser and Assistive Technology (AT). This section bridges the gap between visual code and the hidden logic that powers accessibility.
π² Accessibility Tree
The data structure used by screen readers to "see" your page. It translates HTML roles and attributes into standardized objects.
βοΈ Event Logic
Expected behavioral standards for keyboard navigation and state transitions. Crucial for users who don't use a mouse.
- Focus: Highlights via
:focus-visible - Activation: Responds to
EnterandSpace - Role: Identified as
Input Pattern