PDFSVG

SVG validation guide

How to validate SVG code

A file can be valid XML and still render badly, contain risky markup, or lack an accessible name. Check each layer separately so you know what needs fixing.

To validate an SVG, first check that it is well-formed XML with an <svg> root. Then preview it, inspect the viewBox and internal references, scan for unsafe markup, and review accessibility. One green check cannot prove all five.

What does SVG validation actually mean?

SVG is an XML-based graphics language. The SVG 2 conformance rules describe a stand-alone SVG file as well-formed XML with an svg root. That is the starting point, not the whole review.

The W3C also notes that an image/svg+xml processor expects well-formed XML but cannot assume the document is valid against a particular DTD or schema. In practice, “validate SVG” can refer to several different checks.

CheckWhat it findsWhat it does not prove
XML parsingBroken tags, quotes, entities, and nestingCorrect appearance or safe content
Browser previewBlank output, clipping, and obvious rendering changesConsistent output in every app
Safety reviewScripts, event handlers, and unsafe linksA complete upload security policy
Accessibility reviewMissing names, roles, ARIA references, and duplicate IDsFull WCAG conformance in its final page context

How to validate an SVG file step by step

  1. Parse the XML. Open the file in the SVG code editor. A missing closing tag, unquoted attribute, or stray character can stop the document from parsing.
  2. Confirm the root element. A stand-alone file should have an <svg> root in the SVG namespace, normally written as xmlns="http://www.w3.org/2000/svg".
  3. Check the canvas. Inspect viewBox, width, and height. The SVG 2 viewBox definition requires four numbers, and zero width or height disables rendering.
  4. Preview the result. Look for blank artwork, clipping, lost gradients, missing fonts, and unexpected external assets. Use the SVG viewer when you want inspection without editing.
  5. Review links and executable markup. Unknown SVG can contain scripts, event attributes, or remote references. Run it through the SVG cleaner before publishing or converting it.
  6. Check its final use. If the graphic conveys meaning, review its name and ARIA relationships with the SVG accessibility checker. Then test it where it will actually appear.

Common SVG errors and practical fixes

XML parsing error

Check the line named in the error. Look for a missing closing tag, mismatched quote, bare ampersand, duplicate attribute, or content pasted outside the root element. Fix one syntax error at a time because the first error can hide the next one.

The SVG parses but appears blank

Check for a missing or malformed viewBox, zero width or height, artwork positioned outside the canvas, white artwork on a white background, or a broken url(#id) reference.

The SVG works in one app but not another

External fonts, CSS, linked images, filters, masks, and editor-specific metadata do not travel equally well. Keep the original, clean a copy, and compare the result in the browser and the destination app.

The file is valid but not safe to publish

XML validity does not make active content harmless. Remove scripts, inline event handlers, unsafe URLs, and unsupported elements. If your product accepts user uploads, enforce a server-side allowlist too.

Does every valid SVG need a viewBox?

An SVG can parse without a viewBox, but responsive scaling becomes harder and the artwork may be clipped or sized unexpectedly. The attribute contains min-x, min-y, width, and height. Use the SVG viewBox editor to inspect or fit the canvas around existing artwork.

Validation is not the same as accessibility testing

A parser cannot decide whether an image is meaningful in its page context. W3C guidance recommends an accessible name for SVG content that people need to understand, using a direct child <title>, aria-label, or aria-labelledby. Decorative graphics should be hidden from assistive technology when appropriate.

Common questions

How do I validate SVG code?

Start by parsing the SVG as XML and confirming that it has an svg root. Then check its viewBox and dimensions, preview the artwork, review scripts and links, and test accessibility in the final page context.

Why does my SVG fail to parse?

Common causes include an unclosed tag, a mismatched quote, a bare ampersand, a duplicate attribute, or content outside the root svg element. Fix the first reported syntax error, then parse the file again.

Can an SVG be valid but still appear blank?

Yes. Valid XML can still have a zero-size or incorrect viewBox, artwork outside the canvas, broken internal references, missing external assets, or colors that disappear against the background.

Is a valid SVG safe to upload?

Not necessarily. XML validity does not remove scripts, event handlers, unsafe URLs, or remote references. Clean unknown files and use a server-side allowlist when accepting public uploads.

Does SVG validation prove WCAG compliance?

No. Automated checks can find missing names, broken ARIA references, duplicate IDs, and possible focus targets. Meaning, contrast, keyboard behavior, and assistive technology support still need review in context.