SVG Formatter
Format SVG code online: pretty-print messy markup with consistent two-space indentation, or flatten it to a single line. The document itself is untouched — same elements, same attributes, different whitespace.
Runs entirely in your browser — nothing is uploaded, no account needed.
How it works
The file is parsed with the browser's own DOMParser and printed back out — pretty-printed with two-space indents and one element per line, or flattened to a single line with the whitespace between tags removed. Either way it is the same document: the same elements, the same attributes in the same order, the same text content. Only the whitespace between elements changes, which is why the formatter can promise a lossless round trip where an optimizer cannot. Both outputs are computed at once and their byte counts shown side by side, and everything happens in your browser — the file is never uploaded.
Text that matters is left alone. The content of <text>, <title> and <style> elements is real data, not layout, so the single-line mode only collapses runs of whitespace that sit purely between tags — the classic export artefact — and never touches whitespace inside a text node that has actual content.
What the pretty-printer does, exactly
Every element opens on its own line, indented two spaces per level of nesting. An element with no children is written self-closing — <circle … /> — and an element whose only child is a piece of text keeps it inline, so <title>Bolt badge</title> stays one readable line instead of three. Comments survive in place, because a comment in a hand-maintained file is usually there for a reason. Attribute order is preserved exactly as parsed, and attribute values are re-escaped consistently, so two files formatted here diff cleanly against each other. What does not survive is the XML prolog: the output starts at <svg>, because the <?xml … ?> declaration and DOCTYPE are optional noise for an SVG served with the right content type.
The single-line direction
Single line is for embedding, not for saving bytes. Markup that is about to sit inside a JSON string, a database field, an HTML attribute or a CMS text box breaks — or bloats into \n escapes — the moment it contains literal newlines, and one flat line sidesteps all of it. The transformation is exactly one rule: whitespace that sits entirely between a closing and an opening tag is removed, because it is layout; whitespace inside a text node that has real content is data and is kept. The byte counts under the style switch make the honest point that this saves little — indentation is a few percent of a typical file. For actual weight reduction — dropped metadata, rounded coordinates — you want the optimizer, which minifies as its final step anyway.
When to use it
Exported SVGs are written for machines: Illustrator and Figma emit everything on one line, Inkscape nests groups ten levels deep with a tangle of namespaced attributes. Before you can review a file in a pull request, diff two versions of an icon, or hand-fix one attribute, you need markup a person can read — that is the pretty-print direction, and because the document is guaranteed unchanged, formatting before committing costs nothing but readability gained. It is also the fastest way to see what a file actually contains: one minute of scrolling indented markup answers "why is this icon 30 KB" faster than any inspector. The single-line direction is the opposite move, covered above. Either result can be copied, downloaded, or applied to the shared document and carried into the editor.
Formatter or optimizer?
They answer different questions. The formatter changes how the markup looks and guarantees the document is untouched — nothing removed, nothing rounded. The optimizer changes what the markup contains: it strips editor metadata, drops unreferenced ids, collapses redundant groups and rounds coordinates, each pass switchable, and it can minify as its final step. If the goal is a smaller file, use the optimizer; if the goal is readable or embeddable markup with zero risk of a visual change, use this. And if the question is not how the file looks but whether something is wrong with it, the viewer runs the checks.
Common problems
- Nothing appears in the output. The markup does not parse as XML — an unclosed tag, a stray ampersand, mismatched quotes. A formatter needs a parsed document to print. Paste the markup into the viewer's validation box to get the parser's error with its line number, fix that, and format again.
- The pretty file is bigger than the input. Correct and expected — indentation is added whitespace. Readability and weight pull in opposite directions; format for people, minify (or better, gzip) for the wire.
- Multi-line text shifted slightly. Pretty-printing puts each
<tspan>on its own indented line, and SVG renders the line break between them as a space. For files with carefully spaced text spans, use single-line mode, which introduces no whitespace at all. - The XML declaration is gone. By design — the output begins at the root element. An SVG served as
image/svg+xmlor inlined into HTML has no use for the prolog; add one back manually if a legacy consumer insists.
Questions
Is this SVG formatter free?
Yes. Formatting is free, with no account, no watermark and no limit on how many files you run through it. The site is funded by advertising.
Does formatting change how the SVG renders?
No. The formatter re-parses the document and prints it back with different whitespace between elements — same elements, same attributes in the same order, same text content. It is the one transformation on this site that is lossless by construction.
Is my SVG uploaded anywhere?
No. Parsing and printing both happen in your browser with the DOM’s own parser. Nothing you drop or paste here leaves your machine.
What does “beautify SVG” actually do here?
Pretty-printing: one element per line, nested elements indented two spaces per level, empty elements written self-closing, and an element with only text kept on one line. The point is markup a person can read and a diff tool can compare line by line.
Can I change the indentation size or use tabs?
No — output is always two spaces per level. A single fixed convention is the feature: two files formatted here always diff cleanly against each other, which is what formatting is usually for.
Does the single-line mode make the file smaller?
Only slightly — it removes indentation and newlines, which are a few percent of a typical file. Its real purpose is embedding: markup destined for a JSON string, a CMS field or an HTML attribute where literal newlines break things. For real byte savings use the optimizer.
Are comments and text content preserved?
Yes. Comments are printed back in place in both modes, and the content of text, title and style elements is treated as data, not layout — the single-line mode only removes whitespace that sits purely between tags.
Does it reorder or normalise attributes?
No. Attributes come out in the order they were parsed, so the formatted file diffs sensibly against the original. Values are consistently re-escaped, which is the only normalisation applied.
Why do I get no output for my file?
The markup is not well-formed XML — a formatter can only print what parses. Paste it into the viewer's validation box to see the parser's own error with a line number, fix that, and come back.
Where did the <?xml?> declaration go?
The output starts at the root svg element; the XML prolog and DOCTYPE are dropped. Neither is needed for SVG served with the right content type or inlined into HTML. If a legacy pipeline requires the declaration, paste it back onto the first line.
Should I format or optimize before committing an icon to a repo?
Both, in that order of decision: optimize to remove export cruft, with its minify option off so the output stays one element per line, and you get a small and diffable file at once. Use the formatter alone when the file must provably not change at all.
Notes
- Formatting only touches whitespace between elements. Text content inside <text>, <title> and <style> is left exactly as written.
- Minified here means one line — for actual byte savings (dropped metadata, rounded coordinates) use the Optimizer, which also minifies.
- Attribute order is preserved as parsed, so diffs against the input stay readable.