DEV Community

SVG to React
SVG to React

Posted on Originally published at svgtoreact.com

My SVG Shows Up in the Browser but Not in React. Here's the Checklist I Use

You open the .svg file in the browser and it looks perfect. You drop it into a React component and... nothing. No error, or a vague one, and an empty space where the icon should be.

The file is almost never the problem. Something between the file and the DOM is. Here is the order I check things in.

First, two quick checks. Open the console and look for a React warning. Then inspect the spot where the icon should be:

  • No <svg> or <img> element at all? It's the import or the component.
  • The element is there but blank? It's size, paint, or loading.

1. The import gives you a URL, not a component

Most bundlers turn import logo from "./logo.svg" into a URL string. Render that as <Logo /> and React gets a string where it expected a component.

  • Vite: add vite-plugin-svgr. In its current major version only imports ending in ?react become components: import Logo from "./logo.svg?react". A plain import stays a URL. For TypeScript, add /// <reference types="vite-plugin-svgr/client" />.
  • Create React App: the component is the named export ReactComponent; the default export is still the URL.
  • Next.js: without an SVG loader the import is an object with src, width, and height. <img src={logo} /> renders src="[object Object]". Use logo.src or next/image.

2. Raw SVG was pasted straight into JSX

JSX is not HTML. Some differences only warn (class, stroke-width), but these actually break the icon:

<svg style="fill:red">        // throws: style must be an object
<use xlink:href="#a" />        // Babel: namespace tags not supported
<!-- exported from Figma -->   // syntax error in JSX
<svg viewbox="0 0 24 24">      // lowercase, not treated as viewBox
Enter fullscreen mode Exit fullscreen mode

Fixed versions: style={{ fill: "red" }}, href="#a", {/* comment */}, and viewBox.

3. No viewBox, or no size

If the element exists but takes no space, check its computed width and height.

  • Missing viewBox: a 512-unit drawing in a 24px box shows only its top-left corner. Some optimizer presets remove viewBox (SVGO 3's default preset does).
  • Zero or percentage size: width="100%" inside a parent with no width is zero.
  • Flex rows: give the icon a fixed width/height and flex-shrink: 0.
  • 1em sizing: inside a parent with font-size: 0, the icon disappears.

4. It's drawn, just in an invisible color

fill="none" with no stroke draws nothing. Outline icon sets rely on stroke, so removing stroke removes the icon. And currentColor takes the CSS color of the parent, so white text on a white button gives you a white icon.

5. Duplicate IDs in gradients, masks, and clip paths

IDs in inline SVG are global to the page. Render the same icon twice and every url(#id) points at the first copy. If that copy sits inside a hidden menu, the visible ones can lose their gradient or clip path. Generate a unique ID per instance with React's useId().

If the file loads but never draws

Check that the server sends image/svg+xml as the content type, and that your Content Security Policy allows the source you load it from.


I keep the full version of this checklist, with code for each framework and the MIME/CSP details, here: SVG Not Showing in React? Causes and Fixes. If you'd rather skip the attribute work entirely, I also built a free SVG to JSX converter that runs SVGR in the browser and keeps the viewBox (nothing is uploaded).

What's the weirdest reason an SVG went blank on you?

Top comments (0)