SVG Icons for Forms: Placement, Errors and Contrast Tested
Put the icon in a wrapper around the input only, with pointer-events off and 36 px padding. We tested clicks, RTL, contrast, errors and axe blind spots.
On this page
- The snippet to copy
- Position: wrap only the input
- Clicks: turn off pointer events
- Padding, direction and zoom
- Native controls: check for a second icon
- Names: the label does the work
- Errors: what axe does not catch
- Contrast: the common Tailwind picks fail
- Password toggle: three small traps
- Icon names by pack
- What this test does not cover
Position a leading icon absolutely inside a wrapper that holds only the input, set pointer-events: none and aria-hidden on it, and give the input 36 px of start padding. Error icons are decoration: the error itself must be text, linked to the input with aria-describedby.
We tested this in Chrome 156 with axe-core 4.14 and measured the common mistakes. The biggest finding is what axe misses: an icon-only error and a red border both report zero violations. The old version of this post also had a bug and a wrong WCAG reference, and both are corrected below.
Quick answer
- Position: put only the input in the wrapper. With an error line inside, the centered icon sat 12 px too low.
- Clicks: without pointer-events none, clicking the icon left the input unfocused.
- Padding: 36 px start padding leaves a 9 px gap beside a 16 px icon. 16 px padding overlaps the icon by 11 px.
- Errors: an icon alone and a red border alone both pass axe, and neither gives a screen reader any error text.
- Contrast: on white, use gray-500, red-600, green-700 and amber-700. Gray-400, green-500 and yellow-500 fall under 3:1.
The snippet to copy
We ran this exact code in a form. Axe reported no violations before or after using the toggle, and the same code worked in right-to-left mode and forced colors mode with no changes.
<div class="field">
<label for="email">Email</label>
<div class="control">
<svg class="lead" width="16" height="16" viewBox="0 0 24 24" aria-hidden="true"><!-- mail --></svg>
<input id="email" type="email" aria-invalid="true" aria-describedby="email-error">
</div>
<p id="email-error" class="error">
<svg width="14" height="14" viewBox="0 0 24 24" aria-hidden="true"><!-- circle-alert --></svg>
<span>Enter a valid email address.</span>
</p>
</div>
<div class="field">
<label for="pw">Password</label>
<div class="control">
<input id="pw" type="password" autocomplete="current-password">
<button type="button" class="reveal" aria-label="Show password" aria-pressed="false">
<svg class="on" width="16" height="16" viewBox="0 0 24 24" aria-hidden="true"><!-- eye --></svg>
<svg class="off" width="16" height="16" viewBox="0 0 24 24" aria-hidden="true"><!-- eye-off --></svg>
</button>
</div>
</div>.field { display: grid; gap: 4px; }
.control { position: relative; color: #6a7282; }
.control input {
box-sizing: border-box;
width: 100%;
padding-block: 8px;
padding-inline: 12px;
font: 14px/20px Inter, sans-serif;
border: 1px solid #6a7282;
border-radius: 6px;
}
.control:has(.lead) input { padding-inline-start: 36px; }
.control:has(.reveal) input { padding-inline-end: 40px; }
.lead {
position: absolute;
inset-inline-start: 12px;
top: 50%;
translate: 0 -50%;
pointer-events: none;
}
.error {
display: flex;
align-items: flex-start;
gap: 4px;
margin: 0;
font: 12px/16px Inter, sans-serif;
color: #e7000b;
}
.error svg { flex: none; height: 1lh; }
.reveal {
position: absolute;
inset-inline-end: 4px;
top: 50%;
translate: 0 -50%;
display: grid;
place-items: center;
width: 32px;
height: 32px;
padding: 0;
background: none;
border: 0;
color: inherit;
}
.reveal .off { display: none; }
.reveal[aria-pressed="true"] .on { display: none; }
.reveal[aria-pressed="true"] .off { display: block; }const input = document.getElementById('pw');
const toggle = document.querySelector('.reveal');
toggle.addEventListener('click', () => {
const show = input.type === 'password';
input.type = show ? 'text' : 'password';
toggle.setAttribute('aria-pressed', show);
});
// Keeps focus and the caret in the field after a mouse click.
toggle.addEventListener('mousedown', (e) => e.preventDefault());Color goes on .control and not on the icon for a reason. In forced colors mode, Chrome left a color set directly on the SVG as gray. The same color inherited from the wrapper changed to the system text color.
Position: wrap only the input
The usual top: 50% centers the icon on the wrapper, not on the input. So everything else in the wrapper moves the icon.
| Wrapper holds | Icon offset from the input center |
|---|---|
| Input only | 0 |
| Input and a 20 px error line | 12 px too low, and it jumps when the error appears |
| Label above and error below | 0, but only because the two heights happened to balance |
| 4-row textarea | 30 px below the first line |
For a textarea, use a fixed top instead. With our sizes, top: 10px sat 1 px from the first line. Also set the textarea to display: block, because the default inline display added a baseline gap that moved the center by 2.5 px.
Clicks: turn off pointer events
The old snippet on this page placed the icon over the input with no pointer rule. In our test, clicking that icon left focus on the page body, so the user clicked inside the field and could not type.
| Icon setup | Result of clicking the icon |
|---|---|
| Absolute, no pointer rule | Input not focused |
Absolute, pointer-events: none | Input focused |
Inside a wrapping <label> | Input focused |
Padding, direction and zoom
With the icon 12 px from the edge, a 1 px border and a 16 px icon, the start padding sets the gap:
| Start padding | Gap between icon and text |
|---|---|
| 12 px | Overlaps by 15 px |
| 16 px | Overlaps by 11 px |
| 28 px | 1 px |
| 32 px | 5 px |
| 36 px | 9 px |
| 40 px | 13 px |
Direction. Physical left and padding-left keep the icon on the left in a right-to-left page, where it belongs on the right. With inset-inline-start and padding-inline-start, the snippet above put the icon 12 px from the right edge and moved the toggle to the left, with no extra CSS.
Zoom. At a 200% root font size, an icon sized in px stayed 16 px beside 32 px text. An icon sized in rem scaled to 32 px. If your form text is sized in rem, size its icons in rem and set the padding in rem too.
Native controls: check for a second icon
Some inputs draw their own icons. Adding yours without checking gives you two.
![]()
- Date: Chrome always drew its own calendar at the end of the field. A custom trailing calendar sat right beside it, and a leading one duplicated it.
- Number: the native spinners appeared on hover at the end of the field, so a trailing icon needs reserved padding.
- Search: our custom clear icon needed end padding of its own. We saw no native clear button in our unfocused screenshot, so also check the focused state in your browser.
Names: the label does the work
| Pattern | Name in Chrome | Axe |
|---|---|---|
| Label plus hidden icon | Passes | |
| Placeholder only | Passes, but the label disappears when you type | |
| Icon only, no label | None | Fails label |
<title> in an SVG beside the input | None | Fails label |
<title>Email icon</title> inside the label | Email icon Email | Passes, but is read with extra words |
An icon never names an input. Keep a visible label and hide the icon. More on hidden icons is in our accessible SVG icons guide.
Errors: what axe does not catch
Each of these error states ran through axe. The first three look fine in a report and still fail the user.
| Error state | Axe violations | Description given to the input |
|---|---|---|
Icon only, with aria-invalid | 0 | None |
| Red border only | 0 | None, and the input is not even marked invalid |
aria-describedby pointing to a missing ID | 0 | None |
| Text plus hidden icon | 0 | Enter a valid email |
Text plus SVG <title>Error</title> | 0 | Error Enter a valid email |
| Text plus hidden "Error: " prefix | 0 | Error: Enter a valid email |
This post used to call an icon-only error a failure of WCAG 1.4.1. That criterion, Use of Color, is about color being the only visual signal. The rule for an icon-only error is WCAG 3.3.1, Error Identification, which says the error must be described to the user in text. A red border alone runs into both.
Forced colors mode shows why the border cannot carry the message. In our test, a normal border and a red error border both became the same system color, black in light mode and white in dark mode. The red was gone, so only the text and an inline icon remained. An inline currentColor icon stayed visible in both modes. A data URI background icon did not appear in our dark forced colors screenshot, which is one more reason to use inline SVG.
Add a text prefix when the message could be mistaken for help text. A visible icon plus "Enter a valid email" is usually clear, and hidden "Error: " text makes the state clear to a screen reader as well.
Multi-line errors. With 12/16 px text over two lines, align-items: center put the icon 8 px below the first line. align-items: flex-start was 1 px off, and adding height: 1lh to the SVG brought it to 0.
Contrast: the common Tailwind picks fail
WCAG 1.4.11 asks for 3:1 on visual information needed to identify a control or its state, and normal message text needs 4.5:1. Here are the measured Tailwind v4 colors. The old snippet on this page used gray-400 for icons and green-500 and yellow-500 for states.
| Color | On white | On #111827 | Use on white for |
|---|---|---|---|
| gray-400 | 2.6 | 6.82 | Fails 3:1 |
| gray-500 | 4.84 | 3.67 | Icons and message text |
| green-500 | 2.22 | 8 | Fails 3:1 |
| green-600 | 3.22 | 5.51 | Icons only |
| green-700 | 4.95 | 3.59 | Icons and message text |
| red-500 | 3.81 | 4.66 | Icons only |
| red-600 | 4.77 | 3.72 | Icons and message text |
| yellow-500 | 1.91 | 9.29 | Fails 3:1 |
| yellow-600 | 2.94 | 6.04 | Fails 3:1 |
| amber-700 | 5.03 | 3.53 | Icons and message text |
Dark mode reverses this: every color that passes well on white drops under 4.5:1 on #111827, so use a separate dark value. Set state colors once and let icons follow with currentColor, as covered in how to change SVG icon color.
Axe's contrast check does not cover these icons. In our earlier social icon test, it also did not flag low-contrast SVGs, so check the ratios yourself.
Password toggle: three small traps
1. Set type="button". A button with no type inside a form is a submit button. In our test, pressing Enter in the password field clicked that toggle, which revealed the password as plain text and submitted the form. Clicking the toggle submitted too. With type="button", Enter submitted without revealing anything, and clicking the toggle did not submit.
2. Use a fixed label or a flipped label, not both. The old snippet changed the label to "Hide password" and also set aria-pressed="true". That combination reads as "Hide password, pressed", which is a confusing state. The WAI-ARIA button pattern says "label on a toggle does not change when its state changes." Our snippet keeps "Show password" with aria-pressed. A label that flips with no aria-pressed also passed axe, and many teams use it.
3. Make the target real. The old 16 px button with neighbors failed the axe target check in one layout. A 32 px box passed. WCAG 2.5.8 asks for 24 px, with a spacing exception. A clear button with no label failed both button-name and target-size.
The value and caret position survived the toggle. A click moved focus to the button, unless mousedown called preventDefault. In that case, focus and the caret stayed in the field.
Icon names by pack
Every name below exists in our pack data. Each pack names the states differently, so check this before swapping packs. The old post also used names such as check-circle for Lucide, which now work only as aliases.
| State | Lucide | Tabler | Phosphor | Heroicons | MDI |
|---|---|---|---|---|---|
| Error | circle-alert | alert-circle | warning-circle | exclamation-circle | alert-circle |
| Success | circle-check | circle-check | check-circle | check-circle | check-circle |
| Warning | triangle-alert | alert-triangle | warning | exclamation-triangle | alert |
| Hide | eye-off | eye-off | eye-slash | eye-slash | eye-off |
| Loading | loader-circle | loader-2 | spinner | none tested | loading |
For matching icon sizes in other controls, see buttons and CTAs.
What this test does not cover
All tests ran in Chrome 156 on Linux, with lucide-static icons and Tailwind v4 colors. We could not trigger browser autofill in a headless browser, so autofill backgrounds over icons are untested. Older advice moves the icon out of the input background, and we could not confirm whether that is still needed. Edge's password reveal button, Firefox, Safari and real screen readers were not tested, so we also make no claim on when role="alert" is announced. The description text comes from Chrome's accessibility tree. We read the top search results through summaries only.
How do I put an icon inside an input?
Why does clicking my input icon not focus the field?
Is an error icon alone accessible?
What color should form icons be?
Should a password toggle change its label?
Can I add a calendar icon to a date input?
Related posts