About the SVG Viewer
Paste SVG code or open an .svg file in this online SVG viewer to see the image, its size and a safety check. Fix problems in the code as you go, then download the SVG or convert it to a PNG at any width.
SVG is an image format written as XML text, which is why icons, logos and charts made with it stay sharp at any size. Because it's text, an SVG can also carry scripts and links to other sites. The preview shows your SVG the way an <img> tag does on a website, where browsers never run scripts or load outside files, and the safety check tells you what would run if you used the code inline. Nothing is uploaded: the preview and the PNG are made in your browser.
- Paste into the SVG code box, or click Open File or drop an
.svgfile onto the editor (up to 5 MB). Load Example fills in a small picture of mountains to try. - The Preview updates about 0.3 seconds after you stop typing and scales the image to fit. Under it you see the size in pixels and the file size.
- Switch Background between Checkerboard, White and Black. Checkerboard shows which parts are transparent, and Black helps with white icons that vanish on white. Full Screen shows the image over the whole screen; press Esc to close it.
- If the code has an error, its line number turns red and the message under the editor gives the line and column. If the
<svg>tag has noxmlns, click Add xmlns. - To export, click Copy Data URI for CSS or HTML, or Download SVG for the edited code. For a PNG, type a width next to PNG width and click Download PNG. The height follows the image's proportions, up to 8192 px per side.
- Read the Safety check at the bottom before you paste an SVG from someone else into a web page.
Input<svg width="24" height="24" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" fill="teal"/>
</svg>Output<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" fill="teal"/>
</svg>Inline SVG inside HTML works without the namespace, because the HTML parser adds it. A file, an <img> or a CSS background is read as XML, and there an <svg> without xmlns is just an unknown tag, so nothing is drawn. This is the most common reason an SVG copied from a web page shows up blank.
Input<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2 2 22h20z" fill="#f59e0b"/></svg>Outputdata:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 24 24%22%3E%3Cpath d=%22M12 2 2 22h20z%22 fill=%22%23f59e0b%22/%3E%3C/svg%3EThat's 156 characters for a 107-byte icon. The same icon in Base64 takes 170. The # of the color becomes %23, because a raw # would end the URL, and quotes are encoded so the result works inside single or double quotes.
Input<svg xmlns="http://www.w3.org/2000/svg" width="120" height="60" onload="alert(1)">
<script>alert(document.domain)</script>
<image href="https://example.com/track.png" width="1" height="1"/>
<rect width="120" height="60" fill="#e11d48"/>
</svg>OutputSafety check: this SVG can run code or load files
Contains a script element: <script>
Has event handler attributes: onload
Loads files from other sites: https://example.com/track.pngThe preview shows a red rectangle and nothing else: no alert, and no request to example.com. Pasted inline into a page, both scripts would run with full access to that page, and the tracking image would load.
Why an SVG shows nothing
The viewer points out most of these. The usual causes:
- No
xmlnson the<svg>tag. Inline SVG works without it, but files,<img>tags and CSS backgrounds needxmlns="http://www.w3.org/2000/svg". - An
xlink:hrefattribute withoutxmlns:xlink="http://www.w3.org/1999/xlink"on the root. That's an XML error, so nothing renders. Current browsers also accept a plainhref. - No width, height or viewBox. Browsers then draw the SVG at 300 × 150 and may cut off the drawing. With only a viewBox, they fit the image inside 300 × 150, so a square icon shows at 150 × 150 unless your CSS sets a size.
- White shapes on a white page, or
fill="none"with no stroke. Switch Background to Black to check. - Images, fonts and stylesheets from other sites. They never load when the SVG is used as an
<img>or a CSS background, so embed images as data URIs and turn text into paths in your design app. - A
.svgzfile. It's gzip-compressed, not text, so rungzip -dc icon.svgz > icon.svgfirst.
Is an SVG file safe?
An SVG is a document, not only a picture. It can hold <script> tags, event handlers such as onload, javascript: links and HTML inside <foreignObject>. What happens depends on how the SVG is used:
- In an
<img>tag or as a CSS background, scripts don't run and no outside files load. The preview here works this way. - Opened directly in a browser tab, or embedded with
<object>,<embed>or<iframe>, scripts run on the site that serves the file. - Pasted inline into HTML, scripts run inside your page, with access to its cookies and data.
Converting SVG to PNG
A few things to know before you click Download PNG:
- The PNG keeps transparent areas transparent. The Background setting is only for viewing and isn't added to the file.
- Text uses the fonts installed on your computer, the same as in the preview. If the SVG names a font you don't have, the PNG uses a fallback, so turn text into paths when the result must match exactly.
- Sizes in
mm,cm,inorptare converted at 96 pixels per inch, so an A4 page (210mmby297mm) becomes 794 × 1123 px. - For screens with high pixel density, export at two or three times the size you'll show the image at.
- SVGs with
<foreignObject>can't be exported: the browser blocks it and you'll see a message saying so.
If your site accepts SVG uploads
An uploaded SVG that a visitor opens on your domain can run its script there, which is a classic stored cross-site scripting (XSS) hole. Show uploads only through <img>, serve them from a separate domain or with the header Content-Security-Policy: script-src 'none', and clean them with a sanitizer before you store them.
The safety check here looks for scripts, event handlers, javascript: links, embedded content and outside files. It helps you judge a file. It isn't a sanitizer, so use one like DOMPurify in your code:
// npm install dompurify jsdom
import createDOMPurify from "dompurify";
import { JSDOM } from "jsdom";
const DOMPurify = createDOMPurify(new JSDOM("").window);
// Keeps shapes, gradients and filters; drops scripts, event handlers and javascript: links.
const clean = DOMPurify.sanitize(svg, { USE_PROFILES: { svg: true, svgFilters: true } });// npm install dompurify
import DOMPurify from "dompurify";
const clean = DOMPurify.sanitize(svg, { USE_PROFILES: { svg: true, svgFilters: true } });Convert SVG to PNG in code
For many files or a build step, convert on the command line or in code. In sharp, the density option sets the drawing size: 72 draws the SVG at its own size, and 1536 draws a 24 px icon at 512 px.
// npm install sharp
import sharp from "sharp";
// 72 × (512 / 24) = 1536, so the 24 px icon comes out 512 px wide.
await sharp("icon.svg", { density: 1536 }).png().toFile("icon.png");# librsvg (brew install librsvg, apt install librsvg2-bin)
rsvg-convert -w 512 icon.svg -o icon.png
# Inkscape 1.x
inkscape icon.svg --export-type=png --export-width=512 --export-filename=icon.png# pip install cairosvg
import cairosvg
cairosvg.svg2png(url="icon.svg", write_to="icon.png", output_width=512)async function svgToPng(svg, width, height) {
const url = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml" }));
try {
const img = new Image();
img.src = url;
await img.decode();
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
canvas.getContext("2d").drawImage(img, 0, 0, width, height);
return await new Promise((resolve) => canvas.toBlob(resolve, "image/png"));
} finally {
URL.revokeObjectURL(url);
}
}Use an SVG as a data URI in CSS
Copy Data URI gives a URL-encoded string you can put straight into url("..."). To make the same string in your own code, encode the SVG and then put back the characters that are safe to leave readable. If a tool insists on Base64, the Base64 Encoder makes it, and you prefix data:image/svg+xml;base64,.
.warning::before {
content: "";
display: inline-block;
width: 1em;
height: 1em;
background: url("data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 24 24%22%3E%3Cpath d=%22M12 2 2 22h20z%22 fill=%22%23f59e0b%22/%3E%3C/svg%3E") center / contain no-repeat;
}function svgToDataUri(svg) {
const encoded = encodeURIComponent(svg.trim())
.replace(/'/g, "%27")
.replace(/%20/g, " ")
.replace(/%3D/g, "=")
.replace(/%3A/g, ":")
.replace(/%2F/g, "/");
return `data:image/svg+xml,${encoded}`;
}How do I open an SVG file?
Click Open File here or drop the file onto the editor to see the image and its code. Any browser can also open an .svg file directly, but then scripts inside it run, which never happens in this preview.
How do I convert SVG to PNG?
Load the SVG, type the width you need next to PNG width, and click Download PNG. The conversion happens in your browser and keeps transparent areas transparent.
Why is my SVG not showing?
Most often the <svg> tag lacks xmlns="http://www.w3.org/2000/svg", which files and <img> tags need. Other causes are an XML error, a missing size, or white shapes on a white page, and the viewer points out each of them.
Can SVG files contain malware?
An SVG can contain JavaScript, which runs when you open the file directly in a browser or paste it inline into a page. Shown through an <img> tag it can't run, and the Safety check here lists any scripts, event handlers and outside links.
What does viewBox mean in SVG?
viewBox="0 0 24 24" sets the drawing's coordinate system: 24 units wide and 24 high, starting at 0,0. Width and height set the display size, and the browser scales the viewBox to fit it, so one icon works at 16 px and at 512 px.
Should I use an SVG data URI or a separate file?
A data URI saves a request and suits small icons in CSS, up to about 2 KB. Larger SVGs work better as separate files, which the browser caches and reuses across pages.
Can I edit an SVG here?
Yes. Change the code and the preview updates, then click Download SVG. There are no drawing tools, so draw shapes in a design app, but colors, sizes and small fixes are often quicker in the code. To test an SVG inside a full page, paste the page into the HTML Preview.