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.

Amit Yadav
13 min read
On this page

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 holdsIcon offset from the input center
Input only0
Input and a 20 px error line12 px too low, and it jumps when the error appears
Label above and error below0, but only because the two heights happened to balance
4-row textarea30 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 setupResult of clicking the icon
Absolute, no pointer ruleInput not focused
Absolute, pointer-events: noneInput 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 paddingGap between icon and text
12 pxOverlaps by 15 px
16 pxOverlaps by 11 px
28 px1 px
32 px5 px
36 px9 px
40 px13 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.

Four inputs: a search field with a custom clear icon, a date field with a leading calendar plus the native one, a date field with two calendars side by side at the end, and a number field with native spinners

  • 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

PatternName in ChromeAxe
Label plus hidden iconEmailPasses
Placeholder onlyEmailPasses, but the label disappears when you type
Icon only, no labelNoneFails label
<title> in an SVG beside the inputNoneFails label
<title>Email icon</title> inside the labelEmail icon EmailPasses, 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 stateAxe violationsDescription given to the input
Icon only, with aria-invalid0None
Red border only0None, and the input is not even marked invalid
aria-describedby pointing to a missing ID0None
Text plus hidden icon0Enter a valid email
Text plus SVG <title>Error</title>0Error Enter a valid email
Text plus hidden "Error: " prefix0Error: Enter a valid email
Correction: the rule is 3.3.1, not 1.4.1

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.

ColorOn whiteOn #111827Use on white for
gray-4002.66.82Fails 3:1
gray-5004.843.67Icons and message text
green-5002.228Fails 3:1
green-6003.225.51Icons only
green-7004.953.59Icons and message text
red-5003.814.66Icons only
red-6004.773.72Icons and message text
yellow-5001.919.29Fails 3:1
yellow-6002.946.04Fails 3:1
amber-7005.033.53Icons 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.

StateLucideTablerPhosphorHeroiconsMDI
Errorcircle-alertalert-circlewarning-circleexclamation-circlealert-circle
Successcircle-checkcircle-checkcheck-circlecheck-circlecheck-circle
Warningtriangle-alertalert-trianglewarningexclamation-trianglealert
Hideeye-offeye-offeye-slasheye-slasheye-off
Loadingloader-circleloader-2spinnernone testedloading

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?
Wrap only the input in a relatively positioned element, then place the SVG absolutely with inset-inline-start and top 50 percent. Add pointer-events none and aria-hidden to the icon, and 36 px of start padding to the input.
Why does clicking my input icon not focus the field?
The icon sits on top of the input and takes the click. Set pointer-events to none on the icon, or place the icon inside a wrapping label. Both focused the input in our test.
Is an error icon alone accessible?
No. WCAG 3.3.1 requires the error to be described in text. Axe reported zero violations for an icon-only error in our test, so a clean report does not prove the error is exposed. Add text and link it with aria-describedby.
What color should form icons be?
On white, gray-500 for neutral icons, red-600 for errors, green-700 for success and amber-700 for warnings all measured above 4.5:1 in Tailwind v4. Gray-400, green-500 and yellow-500 measured under 3:1.
Should a password toggle change its label?
Either keep a fixed label with aria-pressed, or flip the label and leave aria-pressed off. Doing both gives a confusing state. Set type to button either way, or pressing Enter in the field can reveal the password.
Can I add a calendar icon to a date input?
Chrome already draws its own calendar in a date input, so a second icon doubles it. Keep the native one, or hide it and use your own picker. We tested only Chrome.

Related posts

Share this post