SVG Fundamentals and viewBox
You have used Lucide icons by copying SVG markup and dropping paths into symbols. That workflow is fast and practical, but it leaves the internals as a black box. When an icon renders at the wrong size, clips unexpectedly, or needs a small modification, you need to understand what is actually happening inside the SVG. Hand-coding a simple icon teaches you the coordinate system, the shape primitives, and the path command syntax that every SVG is built from. You will not memorize every command, but you will be able to read, debug, and modify SVG markup with confidence.
The Coordinate System and viewBox
SVG uses a coordinate system where the origin (0, 0) is the top-left corner. The x-axis increases to the right and the y-axis increases downward, which is the opposite of the math convention but consistent with how CSS lays out the page.
The viewBox attribute defines the coordinate space the SVG draws in, as four values: min-x min-y width height. The outer width and height attributes (or CSS dimensions) control how large the SVG renders on screen. The viewBox controls the internal grid the shapes are drawn on. The browser scales the internal grid to fit the rendered size automatically.
A viewBox="0 0 24 24" means the drawing canvas runs from 0 to 24 in both axes. If you render that SVG at 48px wide, each internal unit equals 2px on screen. This is why all Lucide icons share viewBox="0 0 24 24" and scale perfectly to any size.
Basic Shape Elements
SVG provides several shape primitives before you ever need the <path> element. These cover most simple icon components:
rect
<!-- x, y = top-left corner; rx = corner radius -->
<rect x="2" y="2" width="20" height="20" rx="2"></rect>
circle and ellipse
<!-- cx, cy = center; r = radius -->
<circle cx="12" cy="12" r="10"></circle>
<!-- rx, ry = horizontal and vertical radii -->
<ellipse cx="12" cy="12" rx="10" ry="6"></ellipse>
line and polyline
<!-- x1,y1 = start; x2,y2 = end -->
<line x1="2" y1="12" x2="22" y2="12" stroke-linecap="round"></line>
<!-- Multiple connected points -->
<polyline points="2,20 12,4 22,20"></polyline>
polygon
<!-- Like polyline but automatically closes -->
<polygon points="12,2 22,20 2,20"></polygon>
SVG shape attributes like fill, stroke, stroke-width, opacity, and stroke-linecap can be written directly on the element as attributes or applied via CSS. CSS takes precedence over presentation attributes, which is why setting fill: currentColor in a stylesheet overrides a fill="black" attribute on the element.
The path Element: d Mini-Primer
The <path> element can draw any shape using the d attribute, which is a string of commands and coordinates. Each command is a single letter followed by numbers. Uppercase letters use absolute coordinates; lowercase letters use coordinates relative to the current position.
You do not need to memorize every command. The ones you will encounter in icon SVGs are:
-
M x y: Move to. Lifts the pen and moves to a new position without drawing. Every path starts with M. -
L x y: Line to. Draws a straight line from the current position to the given point. -
H x: Horizontal line to. Shorthand for a horizontal L. -
V y: Vertical line to. Shorthand for a vertical L. -
C x1 y1, x2 y2, x y: Cubic bezier curve. Two control points (x1,y1andx2,y2) and an endpoint (x,y). This is what produces smooth curves in icons. -
A rx ry x-rotation large-arc sweep x y: Arc. Draws an elliptical arc. More complex but used for rounded corners and circle segments. -
Z: Close path. Draws a straight line back to the starting point and closes the shape.
<!-- A simple house icon hand-coded with path -->
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<!-- Roof: move to peak, line to right edge, line to left edge, close -->
<path d="M 12 3 L 22 12 L 2 12 Z"></path>
<!-- Walls: rect is cleaner than path for rectangles -->
<rect x="4" y="12" width="16" height="9"></rect>
<!-- Door: small rect centered at bottom -->
<rect x="9" y="16" width="6" height="5"></rect>
</svg>
Reading an existing Lucide path like M 3 9 L 12 3 L 21 9 V 20 H 3 Z is now decipherable: move to (3,9), line to (12,3), line to (21,9), vertical line to y=20, horizontal line to x=3, close. That is a house outline.
Groups and Transforms
The <g> element groups shapes so you can apply attributes, styles, or transforms to all of them at once. A transform on a <g> moves, rotates, or scales every child element together.
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<!-- Group with shared stroke style -->
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round">
<line x1="3" y1="6" x2="21" y2="6"></line>
<line x1="3" y1="12" x2="21" y2="12"></line>
<line x1="3" y1="18" x2="21" y2="18"></line>
</g>
<!-- Translate a group 10 units right and 5 units down -->
<g transform="translate(10, 5)">
<circle cx="0" cy="0" r="4"></circle>
</g>
<!-- Rotate a group 45 degrees around the center of the viewBox -->
<g transform="rotate(45, 12, 12)">
<rect x="8" y="8" width="8" height="8"></rect>
</g>
</svg>
Transforms stack when nested: a child group's transform is applied relative to the parent group's transformed coordinate space. This is the same behavior as CSS transforms on nested elements.
Live Icon Demo
The SVG below is a hand-coded camera icon built from the primitives covered on this page. Toggle each layer to see which markup produces which part of the icon.
Build Your Demo
Create a new directory at unit-6/svg-fundamentals/ in your Practice Site with index.html and svg-fundamentals.css. Hand-code at least one original icon from scratch without copying paths from Lucide or any other library. Your icon must include:
-
A
viewBoxthat defines the coordinate space, with a visible size set via CSS or HTML attributes. -
At least two different shape primitives (
rect,circle,line,polyline, orpolygon) or one shape and onepathwith at least three different commands. -
At least one
<g>group with a shared style or transform applied. -
currentColorused for stroke or fill so the icon inherits the parent's text color.
Add a link to your demo on your Practice Site homepage and in your README.md.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
What
viewBoxdoes and how it relates to the rendered width and height. Show the same SVG at two different rendered sizes with the sameviewBoxto prove the shapes scale correctly. - At least three path commands from the mini-primer, demonstrated on a real path string. Point to the letters and coordinates and narrate what the pen is doing at each step.
-
How
<g>groups share styles and transforms across children. Show what happens when you move the transform from the group to an individual child. - Your hand-coded icon: build it live from an empty SVG element, adding one shape at a time. Starting from a blank canvas and arriving at a recognizable icon in real time is the clearest way to teach this topic.
Open the SVG in a browser as you build it so teammates see the output updating alongside the code. Browser DevTools SVG inspection is also worth a quick tour: show how hovering an element in the Elements panel highlights the corresponding shape in the viewport.
Resources
- MDN: SVG Paths — Full path command reference with diagrams for each command
- MDN: viewBox — Coordinate space reference including preserveAspectRatio behavior
- MDN: g element — Grouping reference with transform and style inheritance
- SVG Path Visualizer — Paste any path d string and see each command highlighted step by step