SVG viewBox

An svg viewbox is the coordinate system of the drawing, not the pixel size on the page. It gives the browser a rectangle to map into the space available for the image. In actual markup, the attribute is spelled viewBox, with a capital B. Keeping these two kinds of size separate makes responsive icons much easier to understand.

The four numbers

The pattern is viewBox followed by min-x, min-y, width, and height. Its four values define a rectangle in user coordinates. The first number is the minimum horizontal coordinate, and the second is the minimum vertical coordinate. The third and fourth describe the rectangle's width and height. They are extents, not the coordinates of the opposite corner.

OrderNameMeaning
1min-xLeft edge of the coordinate rectangle
2min-yTop edge of the coordinate rectangle
3widthHow wide the rectangle is, not the right-edge coordinate
4heightHow tall the rectangle is, not the bottom-edge coordinate

Source: the viewBox attribute in the W3C SVG specification. The default alignment is described under preserveAspectRatio.

For example, 0 0 24 24 starts at the origin and covers twenty-four units in both directions. A rectangle described as 10 20 24 24 has the same size but starts at a different position. Negative starting values are possible too. Changing the starting pair changes which coordinate region is mapped into the viewport; it does not rewrite the points inside each path.

These values are not CSS pixels by themselves. They are a measuring system for the artwork. A distance of twelve drawing units can occupy different screen distances depending on the viewport and scaling rules. Reading an svg viewbox as a coordinate rectangle avoids the mistaken assumption that every number must match a display dimension.

Width and height

The width and height attributes, when present, set the viewport dimensions. CSS can also set the displayed size, so the final space depends on how the SVG is placed in the page. The viewBox maps the drawing into that viewport without requiring every shape to receive new coordinates.

Imagine a square drawing whose coordinate box is 24 units across. Displaying it in a square viewport 48 CSS pixels across gives each drawing unit twice that screen distance. Displaying it at 96 pixels increases the scale again. The circle centers and path points remain unchanged in the source. Adjusting the viewport and editing the geometry are therefore separate operations.

preserveAspectRatio

A square drawing will not naturally match a wide rectangular viewport. The preserveAspectRatio attribute determines how to handle that mismatch. Its default is xMidYMid meet: the whole drawing stays visible and is centered, with empty space if the aspect ratios differ. The same scale is used horizontally and vertically, keeping circles circular.

Using slice instead of meet fills the viewport while keeping the proportions, but part of the drawing may extend beyond the viewport and be cropped. That can suit a decorative background while hiding important parts of an icon. Consider whether showing the complete symbol or filling the available rectangle matters more for the particular image.

SVG Optimize has no switch for preserveAspectRatio. If an image has unexpected empty space, inspect its coordinate box, viewport dimensions, and alignment setting together. Space can also be built into the artwork itself: a small shape inside a large coordinate rectangle will retain that surrounding room when scaled.

Why not to delete it to save bytes

Removing the viewBox can leave the image unable to scale as intended, or change how width and height apply to the visible artwork. A larger image box may no longer produce a larger drawing. Saving a short attribute is a poor trade when a symbol then breaks at another size.

In SVG Optimize, Remove viewBox is off by default for that reason. SVG Optimize is a free browser tool for people who need a smaller SVG without uploading the file. When using SVG Optimize, the file stays in the browser for optimization. Leave that removal disabled when the asset needs its coordinate mapping. Use the SVG Editor for reading the attribute in the markup and checking the rectangle against the drawing.

A short coordinate example

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="48" height="48">
  <circle cx="12" cy="12" r="8" />
</svg>

This circle sits at the center of a 24 by 24 coordinate rectangle. Its radius is eight drawing units, leaving four units between its edge and each side of the box. The viewport is 48 by 48, so the circle is drawn with a radius of sixteen CSS pixels. Changing both display dimensions to 96 doubles that radius again while the svg viewbox stays unchanged.

Frequently asked questions

Is the viewBox the same as width and height?

No. The viewBox describes a rectangle in the drawing's own coordinates. Width and height help establish the viewport in which that rectangle is displayed, and CSS can control the displayed size too. A drawing can keep its original coordinate values while appearing in several different sizes on a page.

What happens if I remove the viewBox?

The automatic mapping from the drawing's coordinate rectangle to its viewport is removed. Depending on the remaining dimensions and surrounding layout, the artwork may stop scaling as expected, occupy only part of its box, or become clipped. Compare the image at several sizes before accepting a file without this attribute.

What do the four numbers mean?

They give the minimum x coordinate, minimum y coordinate, coordinate width, and coordinate height, in that order. The first pair locates the rectangle; the second pair describes its extent. For 0 0 24 24, the rectangle starts at the origin and extends twenty-four drawing units along each axis.

What is preserveAspectRatio?

It controls alignment and scaling when the coordinate rectangle and viewport have different proportions. The default, xMidYMid meet, centers the complete drawing with room left over when necessary. A slice setting fills the viewport while preserving proportions, so some artwork may fall outside the visible area and be cropped.

Should an icon keep its viewBox?

Usually yes, especially if the same icon appears in buttons, menus, and larger layouts. Keeping the coordinate box lets those uses share the same geometry while changing the display dimensions. Verify that the box also includes intended strokes and spacing; retaining an incorrectly sized box will not fix an already cropped icon.