Why Your SVG Does Not Show in a Browser
Twenty-two files, each with exactly one thing wrong, each loaded twice — once as
<img src> and once pasted into the page — and the ink counted from a screenshot
diff. Six of them paint nothing as a file and a full 3,764 pixels when pasted;
11 paint nothing either way; three turn out not to be defects at all; and one does the
opposite, painting fine as a file and nothing when pasted. Two numbers make the diagnosis
harder than it should be. Only three of those six file-only failures print an error
message, and all 45 requests returned HTTP 200. A blank SVG is usually not a broken
server and often not a broken file — it is a file the browser declined to interpret.
Every case below is the same 100 × 60 badge in a 200 × 120 slot, so the only variable is the defect. The control paints 3,764 pixels in both modes; anything at zero is genuinely blank, not faint.

The test: one badge, one thing wrong per file
The badge is a rounded rectangle, a circle and a two-unit line, 270 bytes. Each of the other twenty-one files is that file with one change: a namespace removed, a declaration moved, a viewport zeroed, a fill set to none. Each is loaded as an image and then pasted into an HTML page, screenshotted, and compared against white with a threshold of 8 of 255 per channel. Requests and status codes come from the server log, not from page events, because a page event fires for requests the browser then refuses to make.
The two loading modes are not a technicality. An <img> sends the bytes to an
XML parser and asks for a document in the SVG namespace. Pasting the same text into an HTML page
sends it to the HTML parser, which is forgiving by design: it lowercases element names, invents
missing namespaces and swallows a stray ampersand. Most of what follows is that difference, seen
from the file's side.
Six failures that only happen in a file
| What is wrong | Inline | Err |
|---|---|---|
no xmlns | 3,764 | no |
xmlns uses https:// | 3,764 | no |
<?xml?> not the first byte | 3,764 | yes |
| element names in capitals | 3,852 | no |
xlink: with no xmlns:xlink | 3,852 | yes |
a bare & in text | 3,764 | yes |
Every one of these files paints 0 as an <img>; the Inline column is what the
same markup paints when pasted into the page, in ink pixels of a 200 × 120 slot. The control
paints 3,764 in both modes, so 3,852 means a drawing with one shape instead of three.
The namespace pair is the classic. The SVG namespace is
http://www.w3.org/2000/svg — an identifier, not a URL you can fetch — so rewriting it
to https:// puts every element in a namespace the browser has never heard of. The
document parses, the tree exists, and nothing draws. No error, because there is nothing wrong with
the XML.
Capital letters fail the same way for a different reason. XML is case sensitive, so
<SVG> is not the SVG element; it is an unknown element that happens to contain
children. Paste the same text into HTML and the parser lowercases it into a working drawing —
3,852 pixels, because the pasted version has one rectangle and the control has three shapes.
Half of them print nothing at all
Open those six files directly in Chrome and three produce a message: Invalid processing instruction: <?xml for the late declaration, Attribute xlink:href prefix is unbound for the undeclared prefix, and Unexpected token inside entity for the bare ampersand. Those three are syntax errors, and the XML parser is happy to say so.
The other three are not syntax errors. A file with no namespace, the wrong namespace or capital element names is well-formed XML — it is simply XML about nothing the renderer knows. That is the worst diagnostic situation there is: a valid document that draws zero pixels and reports zero problems. If you are looking for a message in the console and there is none, the namespace is the first thing to check, not the last.
The server said 200
All 45 requests in this test returned HTTP 200, including every file that painted nothing. The status code describes the transfer, not the drawing. That is worth stating plainly because "the file loads, I can see it in the network tab" is the most common reason a blank SVG gets diagnosed as a CSS problem.
The one case where the status was genuinely not the whole story: the control file served with
Content-Type: text/plain instead of image/svg+xml. Chrome refused it as an
image — naturalWidth 0, a broken-image icon, 0 painted pixels — while the bytes
on the wire were identical to the working file. If an SVG works when opened directly and fails on
the page, check the response headers before you check the markup; this is also why a file served
from a development server with the wrong MIME type can look "corrupt" while being perfect.
Eleven failures that happen either way
These are defects in the drawing, not in how it is delivered, and they survive both modes. Grouped by what they touch:
- The box. A
viewBoxwith a zero width, andwidth="0" height="0", both paint nothing. The element is present with a real bounding box of 88 × 44 inside it — the second one collapses the element itself to 0 × 0. - The paint.
display:nonepasted,visibility="hidden",opacity="0", a rootfill="none"that nothing overrides, and a drawing whose every fill is white on a white page. The last one is not a bug and is worth keeping in mind: it paints, it is simply the same colour as the background. - The reference. A
<use href="#badge">pointing at an id that does not exist draws nothing and reports nothing. Measured bounding box: 0 × 0. - The geometry. Art placed outside the viewBox, and a
transformon the root<svg>, which is ignored. Both leave a bounding box of 88 × 44 at x = 506 while the visible area stays empty — the file is fine and the crop is not. - The content. A file that contains only
<defs>, and a path whosedis malformed. The second one is the subject of the path command guide: the parser keeps everything before the bad token and drops the rest.
The off-canvas cases are the ones people argue about, because "the element is there" is true.
getBBox() reports 88 × 44 at x = 506 whether or not a single pixel lands inside
the viewBox. Geometry and visibility are separate questions, and only a screenshot answers the
second one.
Three things that are not the cause
A UTF-8 byte-order mark in front of the XML declaration is handled correctly by Chrome in both
modes. A file with neither a viewBox nor a width/height
renders — it simply reports an intrinsic size of 300 × 150, which is the fallback, not your
drawing. And a nested <svg> without its own size fills its parent and draws
normally.
These matter because they are the three things people remove first when a file misbehaves. The BOM is not your problem, the missing size is a layout problem rather than a blank one, and nesting is legal. Removing them changes nothing and delays the actual diagnosis.
One file that only fails when pasted
The mirror image of the first group: style="display:none" on the root painted
3,764 pixels as an image and nothing when pasted. Chrome does not apply the root element's
display when the SVG is the document of an <img> — the file renders
as though the property were not there — while the same markup inside an HTML page hides itself,
along with its box, which collapses to 0 × 0.
This is a good reminder that the two parsers disagree in both directions, and that a
display:none "fix" applied to a file may do nothing at all for the visitors who see it
as an image. If a drawing must be hidden, control it from the page with a class, not from the
file's root element.
Diagnose in the order the browser decides
The cases sort into a sequence, and running the sequence in order avoids the usual hour of
guessing. First the response: is it 200, and is the content type image/svg+xml.
Second the XML: does it parse, and is the root in the SVG namespace. Third the box: a positive
viewBox, a non-zero width, an element that is not collapsed. Fourth the paint: nothing hidden,
nothing all-white, nothing off-canvas. Fifth the references: every href resolves.
Steps two, four and five are string work, and they cover sixteen of the twenty-two files:
// The six problems that only exist once the file is parsed as XML.
function fileIssues(svg) {
const out = [];
const body = String(svg).replace(/^\uFEFF/, '');
if (!/<svg[^>]*\sxmlns\s*=\s*"http:\/\/www\.w3\.org\/2000\/svg"/.test(body)) {
out.push(/xmlns\s*=\s*"https:\/\/www\.w3\.org\/2000\/svg"/.test(body)
? 'xmlns points at https:// instead of http://'
: 'no xmlns="http://www.w3.org/2000/svg"');
}
if (body.indexOf('<?xml') > 0) {
out.push('the XML declaration is not the first byte of the file');
}
if (/<(SVG|RECT|CIRCLE|PATH|G|DEFS|USE|TEXT|LINEARGRADIENT)[\s/>]/.test(body)) {
out.push('an element name is in capitals');
}
if (/xlink:[a-zA-Z-]+\s*=/.test(body) && !/xmlns:xlink\s*=/.test(body)) {
out.push('xlink is used but xmlns:xlink is never declared');
}
if (/&(?!(amp|lt|gt|quot|apos|#\d+|#x[0-9a-fA-F]+);)/.test(body)) {
out.push('a bare & that is not a character reference');
}
return out;
}
// Problems that survive any parser: the file is valid and still paints nothing.
function paintIssues(svg) {
const out = [];
const box = svg.match(/viewBox\s*=\s*"([^"]*)"/);
if (box) {
const n = box[1].trim().split(/[\s,]+/).map(Number);
if (n.length !== 4 || !(n[2] > 0) || !(n[3] > 0)) out.push('the viewBox has no positive width and height');
}
const width = svg.match(/<svg[^>]*\swidth\s*=\s*"([^"]*)"/);
if (width && parseFloat(width[1]) === 0) out.push('width is 0');
if (/display\s*:\s*none/.test(svg)) out.push('display:none');
if (/<svg[^>]*\svisibility\s*=\s*"hidden"/.test(svg)) out.push('visibility:hidden on the root');
if (/<svg[^>]*\sopacity\s*=\s*"0"/.test(svg)) out.push('opacity 0 on the root');
if (/<svg[^>]*\sfill\s*=\s*"none"/.test(svg)
&& !/<(rect|circle|path|text|polygon)[^>]*\sfill\s*=\s*"(?!none)/.test(svg)) {
out.push('fill="none" on the root and nothing overrides it');
}
if (/<svg[^>]*\stransform\s*=/.test(svg)) {
out.push('a transform on the root <svg> is ignored');
}
const fills = [...svg.matchAll(/fill\s*=\s*"([^"]+)"/g)].map((m) => m[1].toLowerCase());
if (fills.length && fills.every((f) => f === '#fff' || f === '#ffffff' || f === 'white')) {
out.push('every declared fill is white');
}
const ids = new Set([...svg.matchAll(/\sid\s*=\s*"([^"]+)"/g)].map((m) => m[1]));
for (const m of svg.matchAll(/<use[^>]*href\s*=\s*"#([^"]+)"/g)) {
if (!ids.has(m[1])) out.push(`use points at #${m[1]}, which is not in the file`);
}
const visible = svg.replace(/<defs[\s\S]*?<\/defs>/g, '');
if (!/<(rect|circle|ellipse|line|polyline|polygon|path|text|image|use)[\s/>]/.test(visible)) {
out.push('no painted element outside defs');
}
return out;
}
The two that string rules cannot catch are the off-canvas drawing and the malformed path — one
needs getBBox() compared against the viewBox, the other needs the path scanner from
the path command
guide. Everything else can be decided before a browser is involved. The wrapper puts the
response first, because a MIME problem makes every other finding irrelevant:
// One response in, one ordered verdict out. The order is the order the browser
// decides in: bytes, then content type, then XML, then the box, then paint.
function diagnose(resp) {
const type = String(resp.contentType || '').toLowerCase();
const text = String(resp.text || '');
const found = [];
if (resp.status && resp.status !== 200) found.push(`HTTP ${resp.status}`);
if (!/image\/svg\+xml/.test(type)) {
found.push(`served as ${type || 'an unknown type'}, not image/svg+xml`);
}
found.push(...fileIssues(text));
found.push(...paintIssues(text));
const inlineOnly = found.filter((f) => /xmlns|declaration|capitals|xlink|bare &/.test(f));
return {
okay: found.length === 0,
issues: found,
verdict: found.length === 0 ? 'renders'
: inlineOnly.length === found.length ? 'fails only as a file' : 'fails either way',
};
}
Run it in the build, not in the browser. A file that draws nothing is the one failure mode where the page looks "almost right" — a header with a gap, a button with no icon, a card with an empty square — and the cost of not noticing is paid by whoever has to debug it from the screenshot. The security side of accepting such a file from a stranger is a separate question, covered in whether an SVG can carry malware; the rendering side of choosing a container at all is in inline SVG versus the img tag.
Related SVG Workflows
A blank drawing is a question about order: bytes, then namespace, then box, then paint.
- SVG Path Commands, All 20 — what a broken
ddoes, measured against 29 paths. - Inline SVG vs the img Tag — the two loading modes here, compared across five containers.
- SVG viewBox vs width and height — why a file with no size reports 300 by 138.
- Removing SVG Metadata — nine edits to a real export, and the one that stops it rendering entirely.
- Can SVG Files Contain Malware? — what else a file can carry besides a drawing.
- SVG Namespace Security — the namespace rules from the other side, as an attack surface.
- SVG Validator — the same five checks, run in the browser on a file you paste.
Try These Free Online Tools
No download required — convert, compress & optimize SVGs right in your browser



