Accessible form field with label, hint and error
Associate labels, help text and validation errors with an input so screen readers announce them.
- HTML
- GreenSnippets
- MIT
<div class="field">
<label for="email">Email address</label>
<input
id="email"
name="email"
type="email"
autocomplete="email"
required
aria-describedby="email-hint email-error"
aria-invalid="true"
/>
<p id="email-hint">We only use this to send your receipt.</p>
<p id="email-error" role="alert">Enter an email address like name@example.com.</p>
</div>How it works
Every input needs a real <label for>; a placeholder is not a label. Link help text and error messages with aria-describedby so they are read with the field. Mark invalid fields with aria-invalid="true" and expose errors with role="alert" so they are announced when they appear. Use the right type and autocomplete values to get the correct mobile keyboard and browser autofill.
This snippet has been reviewed but was not executed by an automated test. Test it in your own environment before relying on it.
Related snippets
- CSS
Respect prefers-reduced-motion
Turn off or shorten animations for users who ask their system for less motion.
- CSS
Accessible focus ring with :focus-visible
Show a clear focus outline for keyboard users without drawing it on mouse clicks.
- HTML
Accessible modal with the dialog element
Use the native dialog element for a modal with built-in focus trapping, Escape to close and a backdrop.
- HTML
Accordion with details and summary
A keyboard-accessible FAQ accordion with no JavaScript, including an exclusive group using the name attribute.

