How the SVG viewBox Maps a Drawing to Its Frame
A rectangle 1,500 user units wide and 1,000 high holds a red triangle in an example from the World Wide Web Consortium. User units are the marks on the drawing’s own ruler. The SVG viewBox names the rectangle on that ruler; the SVG viewport is the display frame into which the browser maps it. Narrowing the frame in the consortium’s example squeezes the triangle sideways, because the example gives the browser permission to scale its width and height independently.

What do the four viewBox numbers measure?
In the consortium’s chapter on SVG coordinates, the line above the triangle and its yellow background reads viewBox="0 0 1500 1000". The first pair locates the rectangle’s top-left corner, called its origin: min-x measures across, and min-y measures down. The last pair gives the width to the right of that corner and the height below it. A rectangle starting farther along the drawing ruler would put its right edge at its starting x value plus its width. The same addition gives the bottom edge. The words width and height are doing quite ordinary work here.
- Locate the corner: min-x runs across the drawing and min-y runs down it.
- Measure the span: Width extends to the right from that corner and height extends downward.
- Set the scale: The browser compares each display dimension with the corresponding drawing span and applies the fit setting.
- Place the result: The browser shifts the scaled rectangle into position, including any offset called for by the alignment setting.
There is a second width and height in the markup, on the svg element itself. In this example, those dimensions establish the viewport in pixels. The yellow rectangle has the same origin, width, and height as the viewBox. Its blue outline is a separate stroke. The browser also scales any margin inside the drawing rectangle, so a symbol drawn well inside its edges will still have room around it after the mapping.
The starting corner matters as soon as either minimum moves away from the drawing’s zero point. The chapter’s calculation first works out how much to scale the artwork, then uses the scaled position of the corner to work out how far to shift it into place. Moving the specified corner to the right along the drawing ruler brings a part of the picture farther to the right to the left side of the display frame, before any extra alignment offset. Changing the span changes magnification; changing the corner moves the region being framed.
The same ruler explains a detail in the chapter’s example on units. A rectangle sized in percentages sits above an equal rectangle sized in drawing units. The percentage width is calculated from the width in viewBox, and the percentage height from its height. A change to those spans can change the size of the percentage-based rectangle on the drawing ruler itself. Its counterpart keeps the same drawing dimensions and is then scaled into the frame.
What happens when the triangle’s frame narrows?
The two published renders hold the artwork steady. In the wider frame, which the consortium sets at 300 by 200 pixels, the scale is 0.2 in both directions. That is the width of the display divided by the width on the drawing ruler, and the same calculation for height produces the same result. A diagonal on the drawing keeps its angle. The yellow rectangle fills the display and the triangle keeps the proportions it had on the drawing ruler.
The consortium’s narrower frame measures 150 by 200 pixels, so the horizontal scale drops to 0.1 while the vertical scale stays at 0.2. The height is still the same on screen; the base of the triangle takes up half as much screen width. Its sloping sides look steeper. The line in the file that draws the triangle is unchanged. So is the line that picks out the drawing rectangle. All the visible change comes from the altered width of the display frame and the fit instruction that lets the scales diverge.
| Viewport | Horizontal scale | Vertical scale | Red triangle |
|---|---|---|---|
| 300 × 200 pixels | 0.2 | 0.2 | Original proportions |
| 150 × 200 pixels | 0.1 | 0.2 | Narrower at the same height |
That instruction is preserveAspectRatio="none". Aspect ratio is the proportion of width to height. Here, none allows separate scales so the drawing rectangle can match the display frame edge to edge. The wider example happens to start with matching proportions, so none yields equal scales there too. The narrow render is the one that reveals what was permitted all along. Resizing a symbol with lettering or round features would run the same calculation across those features as across the triangle.
There is lettering in the original example. The phrase “Stretch to fit” crosses the picture, and the browser works on it in the same coordinate system as the triangle and the yellow rectangle. That makes it another clue in the narrower render: the glyphs, the drawn shapes of the letters, grow narrower along with the triangle. The words keep their place relative to the artwork. A uniform scale would keep the shape of each letter as well.
Where do meet and slice put the extra space?
The consortium puts a yellow smiley face inside a tall black rectangle and repeats it across a grid of little viewports. In a wide, shallow viewport with meet, the browser scales the drawing rectangle until its height matches the frame. The width fits with room to spare. A left alignment leaves the spare room on the right; a centered alignment divides it between the sides. A right alignment leaves it on the left. The yellow circle stays round in all of them.
With slice in that same wide, shallow frame, the browser scales the drawing rectangle until its width covers the frame. Its height now extends beyond the frame. A top alignment keeps the top of the drawing rectangle at the top edge, so the excess falls below; a bottom alignment puts the excess above. Centering shares the excess between top and bottom. The spec’s rendered grid clips the overhang at the frame edges. Clipping means that artwork outside those edges is hidden; the chapter ties that behavior to the element’s overflow setting.
| Fit | Edge that sets the scale | Where alignment has room to work |
|---|---|---|
| meet | Top and bottom of the drawing rectangle fit the frame height | Spare space to the left, right, or both |
| slice | Left and right sides of the drawing rectangle cover the frame width | Overhang above, below, or both |
| none | Each dimension scales to its matching frame edge | Both spans fill the frame, with proportions changed |
The default fit, xMidYMid meet, centers the drawing rectangle in the viewport along both axes. In the grid’s wide frame, horizontal alignment moves the drawing under meet, while vertical alignment moves it under slice. The tall, narrow frame reverses those roles: meet leaves room above and below the drawing, and slice lets it spill past the sides. An editor looking at a frame of either shape can tell which alignment setting will move an empty band or bring a cropped edge into view.
The order of those decisions explains why a change from centered to left alignment sometimes appears to do nothing. In the wide frame under slice, the drawing’s width already matches the viewport’s width. Moving the horizontal anchor from the center to the left edge lands the artwork at the same horizontal position. It is the vertical anchor that moves the overhang there. The chapter’s calculation applies the alignment offset only to the difference between the frame and the scaled drawing span along that axis; when the span matches, that difference is zero.
The next section of the chapter puts an svg element inside another one. There, the outer drawing can supply a location and a span for an inner frame, and the inner element establishes a viewport of its own. In the consortium’s nested example, the inner rectangle starts a quarter of the way across and a quarter of the way down the outer one. Its width and height are half those of the outer frame, leaving a frame within the drawing itself.
Frequently Asked Questions
Where do an SVG element’s width and height take effect?
The outer svg element’s width and height operate in the coordinate system in place before its viewBox mapping. The consortium’s chapter also describes how a containing page’s CSS layout can establish those viewport dimensions when the graphic is embedded. The size on the page can therefore come from the layout hosting the SVG.
What happens when the SVG viewport and the viewBox have different proportions?
With a viewBox and the default fit, the browser centers the entire drawing rectangle in the viewport at a uniform scale. The extra space falls at the sides of a relatively wide frame or above and below the drawing in a relatively tall one. Artwork or backgrounds can show in that space.
Can SVG viewBox values be separated by commas?
The viewBox grammar allows spaces, commas, or a combination between values, according to the consortium. The order stays the same whichever separator the file uses.
What happens if the width or height in the SVG viewBox is zero?
The World Wide Web Consortium sets out the rule for viewBox dimensions in its SVG specification: "A value of zero for <width> or <height> disables rendering of the element, consistent with the treatment of other zero-sized viewports." A negative width or height makes the viewBox invalid, so the browser treats the attribute as absent.
Sources
- World Wide Web Consortium: Scalable Vector Graphics (SVG) 2: Coordinate Systems, Transformations and Units