SVG viewBox Explained: Scaling Without Surprises
The SVG viewBox is an attribute that defines the coordinate system of your drawing: four numbers that say which rectangle of the drawing canvas should be visible and stretched to fit the space the SVG occupies on the page. Once you understand those four numbers, scaling, cropping and zooming an SVG stop feeling like guesswork. This guide explains how the viewBox works, how it relates to width and height, and how to fix the most common "my SVG won't resize" bugs.
- The viewBox is
min-x min-y width height, written in the SVG's own units, not in screen pixels. widthandheightset the size of the viewport on the page; the viewBox decides what is drawn inside it.- Without a viewBox, an SVG is just a fixed-size picture: changing its displayed size does not scale the artwork.
preserveAspectRatiocontrols what happens when the viewBox and the viewport have different proportions.
What the Four viewBox Numbers Mean
The four numbers in a viewBox are the left edge, the top edge, the width and the height of the visible region, in that order. They are separated by spaces or commas, and they are measured in user units, which are the units your paths and shapes use for their coordinates.
<svg viewBox="0 0 100 50" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="80" height="30" fill="steelblue"/>
</svg>
Here the visible region starts at x = 0, y = 0 and is 100 units wide and 50 units tall. The rectangle sits 10 units in from the left and top, and is 80 by 30 units. Those units are not pixels. If the SVG is displayed 400 pixels wide, each user unit becomes 4 pixels, and the rectangle is drawn 320 pixels wide.
viewBox vs width and height
The viewBox describes the drawing's internal coordinate system, while width and height describe how much room the SVG takes up on the page. The browser maps the first onto the second, scaling everything inside to fit. Think of the viewBox as the window onto your artwork and the width and height as the frame that window is mounted in.
<!-- Same drawing, three different display sizes -->
<svg viewBox="0 0 100 50" width="100" height="50"> ... </svg>
<svg viewBox="0 0 100 50" width="200" height="100"> ... </svg>
<svg viewBox="0 0 100 50" width="400" height="200"> ... </svg>
All three show identical artwork at different sizes, and it stays sharp at each one. See what an SVG file is for the basics.
If you omit width and height but keep the viewBox, the SVG becomes flexible. For inline SVG, it generally fills the width of its container and takes its height from the viewBox proportions, which is exactly what you want for responsive layouts. You can then control the size from CSS, for example with width: 3rem on an icon, and the height follows automatically.
Why an SVG without a viewBox won't scale
An SVG without a viewBox will not scale because there is no coordinate system to scale. The browser has no way of knowing how much of the canvas your artwork covers, so user units simply equal pixels. Changing the width and height just gives you a bigger or smaller window onto the same unscaled drawing.
<!-- No viewBox: the circle stays 100 units across -->
<svg width="400" height="400" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="50" fill="tomato"/>
</svg>
The circle is 100 pixels across and sits in the top-left corner of a 400-pixel box. Setting width: 100% in CSS makes the box larger, but the circle does not grow. Add viewBox="0 0 100 100" and the same markup now draws the circle so it fills the whole box at any size.
preserveAspectRatio: meet, slice and none
The preserveAspectRatio attribute tells the browser what to do when the viewBox and the viewport have different aspect ratios. It has two parts: an alignment, and a mode. The default is xMidYMid meet, which centers the drawing and scales it so everything stays visible.
meet: fit everything inside
With meet, the drawing is scaled uniformly until it fits entirely inside the viewport, like object-fit: contain in CSS. If the proportions differ, you get empty bars on two sides. Nothing is cropped and nothing is distorted.
<svg viewBox="0 0 100 50" width="200" height="200"
preserveAspectRatio="xMidYMid meet">
<rect width="100" height="50" fill="seagreen"/>
</svg>
The 2:1 rectangle is drawn 200 wide and 100 tall, centered vertically in the 200 by 200 box, with 50 pixels of empty space above and below.
slice: fill the viewport and crop
With slice, the drawing is scaled uniformly until it covers the whole viewport, like object-fit: cover. The parts that overflow are clipped. This is useful for banner or hero artwork that must fill a wide strip regardless of screen size.
<svg viewBox="0 0 100 50" width="200" height="200"
preserveAspectRatio="xMidYMid slice">
<rect width="100" height="50" fill="seagreen"/>
</svg>
Now the rectangle is scaled to 400 by 200, so it fills the height and the left and right edges are cut off equally.
none: stretch to fit
With none, the drawing is stretched independently on each axis, so circles become ovals. It suits simple decorative shapes such as a full-width wave divider, rarely icons or logos.
<svg viewBox="0 0 100 50" width="200" height="200"
preserveAspectRatio="none">
<rect width="100" height="50" fill="seagreen"/>
</svg>
The default alignment xMidYMid centers the drawing; values like xMinYMin pin it to a corner instead, and alignment is ignored for none.
| Value | Scales | Crops? | Distorts? | CSS analogy |
|---|---|---|---|---|
xMidYMid meet (default) | Uniformly, to fit inside | No | No | object-fit: contain |
xMidYMid slice | Uniformly, to cover | Yes | No | object-fit: cover |
none | Separately on each axis | No | Yes | object-fit: fill |
Cropping and Zooming with the viewBox
You can crop or zoom an SVG without touching any of its shapes, simply by changing the viewBox. A smaller region means the drawing is magnified, and moving the origin pans across the canvas. This is handy when an exported SVG has too much empty margin around it.
<!-- Before: lots of empty margin -->
<svg viewBox="0 0 200 200" width="300" height="300"> ... </svg>
<!-- After: tightly cropped to the artwork -->
<svg viewBox="40 60 120 80" width="300" height="200"> ... </svg>
The new viewBox starts at x = 40, y = 60 and is 120 wide and 80 tall. Remember to update the height (or remove it) so the box has the same 3:2 proportions as the new viewBox; otherwise the default meet behavior will leave blank bars.
Common viewBox Bugs and How to Fix Them
Most viewBox problems come down to a few situations: the icon is clipped, the SVG has a fixed size it refuses to leave, or CSS sizing is not doing what you expected. Here is what each one looks like and how to resolve it.
The icon is clipped on one or more sides
A clipped icon usually means the viewBox is smaller than the drawing, or the origin is offset. Shapes outside the viewBox rectangle are not visible. Strokes are a frequent culprit: a stroke is centered on the path, so a shape that touches the viewBox edge loses half its stroke width outside the visible area.
Check that the viewBox covers the full artwork, including stroke widths. If the artwork starts at negative coordinates, the viewBox needs a negative min-x or min-y. As a quick test, temporarily enlarge the viewBox (for instance from 0 0 24 24 to -2 -2 28 28) and see whether the missing parts appear.
The SVG was exported with a fixed size
If an export contains width="512" height="512" but no viewBox, it will always render as a 512 pixel box with unscaled content. Add a viewBox that matches those numbers, and remove or override the fixed size.
<!-- Before -->
<svg width="512" height="512" xmlns="http://www.w3.org/2000/svg"> ... </svg>
<!-- After -->
<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"> ... </svg>
CSS sets only the width, and the height collapses or stays huge
The reliable pattern is to keep the viewBox, drop the width and height attributes, and size the element with CSS. See also inline SVG, img and CSS backgrounds.
When CSS sets only width, the height depends on what the SVG offers. With a viewBox and no height attribute, the height follows the ratio and everything works. Without a viewBox, or with a leftover height attribute, the browser may use a default height, leaving a tall empty gap or a squashed box.
How to Fix an SVG That Won't Resize
To fix an SVG that will not resize, make sure it has a viewBox that matches the artwork, then control the displayed size from CSS or from the embedding element. Work through these steps in order.
- Open the file in a text editor and look at the opening
<svg>tag. Note anywidth,heightandviewBoxattributes. - If there is no viewBox, add one using the original size:
viewBox="0 0 width height", with the numbers taken from the existingwidthandheight(strip units likepx). - Check that the viewBox covers all the artwork. If parts are cut off, enlarge it or shift the origin, remembering stroke widths.
- Remove fixed dimensions you do not want. Delete
widthandheightfrom the SVG tag if the layout should decide the size. - Size it with CSS. For example,
svg { width: 100%; height: auto; }for fluid graphics, or a fixedwidth: 2remfor an icon. - Reload and test at a few sizes. Resize your browser window and confirm the artwork scales with the box and keeps its proportions.
Once your SVG scales properly, it is worth tidying the file too. Our guide to SVG optimization techniques covers ways to reduce file size, and you can use the SVG optimizer to compress files. Because optimizers vary in how they treat attributes, always compare the result on screen and confirm the SVG still scales the way you want.
For a shorter reference written for the help center, see understanding SVG viewBox and viewport.
Frequently Asked Questions
What does viewBox="0 0 24 24" mean?
It means the visible region starts at the top-left corner (0, 0) and is 24 units wide and 24 units tall. All coordinates in the drawing are measured on that 24 by 24 grid, and the browser scales the grid to whatever size the SVG is displayed at.
Do I need both a viewBox and width and height?
You need a viewBox for the SVG to scale. Width and height are optional: include them to give the SVG a default size, or leave them out and let CSS or the surrounding layout decide.
Why is there empty space around my SVG?
Usually the viewBox is larger than the artwork, or its proportions differ from the width and height, so the default meet behavior adds bars. Tighten the viewBox around the artwork or match the aspect ratio of the box.
Can I change the viewBox with CSS?
No, the viewBox is an attribute and is not set through CSS. You can change it by editing the markup or with JavaScript, for example svg.setAttribute("viewBox", "0 0 50 50").
Ready to tidy up your files after fixing the viewBox? Try the ToSVG SVG optimizer and check the result on screen.