Home HTML SVG HTML SVG Definition
Definition and Usage SVG (Scalable Vector Graphics) is a format for drawing images with shapes and paths instead of a grid of pixels. On an HTML page, SVG stays sharp when you zoom or resize—ideal for icons, logos, and diagrams.
What does this mean for HTML authors? You do not need to master every SVG feature to use it. This tutorial focuses on how to put vector graphics into a webpage : with <img> , by pasting <svg> markup inline, with <iframe> , or as a CSS background.
Vector vs raster on the web Raster files (PNG, JPEG, GIF) store pixels—great for photos, but they look blocky when enlarged. Vector SVG stores instructions the browser redraws at any size, so edges stay clean.
When should you use SVG? Prefer SVG for UI icons, logos, charts, and simple illustrations. Prefer raster for photographs. Prefer canvas for pixel games and heavy JS drawing.
Sharp at any size. Scale an SVG logo in CSS or with width/height—it should stay crisp on retina screens.
Not a full SVG course. This page teaches embedding SVG in HTML. For the <svg> element itself (viewBox, accessibility), see HTML svg Tag . For shape deep-dives, continue to SVG tutorials .
Editors help. Hand-code simple circles and rects; export complex art from Inkscape, Illustrator, or Figma as .svg.
Syntax
HTML Syntax — Ways to Include SVG Four common patterns on an HTML page:
<img src="logo.svg" alt="Company logo" width="120" height="40"> <svg width="200" height="200" role="img" aria-label="Blue circle">
<circle cx="100" cy="100" r="60" fill="#3b82f6"/>
</svg> <iframe src="diagram.svg" title="Process diagram" width="400" height="300"></iframe> .hero {
background-image: url("pattern.svg");
background-size: cover;
} Reference
Core HTML Concepts Choose an embed method by what the page needs:
Method Best for CSS / JS from the page <img src="*.svg">Icons, logos, cached assets Limited (treat as an image) Inline <svg>…</svg> Hover styles, DOM, a11y inside graphic Full (page CSS & script) <iframe src="*.svg">Standalone SVG documents Limited / same-origin only CSS background-image Decorative patterns Background only (not content)
img vs inline vs canvas img + SVG file Inline SVG Canvas Scales sharply Yes Yes No (bitmap) Browser cache Yes No (in HTML) N/A Style with page CSS Hard Easy Draw via JS Typical use Static icons Interactive UI art Games / pixels
Common SVG shapes (quick map) Element Draws <circle>Circle (cx, cy, r) <rect>Rectangle <path>Custom curves and shapes <line> / <polygon>Lines and multi-point shapes <text>Text that stays selectable / accessible
Approach
How to Add SVG to an HTML Page A practical workflow:
Decide if the graphic is decorative, a logo, or needs interaction / hover styles. For a reusable file → save icon.svg and use <img> with meaningful alt. For CSS hover or scripting → paste the <svg>…</svg> markup into the HTML. Set width / height (or CSS size) so layout does not jump. For meaningful graphics, add accessible text (alt on img, or title / aria-label on inline SVG). Goal HTML pattern Show a logo file <img src="logo.svg" alt="…">Hover-color icon Inline <svg> + CSS :hover Decorative pattern CSS background-image: url(…svg) Fallback for old browsers <img src="icon.png" srcset="icon.svg">
Try it
Live Preview Inline SVG scales cleanly. Drag the slider—shapes redraw sharply at every size.
Code
HTML Examples Six hands-on patterns: basic circle, multiple shapes, attributes, CSS styling, animation, and a full page.
1. Circle 2. Shapes 3. Attributes 4. CSS 5. Animate 6. Full page Example 1 — Basic Circle (Inline SVG) <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="60"
fill="#3b82f6" stroke="#1e3a8a" stroke-width="4"/>
</svg> How It Works svg creates a drawing area in the HTML page.circle uses center (cx, cy) and radius r.fill / stroke color the shape without CSS.Inline means the graphic is part of the document DOM.Example 2 — Rectangle and Circle Together <svg width="220" height="120" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="100" height="100" fill="lightblue" stroke="black" stroke-width="2"/>
<circle cx="170" cy="60" r="40" fill="tomato" stroke="black" stroke-width="2"/>
</svg> How It Works One svg can hold many shapes.Paint order follows markup—later shapes sit on top.rect and circle share fill/stroke attributes.Diagrams are built by stacking simple elements.Example 3 — Inline Attribute Styling <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40"
fill="blue" stroke="black" stroke-width="3"/>
</svg> fill="blue"
stroke="black"
stroke-width="3" How It Works Presentation attributes style a one-off shape quickly.No stylesheet required for basic colors.Reusable look → prefer CSS classes (next example).img embeds cannot be restyled this way from the page.Example 4 — CSS Class Styling <style>
.my-circle { fill: green; stroke: red; stroke-width: 2; }
.my-circle:hover { fill: lightgreen; }
</style>
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" class="my-circle"/>
</svg> Hover in Try It to see the fill change.
How It Works Inline SVG lives in the HTML DOM like other elements.Page CSS can target classes and :hover.This is a key reason to choose inline over img for interactive icons.External SVG via img usually cannot use these page styles.Example 5 — Simple Animation <svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="25" fill="blue">
<animate attributeName="cx" from="50" to="150"
dur="2s" repeatCount="indefinite"/>
</circle>
</svg> attributeName="cx" — moves left to right
dur="2s" — one cycle takes 2 seconds
repeatCount="indefinite" — loops forever How It Works animate changes an attribute over time.cx slides the circle horizontally.Inline SVG is required for this markup to live in the page.Modern alternative : CSS @keyframes on inline SVG.Example 6 — Complete HTML Page <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG Example</title>
<style>
.rect-style { fill: lightblue; stroke: black; stroke-width: 2; }
.circle-style { fill: tomato; stroke: black; stroke-width: 2; }
</style>
</head>
<body>
<h1>SVG Example</h1>
<svg width="220" height="200" xmlns="http://www.w3.org/2000/svg">
<title>Sample shapes</title>
<desc>A rectangle, circle, and green curve.</desc>
<rect x="10" y="10" width="100" height="100" class="rect-style"/>
<circle cx="170" cy="60" r="40" class="circle-style"/>
<path d="M10 170 Q 60 140, 110 170 T 210 170"
fill="none" stroke="green" stroke-width="4"/>
</svg>
</body>
</html> How It Works Full HTML document hosts inline SVG like any other content.CSS classes style shapes from the page stylesheet.title / desc help describe meaningful graphics.path draws a curve—hand-code simple paths or export from an editor. Edge Cases & Pitfalls Most SVG-in-HTML issues are the wrong embed method or missing accessibility text.
method Expecting page CSS to style an img SVG External files in img are largely opaque to your stylesheet. Inline the SVG if you need hover colors.
size Huge inline SVG everywhere Duplicating large markup bloats HTML and skips caching. Reuse a file with img when you do not need DOM access.
a11y Meaningful graphic with no alt / title Logos and informative diagrams need text alternatives. Decorative SVGs can use aria-hidden="true".
photo Tracing a photo into SVG Complex photos often make heavy SVG files. Prefer JPEG/PNG for photographs.
iframe Using iframe as the default Usually worse than img or inline for icons. Prefer the simpler methods first.
server Wrong MIME type for .svg files Servers should serve SVG as image/svg+xml. Misconfiguration can prevent images from showing.
Compatibility
Browser Support SVG in HTML via img and inline svg is Baseline and supported in all modern browsers. Prefer img or inline over iframe for most UI graphics.
✓ Baseline · Widely available
SVG on HTML pages Vector graphics embed cleanly with img, inline svg, and CSS backgrounds across current browsers.
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support HTML SVG Universal
Bottom line: Pick img for cached static icons, inline svg when you need CSS or DOM—and keep photos as raster.
Remember
Key Takeaways SVG is vector art that stays sharp in HTML pages.
img for simple display; inline svg for CSS and scripting.
Use raster for photos; SVG for icons, logos, and diagrams.
Tag details live on
/html/tags/svg ; shape lessons continue under
/svg .
One line: Put SVG on the page with img when you only need to show it, or inline <svg> when you need to style or script it.
Frequently Asked Questions What is SVG in an HTML page? SVG (Scalable Vector Graphics) describes images with shapes and paths instead of pixels. On HTML pages you can show SVG with img, paste the markup inline, use an iframe, or set a CSS background.
How is this page different from the svg tag page? This tutorial teaches how to include vector graphics in HTML (img vs inline vs iframe vs CSS). The /html/tags/svg page is the element reference—viewBox, attributes, accessibility details for the svg tag itself.
Should I use img or inline svg? Use img (or object) for icons and logos you only need to display and cache. Use inline svg when you need CSS hover styles, DOM access, or title/desc inside the graphic.
SVG vs PNG/JPEG? SVG stays sharp at any size and is great for icons, logos, and diagrams. Raster formats (PNG, JPEG) are better for photos and complex pictures with many colors.
Can I style SVG with CSS? Yes for inline SVG in the HTML document—classes, hover, and animations work. External SVG loaded through img has limited CSS control from the page stylesheet.
Do I need a graphics editor? Simple shapes can be hand-coded. Complex illustrations are usually made in Inkscape, Illustrator, or Figma, then exported as .svg and embedded in HTML.
Practice in the editor Open a Try It demo and edit inline SVG shapes on a full HTML page.
Open Try It editor → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion