Common SVG Icon Mistakes, Tested in Chrome and Firefox

We reproduced common SVG icon mistakes at 16, 24 and 48 px: duplicate ids, missing viewBox, color, blur and alignment, with measured results and fixes.

Amit Yadav
15 min read
On this page

The most damaging SVG icon mistakes are duplicate ids across inlined icons, a missing viewBox or size, hardcoded colors, and icons left on the text baseline. Fix them with one unique id per definition, viewBox plus width and height on every icon, currentColor, and flex centering.

We reproduced each mistake in Chrome 156 and Firefox 157 on 2026-11-05, at 16, 24 and 48 px. We measured boxes with getBoundingClientRect and counted pixels in screenshots. The bugs that pass code review come first.

Key takeaways

  • A hidden copy can erase a visible icon. If the first element with a given id sits inside display:none, later icons that use that id paint nothing.
  • Default SVGO settings create id clashes. All 98 real icons we sampled got the id a, and 77 of them changed visibly when placed after another icon. Add prefixIds.
  • A viewBox with no size is not zero size. It rendered at 800 by 800 in a block and at 0 by 0 in a button. Set both.
  • Blur comes from the size, not the position. A 24 px menu icon had 11% gray pixels, and 24.5 px had 60%. Half-pixel layout offsets changed nothing.
  • Safari was not tested. WebKit would not start on our Linux test machine.

Before and after screenshots of six SVG icon mistakes at 16, 24 and 48 pixels, including a vanished Instagram icon and a bell cut off with no viewBox

Duplicate ids across inlined icons

Gradients, clip paths and masks are referenced by id, and inline SVGs share one document. So url(#g) resolves to the first #g on the page, not the one inside the same icon.

Results at all three sizes, both browsers:

SetupResult
Two gradients, same idSecond icon paints blue, not orange
Two clip paths, same idSecond uses first's clip (484 ink pixels, not 312, at 24 px)
First copy in display: none, gradientSecond icon paints nothing
First copy in display: none, clip or maskClip ignored, full square
First copy visibility: hidden or opacity: 0No effect
Hidden copy comes secondNo effect

We rendered the real Instagram icon from Skill Icons twice with the first inside a hidden div. The visible copy went from 481 ink pixels at 24 px to 0. The SVG keeps its layout box, so nothing looks wrong in the markup. A closed mobile menu or an inactive tab earlier in the page can set up the same order.

Across 17 packs and 34,239 icons, outline packs barely use ids (Lucide none of about 2,000, Tabler 3 of about 6,300). Color packs do: Skill Icons 158 of 400, Logos 477 of 2,303, Circle Flags all 696.

Those ids are mostly hashed: only 6 of 15,759 distinct ids had two different definitions. Two different icons rarely clash, but the same icon rendered twice always shares its ids.

Two fixes worked:

  • Unique ids per instance. Rewrite the ids and their references when you render.
  • One definition. Keep each icon once in a sprite and reuse it with <use>. Hide the sprite with size, not with display: none:
<!-- Gradients keep working: 576 ink pixels at 24 px -->
<svg width="0" height="0" style="position:absolute">...</svg>

<!-- Gradient icons drawn through <use> paint nothing -->
<svg style="display:none">...</svg>

The SVG sprite maker builds the sprite file, and our icon delivery comparison covers when a sprite is the right choice.

SVGO can create the clash

We ran 98 real icons that use url(#...) through SVGO 4.1.0 defaults with multipass on. All 98 came out with an id named a, because the cleanupIds plugin shortens ids per file and has no view of the page.

We then placed each one after a minified Instagram icon at 48 px. In 77 of the 98, more than 5% of ink pixels changed, and one all but vanished. Unique source files can still break in the build.

prefixIds fixes it. With a distinct prefix per icon, 0 of 20 tested icons changed. Pass the file path and SVGO builds the prefix from the file name, so a became instagram_svg__a in our run:

import { optimize } from 'svgo';

const { data } = optimize(source, {
  path: 'instagram.svg',
  multipass: true,
  plugins: ['preset-default', 'prefixIds'],
});

Using a browser tool instead? In our SVG optimizer, turn off Cleanup IDs for gradient icons you will inline. It is on by default and the tool has no prefix option. We tested that setting in SVGO itself, not through the tool.

Style tags that leak

A <style> inside an inline SVG is not scoped. It applies to the whole page. We wrote test icons in the style design tools export, each with .cls-1 as its fill:

  • Same class name, two icons. The first icon should be blue and rendered orange, because the later rule wins.
  • Hidden icons count too. A style tag in a hidden div still recolored the visible icon.
  • Bare selectors spread. path { fill: ... } in a hidden SVG filled in a Lucide bell, even with fill="none" on its group. Its ink rose from 164 to 282 pixels at 24 px.
  • Page text is in range. p { color: ... } in an icon recolored a paragraph outside it.

The first 15 packs in our scan had no <style> tags at all, so this comes from design-tool exports rather than prepared packs. SVGO's default inlines a class used once but keeps the style tag for a class used twice. Both of these fixed it:

// Inline every class, even ones used more than once
plugins: [{
  name: 'preset-default',
  params: { overrides: { inlineStyles: { onlyMatchedOnce: false } } },
}]

// Or keep the class and prefix it: .ic__cls-1
plugins: ['preset-default', { name: 'prefixIds', params: { prefix: 'ic' } }]

No viewBox

Without a viewBox, the drawing keeps its original coordinates and width and height only set a window onto it. The Lucide bell, drawn on a 24 grid:

Set sizeBoxPainted areaResult
1616 x 1614 x 15Cropped, 67 ink pixels, not 94
2424 x 2420 x 22Looks correct
4848 x 4820 x 22Small, in the top left corner

The middle row is why this ships. At the icon's native size there is no visible bug, and it appears only when someone changes the size. With neither viewBox nor size, the box measured 300 x 150 in both browsers.

SVGO 3.3.5 defaults removed the viewBox when it matched the width and height. Version 4.1.0 keeps it, so the old removeViewBox: false advice only matters if you are still on v3.

viewBox with no size

Removing width and height gives a size that depends on the parent:

ParentRendered size
Block, 800 px wide800 x 800
Flex row with a label760.8 x 760.8
Plain button or inline-flex button0 x 0
Inline-block0 x 0

So a CSS class such as w-5 holding the size is one late stylesheet away from a giant icon. We delayed the CSS by 600 ms in Chrome. The icon started at 800 x 800, dropped to 20 x 20, and scored 0.0847 CLS. With width="20" height="20" in the markup it held 20 x 20 from the start, with 0 CLS. Keep the attributes even when CSS also sets the size.

Hardcoded colors

A fill written into the file ignores color. We set the parent to red and read the computed style and the actual pixels:

MarkupPaints
fill="#1a1a1a" on the shape#1a1a1a
No fill anywhereBlack
fill="currentColor" on the rootRed
Root #000, child currentColorRed
Root currentColor, child #000Black

The nearest attribute wins, and the same applies to stroke. A hardcoded color on the root does not block a currentColor on the child, so check the child elements when an icon refuses to change. A CSS rule beats a presentation attribute, and an inline style beats a CSS rule. Our currentColor guide has the patterns.

Do not bulk-fix this with SVGO's convertColors and currentColor: true. In our test it turned every color into currentColor, including orange, blue and white, and left only none. A multi-color icon goes flat.

SVG in an img tag

An SVG in <img> is a separate document, so page CSS cannot reach it. A file painting with currentColor stayed black under both color and fill rules. These options gave us red:

MethodSame originCross origin, no CORS
Inline SVGRedNot applicable
<use href="/sprite.svg#i">RedPaints nothing
CSS mask with background: currentColorRedPaints nothing

Dark mode is the one case that can stay in <img>. A prefers-color-scheme query inside the file worked, and it followed the page's color-scheme. With the OS set to light, color-scheme: dark on the page still gave the dark version, so a site toggle can drive it.

Icons left on the baseline

An inline SVG sits on the text baseline, so its bottom edge lines up with the bottom of the letters. We measured the gap between the center of the capital letters and the center of the icon, with 16 px text and 1.5 line height. Zero is perfect, and positive means the icon sits too high.

Method16 px icon24 px icon48 px icon
Default inline+2+6+18
vertical-align: middle-1.77-1.77-1.77
vertical-align: -0.125em0+4+16
Flex align-items: center-1-1-1

Flex center gave -1 in Chrome and -0.5 in Firefox with Liberation Sans, and 0 with Noto Sans. It also keeps the row short. A default 24 px icon made a 31 px row, against 24 px with flex. The popular -0.125em trick is only right near 1em.

The same baseline adds a gap under a lone icon: a div around a 24 px icon measured 31 px tall, and 24 px with display: block on the icon. In Tailwind, a few classes handle both.

Icons that shrink in flex rows

Flex items shrink by default, and width="24" does not protect the icon. In a 200 px row with a long label, a 24 px icon squashed to 6.75 px wide while keeping its full height. A 48 px icon went to 13.03 px, and a CSS class size did no better. flex-shrink: 0 held all three sizes. It needs long text to show, so it often appears first with real user data or translations.

Blur at in-between sizes

We counted the share of inked pixels that were partly covered, meaning gray rather than solid black. Each image was captured at 1x and enlarged without smoothing:

Lucide menu and bell icons enlarged pixel by pixel at sizes from 16 to 48 pixels, showing gray edges at 16.5 and 24.5 pixels, and sharp lines at 24

Lucide menu sizeGray at 1xGray at 2x
16 px72%45%
16.5 px75%50.5%
20 px71%38.5%
24 px11%8%
24.5 px60%42%
48 px8%4%

Three results differ from the usual advice:

  • A 24-grid icon at 16 px is already soft. 16.5 px was only 3 points worse. Any size that is not a multiple of the grid gets gray edges.
  • Half-pixel layout does not blur. A 0.5 px margin, flex centering in an odd box and left: 50% all gave 11%, the same as a whole position, because the browsers snap the box. Only transform: translate(.5px, .5px) blurred, at 70% on 1x and 8% on 2x.
  • Icons drawn for 16 px did not win on this metric. Heroicons 16-solid at 16 px scored 72% and an Octicons 16 px icon 84%. The count includes curves and diagonals, so judge these by eye.

shape-rendering: crispEdges took gray to 0%, but the bell turned jagged and the menu lines came out at uneven weights. Use 24 or 48 where you can, and watch em sizes: 1.25em of 13 px is 16.25 px, which scored 86%.

Over-trimmed path decimals

SVGO's default keeps 3 decimals. We compared lower settings on 92 Lucide and Tabler icons in Chrome, counting ink pixels changed by a visible amount:

DecimalsBytes savedMean change at 24 pxIcons over 10% changed, at 48 px
30.1%0%0
22.8%0.06%0
16.7%1.08%3
019.7%11.86%48

Two decimals is a safe trim. Damage also grew with size: at 0 decimals the mean was 12.64% at 48 px, against 9.02% at 16 px, so this is not only a small-icon problem.

Icon buttons with no name

In Chrome's accessibility tree, a button holding only a bare SVG had an empty name. Adding <title> gave the name "Alerts", and so did aria-label on the button with aria-hidden on the icon. Our accessible SVG icons test covers which markup to pick.

What we could not test

  • Safari. WebKit would not start on our Linux machine, so every result here is Chrome 156 and Firefox 157 only.
  • Fonts. Baseline numbers come from Liberation Sans and Noto Sans. Check vertical-align values on your own font.
  • The style leak test. We wrote those icons by hand to match the export pattern. They are not real files from a design tool.
  • Layout shift. Firefox has no layout-shift value, and we did not test a render-blocking stylesheet.

Checklist

  • Every icon has viewBox, width and height
  • Icons render at 16 and 48 px, not only their native size
  • One color icons use currentColor, with no hard fill on child elements
  • Gradient icons have unique ids, or prefixIds runs in the build
  • Icons have no <style> tag, or their classes are prefixed
  • A gradient icon still paints when an earlier copy is hidden
  • Icon and label sit in a flex row with align-items: center
  • Icons in flex rows have flex-shrink: 0
  • Sizes are multiples of the icon's grid
  • Icon buttons have an accessible name

FAQ

Why is my SVG icon not showing?
Check three things in order. First, whether its gradient or clip id also exists earlier on the page inside a display:none element: that made icons paint nothing in our test. Second, whether it has a viewBox but no size inside a button, which measured 0 by 0. Third, whether the fill matches the background.
Why does my SVG show the wrong gradient?
Another inline SVG on the page uses the same id, and the browser takes the first match in the document. SVGO's default settings can cause this by renaming ids to a, b and c in every file. Add the prefixIds plugin with a different prefix per icon.
Does an SVG need both a viewBox and width and height?
Yes. The viewBox lets the drawing scale, and without it a bell at 48 px stayed at 20 by 22 px. The attributes hold the size before CSS loads, and without them the same icon started at 800 by 800 px and caused a layout shift.
Why is my SVG icon blurry?
Usually because the size is not a multiple of its grid. A 24-grid menu icon had 11% gray pixels at 24 px, and 72% at 16 px. Half-pixel transforms also blur it on 1x screens. Half-pixel layout positions did not change the result in Chrome or Firefox.
How do I align an SVG icon with text?
Put both in a flex row with align-items center. It measured within 1 px of the capital letters' center at 16, 24 and 48 px. Default inline placement put a 48 px icon 18 px too high, and vertical-align -0.125em only worked near 16 px.

Related posts

Share this post