---
title: "SVG Documents Explained: Structure, Elements and Examples"
slug: "svg-document-structure"
description: "How SVG documents are structured: the root element, namespace, viewBox, groups, definitions, reuse, drawing order and the errors that stop a file rendering."
primaryKeyword: "svg documents"
secondaryKeywords: []
relatedTools:
  - "/svg-formatter"
  - "/svg-validator"
  - "/svg-viewer"
date: "2026-11-27"
author: "MintConvert Team"
---

# SVG Documents Explained: Structure, Elements and Examples

Open an exported graphic in a text editor and you may see a wall of tags. Once you know how the parts fit together, you can find the shape you want and spot why something is broken.

This guide is about what's inside the markup. For the basics of the format, read [what an SVG file is](/blog/what-is-an-svg-file).

## A complete example

```svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 60" role="img" aria-labelledby="t d">
  <title id="t">Two leaves</title>
  <desc id="d">Two green circles side by side</desc>
  <defs>
    <circle id="dot" r="20" />
  </defs>
  <g fill="#10b981">
    <use href="#dot" x="35" y="30" />
    <use href="#dot" x="85" y="30" />
  </g>
</svg>
```

## The root element

Everything sits inside `<svg>`. The W3C specification calls an `svg` element and its contents an [SVG document fragment](https://w3c.github.io/svgwg/svg2-draft/struct.html). It can be a standalone file or part of another document.

| Attribute | What it does |
|---|---|
| `xmlns` | Declares the SVG namespace. Standalone files need it |
| `viewBox` | Sets the internal coordinates: min-x, min-y, width, height |
| `width`, `height` | Set the size of the outermost graphic. CSS can also size it |
| `preserveAspectRatio` | Controls the fit when the box and `viewBox` shapes differ |

When SVG markup is written inline in an HTML page, the HTML parser handles the namespace for you.

## Title and description

`<title>` gives the graphic a short name, and `<desc>` adds detail. Connecting them with `aria-labelledby`, as above, helps assistive technology. Test with a screen reader if the graphic carries important information, since support varies.

## Groups, definitions and reuse

- **`<g>`** groups elements. Presentation attributes such as `fill` can be inherited by its children, and a `transform` moves the whole group.
- **`<defs>`** holds things you define now and use later, such as gradients and clip paths. Its contents aren't drawn directly.
- **`<symbol>`** is a reusable template that can have its own `viewBox`.
- **`<use>`** draws a copy of an element you reference by `id`.

An `id` should be unique. This matters when you place several graphics inline on one page, because repeated IDs can make a gradient or clip path point to the wrong definition.

## Drawing order

Elements are painted in document order. In general, a later element appears on top of an earlier one. If a shape is hidden, check whether something after it covers it.

## Styling

You can set appearance with attributes (`fill="red"`), a `style` attribute, or a `<style>` block. CSS rules override presentation attributes, which explains why changing `fill` sometimes does nothing. Inline `<style>` rules can also affect other elements on the same HTML page, so use specific class names.

## Reading a messy file

Exports are often minified or full of editor data. An [SVG formatter](/svg-formatter) adds indentation so you can follow the nesting. Keep an untouched copy before editing.

## Why a document fails to render

| Symptom | Likely cause |
|---|---|
| XML parsing error | Unclosed tag, unquoted attribute or a bare `&` (write `&amp;`) |
| Works inline but not as a file | Missing `xmlns` |
| Blank area | Shapes fall outside the `viewBox` or have no visible paint |
| Gradient or clip is missing | The referenced `id` doesn't exist or is duplicated |
| Picture or font is missing | The file depends on an external resource |

An [SVG validator](/svg-validator) checks syntax and references. To see the result as you work, use an [SVG viewer](/svg-viewer).

## Frequently asked questions

### Is an SVG document the same as an XML document?

A standalone SVG file is an XML document that uses SVG elements. Inline SVG in HTML follows HTML parsing rules.

### Can one SVG sit inside another?

Yes. A nested `svg` element creates a new viewport with its own coordinate system.

### Does the order of attributes matter?

No. The order of elements matters for drawing, but the order of attributes within a tag doesn't.

### How do I put a photo inside the markup?

Use the `<image>` element. See our guide to [adding pictures inside SVG](/blog/svg-image-element).

## Key takeaways

Start at the root, check `xmlns` and `viewBox`, then follow the groups and references. Keep IDs unique and remember that later shapes are painted over earlier ones.