What is SVG
Vector basics
Learn how XML-based vector markup stays sharp at any size.

This page is a self-contained introduction to SVG (Scalable Vector Graphics)—sharp, resolution-independent graphics for the web. You will understand what SVG is, how to write basic shapes, how to style them, how SVG compares to Canvas and PNG, and where to go next in the topic index.
Vector basics
Learn how XML-based vector markup stays sharp at any size.
<svg> root
Write a minimal inline SVG with shapes, fill, and stroke.
Shapes & path
Meet rect, circle, ellipse, line, polyline, polygon, path, and text.
SVG vs others
Choose SVG, Canvas, or PNG/JPEG for the right job.
Hands-on labs
Practice five snippets with View Output and Try It Yourself.
Full roadmap
Jump from basic shapes to styling, effects, and advanced SVG.
Scalable Vector Graphics (SVG) is an XML-based markup language for describing two-dimensional vector graphics. As a web standard maintained by the W3C, SVG is supported by all major modern browsers.
Unlike raster images (JPEG, PNG, GIF), SVG graphics are defined by math—lines, curves, and fills—so they scale to any size without losing quality. That makes SVG ideal for icons, logos, charts, diagrams, and responsive interfaces.
Because SVG is part of the DOM when inlined, you can target elements with CSS, attach event listeners with JavaScript, and animate properties—something raster images cannot do as flexibly.
Modern UIs rely on crisp icons, logos, and diagrams on every screen size. SVG gives you resolution independence, small files for flat graphics, CSS theming, and scriptable interactivity in one format.
Scale up or down without pixelation—perfect for responsive and retina layouts.
Style with CSS: fills, strokes, filters, transitions, and currentColor.
Elements support clicks, hovers, and scripting when SVG is inlined in HTML.
Add <title> and <desc> so meaningful graphics work with screen readers.
In short: SVG is text-based vector markup. Embed it inline or as a file, use viewBox for responsive scaling, and paint shapes with fill and stroke.
An SVG document is XML. Here is a minimal inline example in HTML—a red circle with a dark stroke:
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg> | Piece | Role |
|---|---|
<svg> | Root element; sets canvas size and the SVG namespace |
cx, cy | Center coordinates of the circle |
r | Radius |
stroke / stroke-width | Border color and thickness |
fill | Interior color |
Here are the most commonly used SVG shape elements. Each links to its dedicated tutorial:
| Element | Description | Tutorial |
|---|---|---|
<rect> | Rectangle (optionally rounded with rx/ry) | Rectangle |
<circle> | Circle defined by center and radius | Circle |
<ellipse> | Oval with two radii | Ellipse |
<line> | Straight line between two points | Line |
<polyline> | Series of connected straight segments (open path) | Polyline |
<polygon> | Closed shape with multiple sides | Polygon |
<path> | Complex shapes via path commands (most flexible) | Path |
<text> | Accessible text labels inside the graphic | Text |
Start with SVG Rectangle—the first basic shape in the sidebar—then explore the rest.
| Task | Example |
|---|---|
| Root canvas | <svg width="200" height="200" viewBox="0 0 200 200"> |
| Red circle | <circle cx="50" cy="50" r="40" fill="red" /> |
| Blue rectangle | <rect x="10" y="10" width="80" height="40" fill="blue" /> |
| Line | <line x1="0" y1="0" x2="100" y2="100" stroke="black" /> |
| Group shapes | <g transform="translate(10,10)">...</g> |
| CSS theme | fill="currentColor" or CSS svg .icon { fill: ... } |
| Responsive icon | viewBox="0 0 24 24" + CSS width/height |
All can display graphics, but they solve different problems.
vector markupBest for icons, logos, diagrams, and UI illustrations that must stay sharp and styleable.
bitmap surfaceBest for games, dense charts, and pixel effects drawn with JavaScript. Scales cleanly only if redrawn.
raster imageBest for photos, textures, and complex imagery on a fixed pixel grid.
Learn more in the Canvas Introduction when you need JavaScript pixel drawing.
Reach for SVG whenever your graphic must scale, theme, or stay editable as markup.
Flat brand marks and UI icons stay crisp on every density and theme.
Lines, nodes, and labels remain selectable and styleable in the DOM.
Hero accents, empty states, and decorative shapes that need CSS control.
One viewBox drawing scales cleanly with CSS width and height.
Detailed photographs and noisy textures belong in WebP, JPEG, or PNG.
Key benefit: one text-based asset can scale, theme, animate, and remain searchable—without exporting a dozen PNG sizes.
Browse every SVG tutorial on CodeToFun, grouped by learning path. Start with Rectangle.
Five starter snippets. Use View Output to preview here, or open Try It Yourself to edit and run live (?tryit=1 through 5).
Start with a filled circle and a rounded rectangle, then try polygons and CSS.
A classic filled circle with a dark outline using presentation attributes.
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<circle cx="60" cy="60" r="45"
fill="#ef4444" stroke="#1e293b" stroke-width="4" />
</svg> The browser places the circle center at (60, 60) with radius 45. fill paints the interior; stroke and stroke-width draw the border.
Use rx to soften corners into a card-like shape.
<svg width="140" height="80" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="120" height="60" rx="12"
fill="#3b82f6" />
</svg> x and y set the top-left corner; width and height set size. rx="12" rounds the corners. Continue in the Rectangle tutorial.
A closed multi-point star built from a list of points.
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<polygon points="50,5 61,38 95,38 68,59 79,91 50,71 21,91 32,59 5,38 39,38"
fill="#fbbf24" stroke="#b45309" stroke-width="2" />
</svg> <polygon> connects the listed points and closes the shape automatically. Learn more in the Polygon tutorial.
Inline SVG can use CSS classes and transitions for interactive styling.
<style>
.btn-icon { fill: #64748b; transition: fill 0.2s; cursor: pointer; }
.btn-icon:hover { fill: #2563eb; }
</style>
<svg width="48" height="48" xmlns="http://www.w3.org/2000/svg">
<circle class="btn-icon" cx="24" cy="24" r="20" />
</svg> The circle uses the CSS class .btn-icon for fill instead of a presentation attribute. External stylesheets can target inline SVG the same way. Hover in the preview or Try It editor to see the transition.
Define geometry once with viewBox, then size the SVG with CSS or attributes.
<svg viewBox="0 0 24 24" width="48" height="48"
xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path fill="currentColor"
d="M12 2l3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.56 5.82 22
7 14.14l-5-4.87 6.91-1.01L12 2z" />
</svg> viewBox="0 0 24 24" maps the path coordinates to a flexible viewport. Change width/height (or CSS size); proportions stay correct. currentColor lets the icon inherit text color.
Real-world places where SVG shows up every day on the web.
Navigation, buttons, and status indicators that must theme with CSS.
Example: a hamburger menu or checkmark icon.
Wordmarks and symbols that stay sharp on retina screens.
Example: a site header logo that scales from mobile to desktop.
Bars, lines, and pie slices as selectable DOM elements.
Example: a sparklines dashboard widget.
Flowcharts, architecture maps, and annotated sketches.
Example: a deploy pipeline illustration.
Friendly illustrations when a list or inbox has no data yet.
Example: a “no results” graphic in search UI.
Tiny marks that remain readable when scaled down.
Example: an SVG favicon that replaces multiple PNG sizes.
Pro Tip: if the graphic is mostly flat shapes and text, SVG usually beats a raster export for size, quality, and theming.
Why SVG is an excellent choice for modern web graphics.
Vector graphics look sharp on retina displays and when zoomed.
Customize with CSS and JavaScript for themes and interactions.
Often smaller than bitmaps for icons and simple illustrations; caching works like any static asset.
Text inside SVG can be indexed; use meaningful labels where appropriate.
Add <title> and <desc>, or mark decorative icons with aria-hidden.
Pro Tip: version-control SVG like code—because source is text, icon changes show up cleanly in Git diffs.
Follow these practices for clean, scalable SVG on the web.
viewBox for responsive iconsDefine geometry once, then size with CSS width/height instead of fixed pixels alone.
Classes, <style> blocks, or currentColor keep icons adaptable to dark mode and brand palettes.
Inline for CSS/JS control; <img> for simple cacheable icons; CSS background for decorative chrome.
Use <title>/<desc> for meaningful art; set aria-hidden="true" on pure decoration.
Simplify paths and strip unused metadata (tools like SVGOMG) so files stay small.
Pro Tip: know basic HTML first—SVG can live inline in an HTML file or as a separate .svg linked with <img>.
Avoid these mistakes when SVG does not look or behave as expected.
viewBoxHard-coded width/height alone can break responsive layouts.
→ Add a viewBox and size the SVG with CSS.
<img> SVGExternal SVG loaded via <img> does not expose internals to page CSS or JS.
→ Inline the SVG (or use CSS filters on the image) when you need per-shape styling.
Design tools often leave metadata, unused groups, and noisy paths.
→ Optimize with SVGOMG (or similar) and enable gzip/brotli on the server.
Complex photos become enormous as vectors or look wrong as traces.
→ Use WebP or JPEG for photos; keep SVG for flat graphics.
Untrusted markup can embed scripts and create XSS risk.
→ Sanitize or serve untrusted SVG as <img>, never raw inline from unknown sources.
Pro Tip: if a shape vanishes, check fill/stroke visibility, coordinates inside the viewBox, and whether zero width/height/radius was set.
| Method | How | Best when |
|---|---|---|
| Inline SVG | Paste <svg>...</svg> in HTML | You need CSS/JS control of individual shapes |
<img> | <img src="icon.svg" alt=""> | Simple, cacheable icons without script access |
| CSS background | background-image: url(icon.svg) | Decorative chrome that should not affect layout semantics |
<object> / <embed> | Legacy embedding options | Rare today; prefer inline or <img> |
For responsive icons, add viewBox="0 0 100 100" and size with CSS width/height.
The browser reads SVG XML (inline or loaded) and builds shape nodes in the DOM.
CSS and presentation attributes set fill, stroke, opacity, and transforms.
The engine converts paths to pixels at the current zoom level and device pixel ratio.
Shapes appear on screen; resize the page and vectors stay sharp.
fill paints the inside; stroke paints the border. CSS can override presentation attributes on inline SVG.viewBox for responsive icons and illustrations.<animate>, but for new projects prefer CSS transitions/keyframes or JavaScript—they have broader tooling support.Quick Takeaway: write shapes in an <svg>, paint with fill/stroke, scale with viewBox, and pick the embed method that matches how much control you need.
SVG is a W3C web standard supported in all modern browsers — and has been for many years — for inline graphics and external .svg files.
Use SVG inline or as .svg assets. Basic shapes, viewBox, fill, stroke, and CSS styling work consistently across Chrome, Firefox, Safari, Edge, and mobile browsers.
Bottom line: Safe for production sites. Prefer inline SVG or cached .svg assets; no polyfill is required for current browsers.
SVG is a versatile, powerful format for creating web graphics. Its scalability, performance benefits, and ease of styling make it a great choice for modern web design.
By understanding the basics of SVG and how to use it, you can create high-quality, responsive graphics for your website—from favicons and icons to charts and hero illustrations.
Practice the five examples above, then continue to SVG Rectangle—the first basic shape in the sidebar.
Use shape elements, path, CSS, and viewBox for responsive icons. Compare with Canvas when you need pixel-drawn graphics.
viewBox and size SVGs with CSS for responsiveness<title> and <desc> for meaningful graphicscurrentColor so icons inherit text color from CSSaria-hidden="true" on decorative iconsBuild crisp, scalable graphics the SVG way.
XML shapes that stay sharp
BasicsRoot canvas for shapes
StructurePaint interiors and outlines
StyleResponsive scaling
LayoutFirst basic shape tutorial
PathEmbed inline with <svg> in HTML or link an .svg file via <img>. Use viewBox for responsive scaling. Style shapes with fill, stroke, and CSS classes. SVG became a W3C recommendation in 2001—making it older than many JavaScript frameworks, yet still essential for modern UI icons. Because SVG source is text, you can version-control icon changes in Git like any code file.
Learn the first basic shape in the sidebar—position, size, and rounded corners with <rect>.
9 people found this page helpful