Quick Navigation

SVG Code Explained — Read, Write & Debug SVG Markup

Published: June 16, 2026
Published by SVGMaker Team
SVG Code Explained — How to Read, Write & Debug SVG Markup

You download an icon from a design library, drop it into your project folder, and out of curiosity, open it in a text editor. Instead of an image, you see this:

svg

Angle brackets, cryptic letters like M and L, numbers separated by spaces. It looks like someone tried to write HTML while solving a math problem. Your instinct is to close the file and never look at it again.

Don't. That wall of text is simpler than it looks.

SVG code is just a set of instructions that tell the browser: “Draw a circle here. Draw a line from this point to that point. Fill it with this color.” Every SVG file, from a simple arrow icon to a complex illustration, is built from the same small vocabulary of tags and attributes.

By the end of this guide, you'll be able to open any SVG file and understand what each line does, write basic SVG shapes from scratch, and diagnose why an SVG isn't rendering the way you expect. If you want to follow along with live preview as you read, paste any code example into SVGMaker's SVG Code Editor online. It updates in real time as you type.

What Is SVG Code, Really?

SVG stands for Scalable Vector Graphics. Unlike raster formats like PNG or JPG, which store images as grids of colored pixels, SVG files contain XML markup which are human-readable instructions that describe shapes mathematically. A circle isn't “these 4,000 pixels are blue.” It's “draw a circle at position (50, 50) with radius 40 and fill it blue.”

Here's the simplest valid SVG you can write:

svg

That's it. Two tags. The <svg> wrapper defines the canvas. The <circle> draws a blue circle in the center. Save this as a .svg file, open it in a browser, and you'll see a perfect circle that stays sharp whether you display it at 16 pixels or 16,000 pixels.

This distinction matters for every downstream use case:

PropertySVGPNG / JPG
Made ofXML code (math instructions)Pixel grid
Scales without blurYes, infinitelyNo, pixelation
Editable in a text editorYesNo
Supports CSS stylingYesNo
Typical icon file size< 1 KB5–15 KB
Searchable / accessibleYes (<title>, <desc>)No (needs alt on <img>)

Because SVG is code, you can version-control it with Git, search through it with grep, and manipulate it programmatically with JavaScript. For a broader introduction to creating SVG files from scratch, see our complete beginner's guide to SVG.

Anatomy of an SVG File — The Tags That Matter

Every SVG file follows the same structure: a root <svg> element that defines the coordinate space, filled with child elements that draw shapes. Let's break it down layer by layer.

The <svg> Root Element

The root tag is where you define how the SVG measures and scales:

svg
  • xmlns — Declares this as an SVG document. Required when the SVG is used as a standalone file.
  • viewBox="0 0 24 24" — Defines the internal coordinate system: starting at (0,0), extending 24 units wide and 24 units tall. This is the most important attribute in SVG — it controls how all coordinates inside are interpreted.
  • width / height — The display size on screen. If you omit these, the SVG fills its container. The viewBox scales to fit.

Think of viewBox as the canvas size and width/height as the frame size. A viewBox of 0 0 24 24 with a width of 200 means every 1 unit in your SVG code is rendered as roughly 8.3 pixels on screen.

Core Shape Elements

SVG provides seven primitive elements for drawing. Every complex SVG — no matter how intricate — is built from combinations of these:

ElementWhat It DrawsKey AttributesCommon Use
<rect>Rectangle / squarex, y, width, height, rxButtons, cards, backgrounds
<circle>Circlecx, cy, rAvatars, status dots, bullets
<ellipse>Ovalcx, cy, rx, ryBadges, loaders
<line>Straight linex1, y1, x2, y2Dividers, tick marks
<polyline>Connected line segmentspointsCharts, checkmarks
<polygon>Closed shape from pointspointsStars, hexagons, triangles
<path>Anythingd (path data)Icons, logos, complex curves

Here's a sampler that draws four shapes on one canvas:

svg

A rounded rectangle, a blue circle, a diagonal line, and an orange triangle — all in eight lines of code.

Grouping and Structure — <g>, <defs>, <use>

As SVGs grow more complex, you need ways to organize elements:

  • <g> — Groups elements together. Apply a transform or style to the group, and every child inherits it.
  • <defs> — Defines reusable elements that aren't rendered until referenced. Think of it as a “template storage” area.
  • <use> — References an element defined in <defs> (or elsewhere) by its id. This is the DRY principle applied to SVG.
svg

One circle definition, three instances. Change the fill color in <defs> and all three dots update simultaneously.

Understanding SVG Path Syntax — The d Attribute Decoded

The <path> element is the most powerful and most confusing part of SVG. Its d attribute contains a mini drawing language — a sequence of single-letter commands followed by coordinates. Once you learn the commands, you can read any path data.

CommandNameParametersWhat It Does
M / mMove Tox yPicks up the pen, moves to a point
L / lLine Tox yDraws a straight line to a point
H / hHorizontal LinexDraws a horizontal line
V / vVertical LineyDraws a vertical line
C / cCubic Bezierx1 y1 x2 y2 x yDraws a smooth curve (two control points)
S / sSmooth Cubicx2 y2 x yShorthand curve (mirrors previous control point)
Q / qQuadratic Bezierx1 y1 x ySimpler curve (one control point)
A / aArcrx ry rotation large-arc sweep x yDraws an elliptical arc
Z / zClose PathDraws a straight line back to the start

Uppercase commands use absolute coordinates. Lowercase commands use relative offsets from the current point.

Here's a simple house icon, built step by step:

svg

Reading the path: Move to (5, 15). Draw a line up-right to (15, 5) — that's the left roof slope. Line down-right to (25, 15) — the right roof slope. Line straight down to (25, 25) — the right wall. Line left to (5, 25) — the floor. Close the path back to (5, 15) — the left wall.

Six commands, one complete shape. Every icon you've ever used follows this same logic, just with more coordinates.

If path syntax still feels abstract, paste any SVG into SVGMaker's SVG online Code Editor and change one number at a time. The live preview shows exactly which part of the shape each coordinate controls.

Styling SVG — Attributes, Inline CSS, and Style Blocks

SVG elements can be styled three ways. Each has different trade-offs.

Presentation attributes — directly on the element:

svg

Inline CSS — via the style attribute:

svg

<style> block — inside the SVG, using CSS classes:

svg
MethodProsConsBest For
Presentation attributesSelf-contained, no CSS conflictsVerbose with many elementsIcons, simple graphics
Inline styleFamiliar CSS syntaxCannot be overridden externallyOne-off overrides
<style> blockDRY, class-basedCan conflict with page CSS when embeddedMulti-element SVGs, theming
External stylesheetFull separation of concernsOnly works when SVG is inlined (not <img>)Web apps with inline SVGs

For most standalone SVG files (icons, logos), presentation attributes are the safest choice — they travel with the element and never conflict with external stylesheets. For detailed optimization strategies, see our SVG optimization techniques guide.

Writing SVG Code — Building an Icon From Scratch

Let's put it all together by building a notification bell icon from an empty canvas.

Step 1 — The bell body (a path with curves):

svg

Step 2 — The clapper (a small line at the bottom):

svg

Step 3 — The notification dot (a filled circle):

svg

Complete bell icon:

svg

Three elements. One bell with a red notification badge. Paste each step into the Code Editor to watch it take shape piece by piece.

Hand-coding is great for learning and for small tweaks, but for production icon sets, you don't need to write every path by hand. Tools like SVGMaker generate clean SVG code from a text prompt in seconds and then you refine the output in the Editor with full control over every attribute.

Debugging SVG — Finding and Fixing Common Markup Bugs

Knowing how to read and write SVG is half the skill. The other half is figuring out why an SVG isn't rendering correctly. Here are the four most common bugs and how to fix each one.

Bug 1: SVG Renders as Blank (Nothing Visible)

The number one cause: your shapes are drawn outside the viewBox boundaries.

BROKEN: shapes at 0-24, but viewBox covers 0-100
svg
FIXED: viewBox matches the content coordinates
svg

The second most common cause: fill="none" with no stroke set. The shape exists but is completely invisible. Always check that at least one of fill or stroke has a visible value.

Bug 2: Colors Not Showing or Wrong Colors

If your SVG uses fill="currentColor", the color is inherited from the parent element's CSS color property. No parent color set? The fill defaults to black — or nothing, depending on the context.

Another culprit: a <style> block inside the SVG that sets fill: none on all paths, overriding your inline attributes. CSS specificity rules apply inside SVG just like in HTML.

Bug 3: SVG Looks Correct Locally but Breaks When Embedded

Two common causes:

  1. Missing xmlns — When an SVG is used as a standalone file (via <img> or as a background), it must include xmlns="http://www.w3.org/2000/svg". Without it, the browser doesn't recognize it as SVG.
  2. CSS class name collisions — If your SVG has a <style> block with generic class names like .icon or .bg, they can conflict with your page's CSS when the SVG is inlined. Use unique prefixes or switch to presentation attributes.

Bug 4: Paths Render With Jagged or Wrong Curves

This usually means the path data has the wrong number of parameters for a command. A cubic Bezier C requires six numbers (two control points plus an endpoint). If you accidentally provide four, the browser tries to interpret the remaining path data as the next command — producing distorted shapes.

For cleaning up messy path data from AI-generated SVGs, see our guide on cleaning up AI-generated SVG code.

Using Browser DevTools for SVG Debugging

Open DevTools (F12), find the <svg> element in the Elements panel, and hover over child elements — the browser highlights each shape on the page. You can:

  • Edit attributes live (change a fill, adjust a cx coordinate) and see instant results.
  • Check the Computed Styles tab for CSS specificity conflicts.
  • Toggle individual elements on/off by setting display: none to isolate the problem.

From SVG Code to Production — Exporting and Converting

Once your SVG code is working correctly, the next step depends on where it's going:

TargetFormat NeededHow to Get There
Web (<img> tag).svg fileSave your code as .svg
Web (inline in HTML)Raw SVG markupPaste directly into your HTML
React componentJSXConvert with the SVG Code Editor (one-click React export)
Figma / design tools.svg fileImport the file directly
Cricut / laser cutter.svg or .dxfConvert with the SVG Converter
Social media / email.png or .jpgExport raster from the SVG Editor with code

Frequently Asked Questions

1. What is SVG code?

SVG code is XML markup that describes vector graphics using mathematical shapes and paths instead of pixels. It's human-readable text — any text editor can open and edit it, and all modern browsers render it natively without plugins.

2. How do I convert SVG code to a visible SVG image?

Save the code with a .svg file extension and open it in any web browser. Or paste it into an online svg code editing tool like SVGMaker to see an instant visual preview alongside the code.

3. Can I generate SVG code from a text description or an image?

Yes. AI tools like SVGMaker generate SVG code from text prompts (e.g., “a flat icon of a shopping cart”). You can also convert raster images (PNG, JPG) to SVG code using the raster to svg Converter.

4. What is the viewBox attribute and why does my SVG look cropped?

viewBox defines the internal coordinate system of the SVG — the range of x/y values your shapes can use. If your shapes are drawn outside the viewBox boundaries, they get clipped. Make sure the viewBox range covers all your shape coordinates.

5. What is the difference between fill and stroke in SVG?

fill is the color inside a shape. stroke is the color of the shape's outline. A shape with fill="none" and no stroke defined will be completely invisible — this is the most common cause of the “blank SVG” bug.

6. How do I debug an SVG that isn't rendering correctly?

Open browser DevTools (F12), locate the <svg> element in the Elements panel, and inspect each child shape. Check for: missing viewBox, fill="none" without a stroke, CSS class conflicts, or malformed path data. Alternatively, paste the SVG code into the SVG Code Editor for instant visual feedback.

7. Is SVG code better than PNG for web icons?

For most use cases, yes. SVG icons are smaller in file size, scale infinitely without blur, can be styled with CSS, and support accessibility attributes like <title> and <desc>. PNG is only preferable for photographic content or complex raster imagery where vectorization would be impractical.

8. How do I convert SVG code to a React component?

Replace class with className, convert hyphenated attributes to camelCase (stroke-width becomes strokeWidth, fill-rule becomes fillRule), and wrap the markup in a function component. SVGMaker's SVG Code Editor automates this with a one-click React export that handles all the JSX conversion for you.

Conclusion: SVG Code Is Just Shapes Made of Text

SVG code isn't a black box. It's a small vocabulary — seven shape elements, a handful of path commands, and a coordinate system defined by viewBox — that combines to produce everything from a simple arrow icon to a detailed illustration.

The three skills covered in this guide are all you need: reading SVG (understanding what each tag and attribute does), writing SVG (building shapes from scratch using <rect>, <circle>, and <path>), and debugging SVG (identifying why a graphic isn't rendering correctly and how to fix it).

The fastest way to solidify these skills is to practice. Open the SVG Code Editor, paste in any SVG you have — an icon from your project, a logo from a design library, or one of the examples from this guide — and start experimenting. Change a fill color, move a cx coordinate, tweak a number in a path command. The live preview makes learning hands-on and immediate.

And when you need to create new SVGs from scratch without hand-coding every path, SVGMaker generates clean, editable SVG code from a simple text prompt — ready to refine in the code editor until it's exactly what you need.

Related Articles

Get started background

No credit card required