What Is SVG and How Does It Use XML Syntax?
Scalable Vector Graphics (SVG) is an open-standard, text-based file format used to render resolution-independent, two-dimensional graphics on the web. This article explains what SVG is, how it leverages Extensible Markup Language (XML) to construct visual elements mathematically, and why its text-based architecture makes it uniquely versatile for modern digital design and web development.
What Is SVG?
SVG is a vector image format developed by the World Wide Web Consortium (W3C). Unlike traditional raster image formats like JPEG, PNG, or GIF—which store graphic data as a grid of colored pixels—SVG stores visual information as mathematical formulas.
Because SVG graphics are defined geometrically using points, lines, curves, and shapes, they can be scaled up or down to any dimension without losing quality or becoming pixelated. This makes SVG ideal for responsive web design, user interface icons, logos, charts, and illustrations across devices with varying screen resolutions.
How SVG Uses XML to Build Graphics
XML (Extensible Markup Language) is a standard format designed to store and transport data in a readable, hierarchical text format. SVG uses XML syntax to define graphic components as structured tags and attributes, effectively turning visual design into structured code.
An SVG file is essentially a plain-text document composed of XML elements. Each element corresponds to a specific visual shape or container, and its attributes define size, position, color, and behavior.
1. The Root <svg>
Element
Every SVG graphic begins and ends with the <svg>
container tag. This element establishes the coordinate system and
defines the bounding box of the graphic through attributes like
width, height, and viewBox.
<svg width="200" height="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<!-- Visual elements go here -->
</svg>The viewBox attribute specifies the internal coordinate
space (min-x, min-y, width, height), allowing the graphic to scale
dynamically relative to its container.
2. Basic Shape Elements
SVG provides dedicated XML tags for standard geometric shapes.
Attributes within these tags determine coordinate positioning
(x, y, cx, cy) and
visual styling (fill, stroke,
stroke-width):
- Rectangles:
<rect x="10" y="10" width="100" height="50" fill="blue" /> - Circles:
<circle cx="50" cy="50" r="40" fill="red" stroke="black" stroke-width="2" /> - Lines:
<line x1="0" y1="0" x2="100" y2="100" stroke="green" /> - Polygons:
<polygon points="50,5 90,90 10,90" fill="orange" />
3. Complex Shapes with the
<path> Element
For freeform shapes, curves, and intricate illustrations, SVG uses
the <path> element. The shape is defined inside the
d (data) attribute using a series of mathematical drawing
commands:
M(Move To): Sets a starting point.L(Line To): Draws a straight line to a point.C/S(Cubic Bézier Curve): Draws smooth curves using control points.Z(Close Path): Closes the current path back to the starting point.
<path d="M 10 80 Q 52.5 10, 95 80 T 180 80" fill="none" stroke="black" />4. Text and Grouping
Text in an SVG is rendered using the <text>
element, keeping the words selectable and readable by search engines.
Elements can also be grouped together using the <g>
tag, allowing transformations, styles, and scripts to be applied to
multiple components simultaneously.
Advantages of an XML-Based Format
Because SVG graphics are written in XML, they offer distinct advantages over binary raster formats:
- DOM Integration: SVG elements exist in the web browser’s Document Object Model (DOM). Developers can style them directly with CSS (e.g., hover effects, color changes) and manipulate them using JavaScript to create interactive animations.
- Accessibility and SEO: Screen readers can parse
internal
<title>and<desc>tags, and search engine crawlers can read the embedded text elements inside the XML code. - Small File Sizes: Vector code is lightweight and compresses efficiently using Gzip or Brotli, which improves website load times.
- Human-Readable and Editable: Developers and designers can open an SVG file in any text editor to modify coordinates, colors, or classes without needing dedicated graphic editing software.