SVG Coordinate Systems and Units – SVG Viewport and Viewbox

All SVG content is drawn inside SVG viewports. Every SVG viewport defines a drawing region characterized by size (width, height), and an origin, measured in abstract user units. ( W3C)

The canvas is the infinite space where SVG elements are placed. The visible portion of that canvas, limited by the screen or container, is called the viewport. Understanding the relationship between canvas, viewport, and viewBox is essential for precise SVG layout and responsive scaling.

Key Takeaways

  • The viewport defines the visible window (width × height) on the infinite SVG canvas.
  • The viewBox maps a user‑defined coordinate rectangle onto that viewport, enabling zoom, pan, and aspect‑ratio control.
  • Changing viewBox values directly influences scaling factors and cropping behavior.

Canvas vs Viewport

The canvas is an abstract, limitless plane that can contain any coordinates, both positive and negative. All SVG content must be displayed inside the viewport that defines an area on the canvas, characterized by size and a point of origin. The default origin is (0,0). Scope of the viewport sets using the width and height attributes on the outermost <svg> element.

1    <svg width="900" height="500" xmlns="http://www.w3.org/2000/svg">
2    </svg>

The above code shows how to set the width and height of the SVG viewport. The picture can be seen through the 900×500 pixels “window”. Such a viewport is named the initial viewport.

The canvas and initial viewport

Initial vs User Coordinate System

Let’s examine how SVG represents the positions and sizes of objects for drawing.

The default coordinate system in SVG is much the same as in HTML. It works like a two-dimensional x-y plane. The initial SVG coordinate system sets in the initial SVG viewport with the point of origin (0,0) in the top left corner. The positive x-direction being to the right, and the positive y-direction being to the bottom. For specifying (x, y) coordinates, width and height values, you can use cm, mm, in, em, ex, pt, pc, and px. The default SVG units are pixels.

We have to differentiate the initial SVG coordinate system and the user SVG coordinate system of the viewport. The main outermost <svg> element has a viewBox attribute. If this attribute is not given, then the user coordinate system is the same as the initial coordinate system.

The viewBox attribute sets a user coordinate system, which may not be the same as the initial one. The viewBox takes four parameters:

Attribute values must be placed inside quotes: viewBox= "min-x min-y width height".

The viewport is like a window you look through to see an SVG’s content. The viewBox is also similar to the viewport, and it can be used for zooming. Consider an example:

1    <svg width="900" height="500" viewBox="220 125 450 250" xmlns="http://www.w3.org/2000/svg">
2    </svg>

According to a code snippet, viewBox defines a specific canvas area, covering a rectangular with the origin point (220,125), the width=450 and the height=250. Then the SVG image is cropped to that area and scaled up to fill the entire viewport.

Initial viewport and user viewbox

The figure on the left shows the infinite SVG document canvas, initial SVG viewport and user viewBox. The figure on the right illustrates the result of the viewBox attribute applying.

As a result of the viewBox attribute using, the user coordinate system unit became equal to two units of the initial viewport. The scaling took place. Changing the parameters of the viewBox attribute allows you to scale, fragment, and move the original image along the coordinate axes. In addition, the SVG coordinate system can be rotated, skewed, and flipped.

Common Mistakes and Fixes

ProblemCauseFix
SVG content is clippedThe viewBox defines a smaller area than the drawn contentIncrease viewBox width and height or adjust min-x / min-y values
SVG appears stretchedviewBox aspect ratio does not match the viewportKeep proportional viewBox dimensions or control scaling with preserveAspectRatio
Objects appear too large or too smallUser coordinate system scale is misunderstoodRecalculate viewBox units relative to the viewport size
Content is shifted unexpectedlyIncorrect min-x or min-y values in viewBoxAdjust the origin values to align the desired content area
Nothing changes after modifying viewBoxThe SVG has no visible content inside the defined areaVerify that drawn elements fall within the viewBox bounds

Quick Recipes

TaskSVG Example
Define a fixed viewport<svg width="400" height="200"></svg>
Make SVG scalable<svg width="400" height="200" viewBox="0 0 400 200"></svg>
Zoom into a specific area<svg width="400" height="200" viewBox="100 50 200 100"></svg>

Next Steps or Related Resources

  • If you’re wondering how SVG viewBox works, its syntax, and practical SVG scaling and positioning examples, please visit the SVG viewBox article.

  • To learn more and get SVG code examples for scaling, moving, rotating, and skewing SVG graphics using the SVG transform attribute, please go to the SVG Transformation article or the SVG 2.0 W3C page.

  • Read the SVG Transformations – C# Examples article to get C# code examples for rotating, scaling, translating, and skewing SVG graphics using Aspose.SVG for .NET library.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.