HTML SVG Graphics
Scalable Vector Graphics (SVG) defines 2D vector-based graphics in XML format. Unlike pixel raster images, SVG images scale infinitely without pixelation, integrate seamlessly into the HTML DOM, and can be styled dynamically with CSS and JavaScript.
Click Try it Yourself » to test inline SVG vector shapes and linear gradients live in the code playground.
Example: Inline SVG Vector Shapes & Gradients
13.1 SVG Container & ViewBox Coordinate Scaling
The viewBox attribute defines the internal coordinate system and aspect ratio of an SVG image. Its four space-separated values are: viewBox="min-x min-y width height".
SVG ViewBox Coordinate System vs. Viewport Scaling
Try It Yourself: ViewBox Coordinate Scaling
13.2 Basic SVG Geometric Shape Tags
SVG provides pre-defined elements for standard geometric primitives such as rectangles, circles, ellipses, lines, polylines, and polygons.
| Tag | Core Attributes | Description |
|---|---|---|
<rect> |
x, y, width, height, rx, ry |
Draws rectangles with optional rounded corners (rx, ry). |
<circle> |
cx, cy, r |
Draws a circle given center point (cx, cy) and radius r. |
<ellipse> |
cx, cy, rx, ry |
Draws an ellipse with horizontal radius rx and vertical radius ry. |
<line> |
x1, y1, x2, y2 |
Draws a straight line segment from start point to end point. |
<polygon> |
points="x1,y1 x2,y2 ..." |
Draws a closed multi-sided shape connected by straight lines. |
Try It Yourself: Geometric Primitives & Polygons
13.3 SVG Paths & Path Commands (<path d="...">)
The <path> element is the most powerful SVG element. The d (definition) attribute contains path commands to draw lines, Bezier curves, and arc segments.
Try It Yourself: Path Commands & Bezier Curves
13.4 SVG Styling, Gradients & DOM Tree
SVG elements exist directly in the HTML document object model (DOM), allowing full styling via CSS attributes (fill, stroke, stroke-dasharray, filter).
SVG XML Document Object Model (DOM) Tree & CSS Styling
Try It Yourself: SVG CSS Hover Animations & Text
13.5 SVG vs. Canvas Comparison & Live Studio
| Feature | SVG (Scalable Vector Graphics) | HTML5 Canvas 2D |
|---|---|---|
| Mode | Retained Mode (XML DOM Tree elements) | Immediate Mode (Bitmap pixel surface) |
| Scalability | Infinitely crisp at any resolution/zoom | Pixelates when scaled beyond buffer resolution |
| Event Handlers | Native event listeners on every element (onclick) |
Manual pixel coordinate collision detection |
| Best Used For | UI icons, logos, charts, illustrations, responsive diagrams | High-FPS games, pixel video filters, particle effects |
Live Interactive SVG Graphic Generator Studio
Try It Yourself: Interactive SVG Polygon & Vector Icon
💻 Chapter 13 Hands-On Code Challenge
Build a Scalable SVG Badge featuring a circle background, inner star polygon, and responsive viewBox scaling:
Key Takeaways & Summary
- Resolution Independence: SVG vector graphics scale smoothly to any screen resolution or zoom level without loss of quality.
- XML DOM Integration: SVG graphics live inside the HTML DOM tree, making individual nodes controllable via CSS and JavaScript.
- ViewBox Attribute: The
viewBoxattribute controls coordinate scaling and aspect ratio preservation. - Geometric Primitives: Use
<rect>,<circle>,<ellipse>,<line>,<polygon>, and<path>to create complex illustrations.