Pasting an exported .svg from Figma, Illustrator, or an AI generator straight into a React component or a CSS background-image almost never works on the first try. Usually one of two things happens: TypeScript yells about kebab-case attributes, or the browser renders an invisible blank box with zero console errors.

Here are the seven failure modes that actually break inlined icons in production, along with the minimal fix for each.

1. Hyphenated SVG attributes fail in React JSX and TypeScript

Standard SVG XML uses kebab-case attributes like stroke-width, stroke-linecap, fill-rule, and clip-path. React's DOM bindings expect camelCase properties (strokeWidth, strokeLinecap, fillRule, clipPath), class must become className, and legacy xmlns:xlink / xlink:href must be rewritten (xlinkHref or plain href).

// Breaks TS (`SVGProps<SVGSVGElement>`) and triggers React warnings <svg viewBox="0 0 24 24" stroke-width="2" stroke-linecap="round"> <path fill-rule="evenodd" clip-rule="evenodd" d="M12 3L4 9v12h16V9l-8-6Z" /> </svg> // Valid React TSX component import type { SVGProps } from 'react' export function HomeIcon(props: SVGProps<SVGSVGElement>) { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" {...props} > <path fillRule="evenodd" clipRule="evenodd" d="M12 3L4 9v12h16V9l-8-6Z" /> </svg> ) } 

Two easy-to-miss details here:

  • Always spread {...props} on the root <svg> so callers can pass className, aria-hidden="true", or event handlers.
  • Keep viewBox intact. If you drop viewBox and only keep width="24" height="24", scaling the icon via CSS (className="size-5" or width: 20px) clips the graphic instead of scaling it.

2. Inline style="..." strings crash React at runtime

Illustrator and Inkscape frequently serialize presentation attributes as a single CSS string:

<path style="fill:#0f172a;stroke:#84cc16;stroke-width:2;" d="..." /> 

In HTML, that works. In React JSX, passing a string to style throws a runtime error (The style prop expects a mapping from style properties to values, not a string).

You either parse style into an object (style={{ fill: '#0f172a', stroke: '#84cc16', strokeWidth: 2 }}) or—better for maintainability—promote the rules to presentation props (fill="#0f172a" stroke="#84cc16" strokeWidth={2}) so parent styling and currentColor overrides stay predictable.

3. Hardcoded hex fills break dark mode (and naive find-and-replace breaks fill="none")

When you want an icon to inherit button or text color via CSS (color or Tailwind's text-* utilities), you replace hardcoded hex fills/strokes with currentColor.

The trap is a naive regex replace on every fill="..." or stroke="...". If an icon has fill="none" on the root <svg> (standard for monoline icons) or fill="url(#grad-1)" on a shape, rewriting those to currentColor turns your stroke icon into a solid black blob or kills the gradient.

Only rewrite paints that are not none, transparent, currentColor, or url(...):

function applyCurrentColor(svg: string): string { return svg .replace( /\b(fill|stroke)\s*=\s*["']\s*(?!none\b|transparent\b|url\(|currentColor\b)([^"']+)\s*["']/gi, '$1="currentColor"', ) .replace( /\b(fill|stroke)\s*:\s*(?!none\b|transparent\b|url\(|currentColor\b)([^;"']+)/gi, '$1:currentColor', ) } 

4. Duplicate <defs> IDs silently hide icons on the same page

Export two icons from Figma and both often contain:

<clipPath id="clip0_1_2"><rect width="24" height="24" /></clipPath> <g clip-path="url(#clip0_1_2)">...</g> 

Inline both components into the same DOM document and id="clip0_1_2" collides. Worse: if the first icon sits inside a closed dropdown or mobile drawer with display: none, the second visible icon references the hidden <clipPath> and disappears completely.

Fix: strip redundant outer <clipPath> wrappers when the shapes already sit inside the viewBox, or scope remaining IDs per component (e.g., React's useId()).

5. Unescaped # truncates CSS data:image/svg+xml URIs silently

When inlining an SVG in CSS (background-image or list-style-image), UTF-8 Data URIs are usually 25–30% smaller than Base64 and compress far better with gzip/Brotli.

The #1 bug when encoding UTF-8 SVG Data URIs is leaving # unescaped inside hex colors:

/* BROKEN: CSS stops parsing the URL at the '#' in #0F172A */ .icon-bad { background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path stroke='#0F172A' d='M5 12h14'/></svg>"); } /* WORKS: escape %, #, <, >, {, } and use single quotes inside double-quoted url("...") */ .icon-good { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none'%3E%3Cpath stroke='%230F172A' stroke-width='2' d='M5 12h14'/%3E%3C/svg%3E"); } 

A minimal encoder that avoids bloated full encodeURIComponent() output while keeping all browsers happy:

export function svgToDataUri(cleanSvg: string): string { const singleLine = cleanSvg.replace(/\s+/g, ' ').trim() const encoded = singleLine .replace(/%/g, '%25') .replace(/#/g, '%23') .replace(/</g, '%3C') .replace(/>/g, '%3E') .replace(/"/g, "'") .replace(/\{/g, '%7B') .replace(/\}/g, '%7D') return `data:image/svg+xml,${encoded}` } 

6. Missing xmlns works inline in HTML, fails silently in Data URIs and <img>

HTML5 parsers auto-infer the SVG namespace for inline <svg> tags, so developers routinely strip xmlns="http://www.w3.org/2000/svg" to save bytes.

The moment you move that exact same markup into data:image/svg+xml,... or an <img src="...">, the browser switches to its strict XML parser. Without xmlns="http://www.w3.org/2000/svg", the image fails silently and renders nothing. Keep xmlns whenever an SVG leaves inline HTML.

7. currentColor does NOT work inside CSS background-image

Another silent trap: you put stroke='%23...' → stroke='currentColor' inside a CSS background-image: url("data:image/svg+xml,...") expecting it to follow the button's color.

It won't. A Data URI loaded via background-image executes in an isolated image document and has no access to the parent element's CSS color (it falls back to black).

If you need a CSS-only icon that inherits currentColor, use mask-image instead:

.icon-mask { display: inline-block; width: 20px; height: 20px; background-color: currentColor; mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Cpath d='M5 12h14'/%3E%3C/svg%3E") no-repeat center / contain; -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Cpath d='M5 12h14'/%3E%3C/svg%3E") no-repeat center / contain; } 

Bonus: sanitize before you inline third-party SVGs

An .svg file is an executable XML document. Before pasting an untrusted icon into JSX or rendering it inline, strip <script>, <foreignObject>, inline event handlers (onload, onclick, onerror), and javascript: URLs. Even when previewing user-supplied SVGs in a web tool, render previews via <img src={blobUrl}> instead of dangerouslySetInnerHTML so scripts never execute in your page origin.

Testing conversions locally without hand-editing attributes

If you run into these conversions regularly and want a quick sanity check without installing a CLI toolchain, I built a browser-local SVG to Code converter that handles all of the above in one pass—bidirectional SVG ↔ React TSX/JSX, clean HTML, and minimal UTF-8 CSS Data URIs, with optional currentColor rewriting and XSS stripping, running 100% locally in the browser with no file uploads.

What SVG-to-JSX or Data URI edge case has bitten you the hardest in production?