SVG Viewport and Coordinate Systems Explained

Scalable Vector Graphics (SVG) rely on a dual-coordinate architecture consisting of the viewport and the viewBox to render scalable, resolution-independent graphics. This article explains how the SVG viewport establishes the visible display area, how the user coordinate system defines internal geometry, and how units and transformations determine the rendering and scaling of vector elements.

The Viewport: The Outer Canvas

The SVG viewport is the visible rectangular window through which an SVG image is viewed. It is defined on the root <svg> element using the width and height attributes, or via CSS.

<svg width="500" height="300">
  <!-- Content goes here -->
</svg>

By default, the initial viewport coordinate system sets: * Origin (0,0): The top-left corner of the viewport. * Positive X-axis: Extends horizontally to the right. * Positive Y-axis: Extends vertically downward.

If no unit is specified for the viewport’s width and height, values default to standard screen pixels (px).

The viewBox: The User Coordinate System

While the viewport defines the physical dimensions on the screen, the viewBox attribute defines the internal “user coordinate system” (or virtual canvas) where elements are positioned and drawn.

The viewBox syntax accepts four comma- or space-separated values:

<svg width="500" height="300" viewBox="0 0 1000 600">
  <circle cx="500" cy="300" r="100" fill="blue" />
</svg>

In this example, the internal graphics are mapped to a 1000×600 grid, which is automatically scaled down by half to fit into the 500×300 pixel viewport.

Mapping Coordinates with preserveAspectRatio

When the aspect ratio of the viewBox does not match the aspect ratio of the viewport, the preserveAspectRatio attribute dictates how the user coordinates scale:

Alignment values (such as xMidYMid, xMinYMin, xMaxYMax) specify how the coordinate space aligns inside the viewport when empty space exists.

Units inside SVG

SVG coordinates can be specified with or without explicit units:

Nested Coordinate Systems and Transformations

Every SVG element can establish its own local coordinate system. Applying transformation attributes (translate, scale, rotate, skewX, skewY) modifies the coordinate grid for the target element and all of its children.

Nested <svg> elements or <g> groups with transforms shift the origin (0,0) and orientation, allowing complex components to be authored in isolated, self-contained coordinate systems before being composed into the root viewport.