Lesson 2 of 9 · 22m
SVG Viewports, Coordinates, and Accessible Graphics
Master viewBox scaling, preserveAspectRatio, responsive sizing, pointer coordinate conversion, and the semantic details that make SVG usable.
Most SVG bugs I review are coordinate bugs wearing a CSS costume. The graphic is cropped, off-center, or impossible to make responsive because nobody decided what coordinate system they were drawing in. SVG has two boxes: the viewport, controlled by CSS size, and the viewBox, which defines the internal user coordinate system.
The viewBox is a coordinate contract
<svg viewBox="0 0 400 300" width="100%" height="auto">
<rect width="400" height="300" fill="#111827"></rect>
</svg>The artwork is authored in a 400 by 300 coordinate space and mapped into whatever viewport CSS creates. width and height control box size; viewBox controls meaning. Choose the latter from the content: a 24 by 24 icon grid, a chart's data space, or a diagram's logical grid.
preserveAspectRatio decides the compromise
meet letterboxes the entire scene. slice fills the viewport and crops overflow. none stretches non-uniformly. Maps and diagrams usually want meet; decorative full-bleed art sometimes wants slice; charts almost never want none.
Alignment matters too: xMinYMid pins the left edge, while xMidYMax pins bottom-center. Read the value as a placement rule, not as a magic string copied from an icon.
Let CSS own responsive size
.graphic {
display: block;
width: 100%;
aspect-ratio: 4 / 3;
height: auto;
}aspect-ratio reserves space before fonts or data load. Without it, diagrams make pages jump on mobile connections. An inner <svg> creates a fresh viewport and clipping boundary; a plain <g> only transforms content.
Meaningful graphics need names
<svg role="img" aria-labelledby="t d">
<title id="t">Error rate rose sharply after 14:20</title>
<desc id="d">Error rate stayed below 1 percent until 14:20, then peaked at 8 percent.</desc>
</svg>Use aria-hidden="true" for decoration. For controls, prefer real buttons and links over pretending a rectangle is interactive. If you implement SVG interaction, you own focus order, visible focus, labels, keyboard behavior, and state.
Screen geometry is not user geometry
clientX is in viewport CSS pixels. SVG geometry is in user units:
const point = new DOMPoint(event.clientX, event.clientY)
const local = point.matrixTransform(svg.getScreenCTM().inverse())This accounts for element position, viewBox scaling, page zoom, and transforms. Hand-written algebra almost always forgets one of those.
Field notes
- Missing
viewBoxmeans user units are viewport pixels; resizing the element does not scale the artwork's coordinate system. - Text does not automatically remain readable when the viewBox scales. Test narrow widths and small user-unit font sizes.
vector-effect="non-scaling-stroke"keeps strokes stable under zoom in maps and diagrams.- Sanitize untrusted SVG. It is markup, not a harmless image format, and can carry script or event handlers.
Was this lesson helpful?