Dev TooloraLocal dev tools

> Images & Codes

SVG to Data URI, CSS & JSX

Turn SVG markup into a data URI, a CSS background rule or a React JSX component, and reject SVG that can run script.

Runs locallyWorks offline
Auto-run
Back to results
Input

Source data

Output
Output

Result

Your result will appear here.

Tool guide

About this tool.

  • A data URI puts a whole file inside a URL, so a small SVG icon can live in CSS or HTML without a separate request. The URL-encoded form percent-encodes only the characters that break a URL, such as #, % and <, and is usually smaller than Base64.
  • The CSS output wraps the data URI in an .icon rule with background-image, background-repeat: no-repeat and background-size: contain.
  • The JSX output turns the SVG into a React component. class becomes className, namespaced and hyphenated attributes become camelCase (xlinkHref, strokeWidth), data-* and aria-* stay as they are, and a style attribute becomes a style object. The root receives {...props} last, so callers can override any attribute.

  • Inline icons in CSS without an extra HTTP request.
  • Background images for buttons, list markers and form controls.
  • Turning a designer export into a React icon component.
  • Checking that an SVG from an unknown source has no script before you inline it.

Paste SVG markup and pick an Output: Data URI, CSS or JSX.

For a data URI or CSS, choose URL-encoded or Base64. For JSX, set the Component name and turn on TypeScript if you need types.

Optimize first is on by default and reports the saved bytes.

The preview shows the SVG that was converted. SVG with script, event handler attributes or javascript: URLs is rejected.

  • <svg …><path d="M5 12l5 5L20 7"/></svg> → data:image/svg+xml,%3Csvg …
  • Output CSS → .icon { background-image: url("data:image/svg+xml,…"); … }
  • Encoding Base64 → data:image/svg+xml;base64,PHN2Zy…
  • class="a" stroke-width="2" → className="a" strokeWidth="2"
  • style="stroke-width: 2; --x: 1" → style={{ strokeWidth: '2', '--x': '1' }}
  • <svg onload="…"> → rejected, the attribute onload can run script

  • Input is at most 500,000 characters; the root element must be <svg>.
  • DOCTYPE declarations are rejected, so external entities are never loaded.
  • The component name must be PascalCase, such as Icon or ArrowLeft.
  • Attribute values with a double quote print as {'…'} expressions.
  • Braces and angle brackets in text print as {'{'} style expressions.
  • Optimize drops Inkscape and Sodipodi editor elements; other elements JSX cannot express are rejected, never dropped.

FAQ

Questions about SVG to Data URI, CSS & JSX

Should I use URL encoding or Base64?

URL encoding keeps the SVG readable and is usually smaller. Use Base64 when a tool or pipeline expects it, or when the SVG has many non-ASCII characters.

Why is my SVG rejected?

Inlined SVG runs in your page. A <script>, an iframe, frame, object or embed element, a srcdoc attribute, an on* event handler (also one set by an animation) or a javascript: URL could run code there, so the tool refuses to convert them.

What does the JSX output leave out?

Comments, and whitespace between elements outside text, which JSX would lose anyway. Text inside <text> and <tspan> keeps its spaces, and <style> content becomes a template literal.

What does Optimize first change?

It removes comments, metadata, editor attributes and extra whitespace, and trims trailing zeros in numbers. The summary shows how many bytes it saved. Turn it off to keep the source exactly.

Can I get a TypeScript component?

Turn on TypeScript. The component then imports the SVGProps type from react and types its props as SVGProps<SVGSVGElement>.

Continue with

SVG OptimizerReduce SVG file size while preserving view boxes, ids, classes and accessibility attributes.SVG to PNGRender pasted SVG markup to a local PNG artifact.SVG Pattern GeneratorGenerate a seamless SVG background pattern of dots, grid, diagonal lines, zigzag or checkers as a ready CSS rule.SVG Path EditorFormat, minify, convert, move and scale SVG path data, with its bounding box, length and points.