SVG for Web Developers: viewBox, Paths and Inline SVG
Contents
Short answer: SVG (Scalable Vector Graphics) is an image format written as text. Instead of storing pixels, an SVG file describes shapes: “a circle here, a line from this point to that one”. The browser draws them at any size, so SVGs stay sharp on every screen, and a simple icon is often smaller than a few hundred bytes.
Because it’s code, you can write SVG by hand, put it straight into your HTML, and style or animate it with CSS. That makes it the right format for icons, logos, illustrations and charts. For photos, stick with JPEG, WebP or AVIF.
Anatomy of an SVG
Here’s a complete SVG: a check mark. Open a text editor, paste it, save it as check.svg, and any browser will display it.
A complete SVG file
The xmlns attribute is required in standalone .svg files. Inline in HTML, you can leave it out.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"
fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round">
<path d="M4 12 L9 17 L20 6"/>
</svg>
viewBox: the coordinate system
viewBox="0 0 24 24" sets up the drawing area: it starts at x 0, y 0 and is 24 units wide and 24 tall. Every coordinate inside the SVG uses those units. The y-axis points down, so 0 0 is the top-left corner.
width and height set the size on the page. The viewBox is scaled to fit, so the same 24-unit drawing can be shown at 16 or 400 pixels. If you leave out width and height on an inline SVG, it stretches to the width of its container, and the viewBox keeps the proportions.
When the viewBox and the box on the page have different proportions, the drawing is centered and fitted inside by default. preserveAspectRatio="none" stretches it to fill the box instead, which is how full-width shapes like the CSS wave divider work.
Basic shapes
For simple geometry, SVG has dedicated elements. fill colors the inside and stroke the outline.
<svg viewBox="0 0 200 100">
<rect x="10" y="10" width="50" height="35" rx="6" fill="#2563eb"/>
<circle cx="100" cy="28" r="18" fill="#9333ea"/>
<ellipse cx="165" cy="28" rx="25" ry="15" fill="#0891b2"/>
<line x1="10" y1="70" x2="60" y2="90" stroke="#0f172a" stroke-width="4"/>
<polyline points="80,90 95,65 110,85 125,60" fill="none" stroke="#0f172a" stroke-width="4"/>
<polygon points="165,58 190,92 140,92" fill="#16a34a"/>
</svg>
SVG paths
The <path> element can draw any shape, and it’s what design tools export. Its d attribute is a list of commands, each a letter followed by numbers:
M x y: move the pen to a point without drawing.L x y: draw a straight line to a point.H xandV y: horizontal and vertical lines.QandC: curves with one or two control points.A: an arc, a piece of a circle or ellipse.Z: close the shape with a line back to the start.
Uppercase letters use absolute coordinates in the viewBox. Lowercase letters are relative to the current point: l 5 5 means “5 right and 5 down from here”. Commas and spaces are interchangeable, and a repeated command letter can be left out, which is why exported paths look like a wall of numbers.
Reading a path: a house icon
Start at the left of the roof, draw up to the peak and down to the right, then down the right wall, along the floor, and close the shape back up the left wall.
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<!-- M3 11: start at (3, 11)
L12 3: line to the roof peak
L21 11: line down to the right eave
V21: straight down to y 21
H3: straight left to x 3
Z: close back to (3, 11) -->
<path d="M3 11 L12 3 L21 11 V21 H3 Z"/>
</svg>
Curves and arcs
Q takes one control point and an end point: the curve bends toward the control point without touching it. T continues with a mirrored control point, which gives a smooth wave. C takes two control points for S-curves and more control.
The arc command reads A rx ry rotation large-arc sweep x y: the radii, a rotation, two flags (0 or 1) that pick which of the possible arcs to draw, and the end point. Below, A 30 30 0 0 1 290 60 draws a half circle with a radius of 30 from (230, 60) to (290, 60).
<svg viewBox="0 0 300 100" fill="none" stroke="#2563eb" stroke-width="3">
<path d="M10 50 Q 35 10 60 50 T 110 50"/>
<path d="M130 70 C 140 10, 200 10, 210 70"/>
<path d="M230 60 A 30 30 0 0 1 290 60"/>
</svg>
For organic shapes, nobody writes the numbers by hand. Draw them in Figma, Inkscape or Illustrator and export, or use a generator. The CSS blob shape snippet is a good example of a shape defined by a path.
How to put SVG on a page
- Inline
<svg>in the HTML. No extra request, and you can style and animate every part with CSS. Best for icons and anything interactive. <img src="logo.svg" alt="...">. Cached like any image and keeps your HTML short. CSS on the page can’t reach inside it, and scripts in the file don’t run. Best for logos and illustrations.- CSS
background-image: url("pattern.svg"). For decorative patterns and backgrounds. - A sprite with
<use>. Define each icon once as a<symbol>, then reference it anywhere with<use href="#icon-check"/>. Good when the same icons repeat many times on a page.
Styling SVG with CSS and currentColor
Inline SVG elements accept CSS like any HTML element, with SVG-specific properties: fill, stroke, stroke-width. Set them to currentColor and the icon takes the text color of its parent, so it follows hover states and dark mode with no extra CSS.
<a class="icon-link" href="/">
<svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2">
<path d="M3 11 L12 3 L21 11 V21 H3 Z"/>
</svg>
Home
</a>
<style>
.icon-link { color: #1d4ed8; }
.icon-link:hover { color: #9333ea; } /* the icon follows */
</style>
Animate a line drawing
Set pathLength="1" on the path, so its length counts as 1 whatever its real size. A dash of length 1 then covers the whole line, and moving the dash offset from 1 to 0 makes it look like the line is being drawn. That’s what happens when you hover the icons in the demo, and the hand-drawn banner on my home page draws itself with the same dash trick.
Respect users who prefer reduced motion, and pick an easing that slows down at the end, like ease-out. More on that in ease-in vs ease-out. To move, rotate or scale whole shapes, use a CSS transform; on SVG elements, add transform-box: fill-box so transform-origin works relative to the shape itself.
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path class="draw" pathLength="1" d="M4 12 L9 17 L20 6"/>
</svg>
<style>
.draw {
stroke-dasharray: 1;
stroke-dashoffset: 1;
animation: draw 1s ease-out forwards;
}
@keyframes draw {
to { stroke-dashoffset: 0; }
}
@media (prefers-reduced-motion: reduce) {
.draw { animation: none; stroke-dashoffset: 0; }
}
</style>
Accessibility
A decorative icon next to text should be hidden from screen readers with aria-hidden="true", so “Home” isn’t announced twice. An SVG that carries meaning on its own, like a logo or an icon-only button, needs a text alternative: role="img" with an aria-label on the SVG, or an aria-label on the button around it. For <img>, use alt as with any image.
<!-- Decorative: next to visible text -->
<svg aria-hidden="true" viewBox="0 0 24 24">...</svg> Home
<!-- Meaningful on its own -->
<svg role="img" aria-label="Acme logo" viewBox="0 0 24 24">...</svg>
<!-- Icon-only button: label the button -->
<button aria-label="Close menu">
<svg aria-hidden="true" viewBox="0 0 24 24">...</svg>
</button>
Optimize and secure your SVGs
- Clean up exports. Design tools add editor metadata, hidden layers and overly precise numbers. SVGO (or its web version, SVGOMG) usually cuts the file size by half or more.
- Set a size. Give inline SVGs and
<img>tagswidthandheight, so the page doesn’t jump while it loads. More in these web performance tips. - Never inline SVGs from users. An SVG file can contain JavaScript. Inline on your page, it runs with full access to your site. If users can upload SVGs, show them only with
<img>, where scripts don’t run, or sanitize them on the server.
FAQ
What is an SVG file?
A text file in XML format that describes an image as shapes, paths and text instead of pixels. You can open it in a browser to view it, or in a text editor to see and change the code.
SVG vs PNG: which should I use?
SVG for anything made of shapes: icons, logos, illustrations and charts. They stay sharp at every size and are usually smaller. PNG (or better, WebP and AVIF) for photos and images with complex detail, where a vector version would be huge.
How do I change the color of an SVG?
Inline, set fill or stroke in CSS, or use currentColor and change the parent’s color. An SVG loaded with <img> can’t be recolored from the page’s CSS; edit the colors in the file instead.
Why is my SVG not showing?
The usual causes: a standalone file without xmlns="http://www.w3.org/2000/svg", an inline SVG inside a container with no width, a missing viewBox, or fill="none" on a shape with no stroke. Also check that the server sends the file as image/svg+xml.
Can I edit an SVG in code?
Yes. Change colors, sizes and coordinates directly in the file. For bigger changes to complex paths, a vector editor like Figma or Inkscape is faster.