SVG viewBox vs width and height: What Each One Does
An SVG has three numbers that decide how big it looks, and they are easy to confuse because all three are about size. width and height on the root element declare the box the drawing wants. viewBox declares the coordinate system that gets fitted into that box. Remove the third and the file stops being a drawing that scales and becomes a picture that is stretched. Measured: the same 60×40 rectangle, in a 240×200 box, lands at y=65 with viewBox and at y=36 without it — 40 pixels tall instead of 40, stretched vertically by 1.82 with its left edge unmoved.
I built seven files that draw the identical rectangle at user coordinates 20,20 with size 60×40, varied only those attributes, and rendered each one into four boxes with the image stretched to fill. Then I scanned the screenshots for ink and read the rectangle's true bounding box off the pixels, so every number below is where the pixels landed rather than where the specification says they should. The seven files are 127 to 189 bytes.

The seven files
| File | Size | Root attributes | Intrinsic size in an img |
|---|---|---|---|
vb-both | 152 B | width 240, height 110, viewBox 0 0 240 110 |
240×110 |
vb-nosize | 127 B | viewBox only, no width or height | 300×138 |
vb-novb | 130 B | width 240, height 110, no viewBox | 240×110 |
vb-wonly | 139 B | width 240, no height, viewBox | 240×110 |
vb-vb-half | 151 B | viewBox 0 0 120 55 at 240×110 |
240×110 |
vb-par-none | 179 B | viewBox, preserveAspectRatio="none" |
240×110 |
vb-par-slice | 189 B | viewBox, preserveAspectRatio="xMidYMid slice" |
240×110 |
The intrinsic column is what an <img> gets when no CSS touches it: the numbers the browser reports as naturalWidth and naturalHeight. Six files agree with their attributes. The seventh — the one with a viewBox and no width or height — reports 300×138, because the default width of a replaced element is 300 pixels and the height then follows the ratio in the viewBox: 300 × 110/240 = 137.5, rounded up to 138.
That one number explains most of the surprise in this area. A file with a viewBox but no size is not "responsive"; it is 300 pixels wide until something else says otherwise.
When the box has the file's own shape
Start with the honest case: every file into a 240×110 box, which is the shape all seven were drawn for.
| File | Rectangle on screen | Painted |
|---|---|---|
vb-both | 20, 20 — 60×40 | 2400 px |
vb-nosize | 20, 20 — 60×40 | 2400 px |
vb-novb | 20, 20 — 60×40 | 2400 px |
vb-wonly | 20, 20 — 60×40 | 2400 px |
vb-vb-half | 40, 40 — 120×70 | 8400 px |
vb-par-none | 20, 20 — 60×40 | 2400 px |
vb-par-slice | 20, 20 — 60×40 | 2400 px |
Six files are identical — the rectangle at 20,20, 60×40, 2,400 pixels. The exception is vb-vb-half, which declares a viewBox of 0 0 120 55: half the canvas, so everything inside is drawn at twice the size, and the rectangle at 40,40 measures 120×80. It runs off the bottom of the box, so what survives is 120×70 and 8400 painted pixels instead of 9,600. A viewBox smaller than the drawing is a crop window, not a scale setting.
When the box is taller than the file
Change the box to 240×200 and keep the image stretched to fill it. This is one column of a responsive grid, or a detail view with more room than the thumbnail had, and it is where the three attributes separate.
| File | Rectangle on screen | Scale | Painted |
|---|---|---|---|
vb-both | 20, 65 — 60×40 | 1.00, centred | 2400 px |
vb-nosize | 20, 65 — 60×40 | 1.00, centred | 2400 px |
vb-wonly | 20, 65 — 60×40 | 1.00, centred | 2400 px |
vb-novb | 20, 36 — 60×74 | 1.00 horizontally, 1.82 vertically | 4440 px |
vb-par-none | 20, 36 — 60×74 | 1.00 / 1.82, on purpose | 4440 px |
vb-par-slice | 0, 36 — 48×74 | 1.82, cropped | 3551 px |
vb-vb-half | 40, 85 — 120×80 | 2.00, centred | 9600 px |
Three files with a viewBox and the default preserveAspectRatio behave the same way: the content keeps its shape, scales by the smaller of the two ratios (here 1.0), and is centred, which in a 240×200 box leaves 45 pixels of empty space above and below. The rectangle moves from y=20 to y=65 and stays exactly 60×40. This is meet, the default, and it is why a logo does not distort when its container changes shape.
vb-novb is the surprise, and it took a pixel measurement to see. Without a viewBox there is no coordinate system to fit, so the browser renders the file at its intrinsic 240×110 and then stretches that picture to 240×200, like a JPEG in an <img> with object-fit: fill. The rectangle's left edge stays at x=20 because the horizontal scale is 1.0; its top edge moves up to y=36 and its height becomes 74 because the vertical scale is 200/110 = 1.82. No viewBox does not mean "the drawing stays put"; it means "the drawing is a bitmap and the box scales it".
When the box is narrower than the file
A 120×110 box, which is what a file gets in a two-column mobile grid:
| File | Rectangle on screen | What happened |
|---|---|---|
vb-both | 10, 37 — 30×21 | scaled to 0.5, centred vertically |
vb-novb | 10, 20 — 30×40 | 0.5 horizontally, 1.0 vertically |
vb-par-none | 10, 20 — 30×40 | the same, deliberately |
vb-par-slice | 0, 20 — 20×40 | cropped 40 of 60 pixels wide |
The slice row is the one that ships bugs. To fill a 120×110 box from a 240×110 drawing it scales to the larger ratio, which is 1.0 vertically, giving a 240×110 picture centred in a 120-pixel-wide window: 60 pixels are cut from each side, and there is no scrollbar, no warning and no visual hint in a build log. The rectangle still paints 800 pixels, a third of it.
Why this passes review and breaks in production
The last box is 480×220, twice the file's own shape. Every file that has a viewBox paints the rectangle at 40,40, 120×80, 9600 pixels — a clean 2×. vb-novb lands on exactly the same numbers, and vb-par-slice too, because the box ratio and the file ratio agree.
That is the trap. As long as the container has the file's aspect ratio, a file with a viewBox and a file without one are indistinguishable. A design review on a matching frame shows nothing, a test at one breakpoint passes, and the difference only appears when the container changes shape — a taller card, a mobile crop, a CMS that lets an editor set a fixed height. If you have ever shipped an SVG that "looked fine in Figma" and stretched on the site, this is the mechanism.
The arithmetic you can check yourself
The browser's rule is short enough to implement, and implementing it is the fastest way to predict what a file will do in a box you cannot see yet.
// What a browser does with a viewBox inside a box of a different shape.
function fit(box, vb, par = 'xMidYMid meet') {
const sx = box.w / vb.w;
const sy = box.h / vb.h;
const [align = 'xMidYMid', mode = 'meet'] = par.trim().split(/\s+/);
if (mode === 'none') {
return { sx, sy, dx: 0, dy: 0, scale: null, crop: false, stretch: true };
}
const s = mode === 'slice' ? Math.max(sx, sy) : Math.min(sx, sy);
const freeX = box.w - vb.w * s;
const freeY = box.h - vb.h * s;
const dx = freeX * (align.includes('xMax') ? 1 : align.includes('xMin') ? 0 : 0.5);
const dy = freeY * (align.includes('YMax') ? 1 : align.includes('YMin') ? 0 : 0.5);
return { sx: s, sy: s, dx, dy, scale: s, crop: freeX < -0.5 || freeY < -0.5, stretch: false };
}
// Where a user-space rectangle lands on screen, given the above.
function place(box, vb, par, rect) {
const f = fit(box, vb, par);
return {
x: rect.x * f.sx + f.dx,
y: rect.y * f.sy + f.dy,
w: rect.w * f.sx,
h: rect.h * f.sy,
};
}
fit() returns the scale and the offsets for a box of any shape. For the 240×200 box it gives scale = 1 and dy = 45, which puts the rectangle's top edge at 20 + 45 = 65 — the measured value. For the 120×110 box it gives scale = 0.5 and dy = 27.5, so the rectangle is 30×20 at y = 10 + 27.5 = 37.5, and the screenshot says 37 because the pixel row rounds down. For slice in the 240×200 box it gives scale = 1.818 and dx = -98.2, which puts the rectangle's left edge at 20 × 1.818 − 98.2 = −61.8: 48 of its 109 pixels are visible, and the number matches.
The one case fit() deliberately cannot model is a file without a viewBox: there is no user coordinate system to map, so the file behaves like a bitmap and the browser scales the rendered picture instead. That is a different code path, and it is the reason vb-novb appears twice in the tables with numbers that look like none.
The attributes are hints, the CSS is the rule
width and height on an SVG are presentational hints, which means any CSS declaration wins. A stylesheet with img { width: 100%; height: auto } overrides both attributes and lets the viewBox ratio decide the height — the arrangement most sites want, and the reason a file without a viewBox and without a ratio collapses to the default 300-pixel width instead of scaling.
Two rules cover almost every case. Keep both attributes and a viewBox in the file, so anything that reads the file without CSS — an email client, a CMS thumbnailer, a PDF export — gets a sensible size and a ratio. Then let CSS do the layout: width: 100% with height: auto preserves the ratio, and aspect-ratio handles the container when the file has no viewBox to derive one from. What not to do is set one dimension in CSS and the other in an attribute and hope: that is where the 1.82 stretch comes from.
The checklist
- Keep
viewBox. Without it the file is a fixed picture and any box of a different shape stretches it. - Keep
widthandheighton the root element, even when CSS will override them. They are the intrinsic size every non-CSS consumer uses. - Never ship a
viewBoxsmaller than the drawing unless you want a crop. It looks like a scale setting and behaves like a window. - Leave
preserveAspectRatioalone unless you specifically wantnone(stretch) orslice(crop). Both are silent. - Use
width: 100%; height: autoinstead of setting both dimensions, so the ratio survives a breakpoint change. - Test at a box shape the file was not drawn for. A matching frame hides every one of these bugs.
What this does not cover
It does not cover <svg> inlined into HTML, where the root element's width and height are CSS lengths subject to the layout, and a percentage height resolves against a parent that may not have one — the classic "the icon is 0 pixels tall" bug. It does not cover <use> and nested viewBox either: a referenced symbol brings its own coordinate system, and the use element article measured what that costs.
And it does not cover text, which does not scale the way shapes do when a viewBox changes: the same font-size in a 120-unit-wide viewBox renders at twice the size of the 240-unit one, which is why a resized logo often ends up with the wrong label size. The Figma viewBox article covers the export side of that problem.
For the tooling: the SVG Editor shows the root attributes and lets you change them, the SVG to PNG converter renders at the size you ask for, which is the quickest way to see a stretch, and the image resizer is where a raster copy of the result usually ends up. One sentence: width and height are the box, viewBox is the coordinate system fitted into it, and a file without one is not scalable — it is a picture that gets stretched.
Related SVG Workflows
Sizing is the first thing that goes wrong with an exported file, and rarely the last. These pages cover the rest of the round trip.
- Figma SVG viewBox Fix — what the exporter writes into the root element, and how to correct it.
- Figma SVG Export Settings — the four switches that decide what the file contains.
- Figma Clipping Mask Fix — the other way a drawing gets cropped without a warning.
- SVG vs PNG — when a format that scales is the wrong choice.
- Optimize SVG for Web Performance — what to remove before you ship the file.
- SVG use Element Security — a second coordinate system hiding inside one file.
- SVG Editor — read and change the root attributes in the browser.
Try These Free Online Tools
No download required — convert, compress & optimize SVGs right in your browser



