2026-11-06 · UI Design
Inline JSX, an img tag, SVGR components or a sprite sheet: how each way of using SVG in React behaves, and a simple rule for choosing.
An SVG file is the same no matter how you load it, but the way you put it into a React or Next.js page changes four things: whether you can recolour it with CSS, whether the browser caches it, how much JavaScript it adds to your bundle, and whether it is visible to assistive technology. Picking one method for every icon is the usual mistake. A 24 px menu icon and a 600 px hero illustration have different needs.
This guide compares four methods: the plain img tag, inline JSX, SVGR components, and a sprite sheet with the use element. For each one you get what it is good at, what it costs, and a short example. A decision rule closes the post.
The simplest approach is to treat the SVG like any other image: put the file in /public and reference it with <img src="/illustrations/hero.svg" alt="Dashboard showing weekly totals" width="640" height="400" />. In Next.js you can use the Image component the same way, though SVGs gain nothing from its resizing since they are already resolution independent. Set width and height anyway so the layout does not jump while the file loads.
What you get: the file is cached by the browser like any asset, it adds nothing to your JavaScript bundle, and it works in server components with no extra setup. What you give up: CSS on the page cannot reach inside the file. A rule like `.icon { fill: currentColor }` does nothing, so the SVG cannot follow your text colour or switch for dark mode. Hover states on inner shapes are also out.
Use this method for large, fixed-colour artwork: illustrations, logos and spot graphics. If the file is above roughly 20 KB, the separate request and cache is usually cheaper than pasting the markup into every page.
Paste the SVG markup straight into a component. Two conversions are needed because JSX is not HTML: attributes become camelCase (stroke-width becomes strokeWidth, fill-rule becomes fillRule, class becomes className), and the style attribute must be an object rather than a string. Here is a 24 px icon written that way:
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6" /></svg>
Because the shapes are now part of the page DOM, stroke="currentColor" makes the icon inherit the text colour of its parent. A button with a hover colour change recolours its icon with no extra code. You can also animate individual paths with CSS or set different colours per shape through CSS variables.
The cost is that the markup is part of your component output, so it ships in the HTML and, for client components, in the JavaScript. For a handful of small icons that is fine. For 40 icons on one screen it adds up, and the same path data is repeated every time the component renders.
Two checks save time. Always keep the viewBox: without it the SVG cannot scale to the width and height you set. And remove hard-coded fill and stroke colours that you want CSS to control, replacing them with currentColor.
SVGR is a tool that turns an SVG file into a React component at build time, so you write `import ArrowIcon from "./arrow.svg"` and render <ArrowIcon className="h-5 w-5" />. In Next.js this takes a small webpack or Turbopack rule in your config that routes .svg imports through @svgr/webpack; follow the current Next.js and SVGR documentation for the exact lines, since the setup differs by bundler.
This is inline SVG without the copy and paste. You keep the original files as the source of truth, designers can replace a file without anyone touching JSX, and the result accepts props, so you can pass className, aria-label or a size. SVGR also runs SVGO by default in many setups, which strips editor metadata and can shorten path data.
Watch one SVGO default. Some optimisation presets remove the viewBox attribute, which breaks scaling. If an icon renders at the wrong size after import, check that viewBox survived. Also note that, as with hand-written inline SVG, each component adds its markup to the bundle; importing 200 icons into one client file is a bundle-size problem, so import only the ones a page uses.
Use SVGR for your product icon library: files stay clean and the components stay recolourable.
A sprite combines many icons into one file, each wrapped in a <symbol id="arrow"> with its own viewBox. You then reference one icon with <svg width="24" height="24"><use href="/sprite.svg#arrow" /></svg>. The browser downloads and caches the sprite once, and every icon on the page reuses it.
The benefit is repeated icons cost only a short <use> element each, not the full path data. That suits dashboards and tables where the same dozen icons appear hundreds of times. The trade-offs: icons are drawn in a shadow tree, so CSS from the page reaches only what the symbol exposes. In practice that means currentColor works if the symbol uses it, but you cannot style individual inner paths from outside. External sprite files also need to be same-origin. Hover effects on inner shapes are limited, and you need a build step or a script to assemble the sprite.
Use a sprite when a single icon set appears many times per page and you do not need per-shape styling.
Ask three questions in order.
1. Does the colour have to follow CSS (text colour, dark mode, hover)? If no, and the graphic is large, use img. Illustrations and logos belong here. 2. If yes, is it one of a library you reuse across the product? Use SVGR components so the files stay the source of truth. 3. If the same icons repeat dozens of times on one screen, move that set into a sprite.
One-off decorative shapes you only need in a single place are the exception: inline JSX is quicker than setting up anything else.
A practical mix for a typical app: logo and illustrations through img, the 30 to 60 interface icons through SVGR with currentColor, and nothing else. Add a sprite only if you measure a reason to.
Any method works better with clean input. Before an icon enters the codebase, check these points:
- The viewBox matches the artwork (for example 0 0 24 24) and there is no fixed width or height that fights your component size. - Strokes and fills that should adapt use currentColor. Fixed brand colours stay hard-coded only where they should never change. - Editor leftovers such as metadata, comments and unused ids are removed. Duplicate id values across inlined SVGs can break gradients and clip paths, because ids are global in the page. - Stroke width and grid size are the same across the set, so icons look consistent when placed side by side. - Decorative icons get aria-hidden="true". Icons that carry meaning on their own need a label, for example role="img" with aria-label, or a visible text label beside them.
If you are starting from a PNG or another format, VEXURA has a free SVG converter and a free in-browser tracer for raster images. Generated icons come from the icon generator, where SVG export is part of Pro; the free tier exports PNG and JPEG.
The icon renders but is the wrong size: the viewBox is missing or the file has fixed width and height. Add or restore the viewBox and set size in the component.
The icon does not change colour: the file has hard-coded fill or stroke values. Replace them with currentColor, or switch from img to an inline method.
Gradients disappear or look wrong when two icons share a page: duplicate ids. Rename them or let your optimiser prefix ids per file.
A React warning about an unknown property such as stroke-width: convert to strokeWidth. The same applies to fill-rule, clip-rule, stroke-linecap and stroke-linejoin.
A hydration mismatch with inline SVG: usually caused by markup that differs between server and client, such as a random id generated on render. Generate stable ids or avoid ids in the markup.
Pick the three icons you use most and put them through the same checklist: viewBox present, currentColor in place, no leftover metadata. Load one through each of the methods above in a throwaway page and open the Network and Elements panels. Seeing which method shows a separate request, which one inflates the HTML, and which one lets CSS reach the shapes teaches the trade-offs faster than any table. Then standardise on one method per category of graphic and write the rule in your project README so the next person does not have to guess.