opyeb SVGForge

Guides · Sizing

viewBox versus width and height: how an SVG decides its size

Updated 28 Nov 2026 · 8 min read

An SVG has two sizes and confusing them causes most sizing bugs. The viewBox is the coordinate system the drawing was authored in: viewBox="0 0 24 24" says the artwork lives in a 24 by 24 unit square. The width and height, or the CSS box around the element, say how large that square is drawn on screen. Without a viewBox, there is nothing to scale and the element is just a box with a drawing at its natural size.

The default is the trap. An SVG with no width, height or viewBox is rendered at 300 by 150 CSS pixels, the same default as a replaced element. A file that looked correct in the editor can therefore appear enormous in a page. Adding a viewBox and letting CSS set the width to 100% is the usual fix.

AttributeWhat it controlsCommon mistake
viewBoxUser coordinate system and scaleRemoved by an optimiser that assumes CSS sizing
width / heightIntrinsic rendered sizeFixed pixels that ignore the parent
preserveAspectRatioFit inside a differently shaped boxSet to none, which stretches the art
vector-effectWhether stroke scalesUsed everywhere, making icons look inconsistent
CSS widthLayout size of the elementOverriding an inline width attribute without !important

Responsive by construction

For an icon that should fill its container, keep the viewBox, omit width and height, and set width: 100%; height: auto in CSS. The browser derives the height from the viewBox ratio. For an icon that must be exactly 24 px in a toolbar, set width and height attributes to 24 and keep the viewBox so the drawing still scales if the CSS changes later.

For a hero illustration that must crop rather than fit, use preserveAspectRatio="xMidYMid slice". That fills the box and crops the overflow, like background-size: cover. The default, meet, fits the whole drawing and leaves empty space, which is usually what an icon wants.

Stroke scaling and the icon grid

Icon sets are drawn on a grid, usually 24 by 24 or 20 by 20, with a stroke of 1.5 or 2 units. At 24 px the stroke is 1.5 to 2 px; at 48 px it becomes 3 to 4 px. That is correct for a scalable icon and wrong for a toolbar where every icon must keep a 2 px line. vector-effect="non-scaling-stroke" keeps the stroke constant but is not supported in every renderer and can make a scaled icon look unbalanced.

A related trap is a viewBox that does not start at zero. viewBox="4 4 16 16" is legal and crops to the middle of the coordinate space. Optimisers that rewrite the viewBox to start at zero change the crop, so a tool must not do that unless it also translates every coordinate. This site does not move the viewBox origin; it only rounds its numbers.

Keep the viewBox even when width and height are present. It is the only attribute that lets the same file serve a 16 px favicon and a 256 px app icon without a second export.

Sources

Questions

What happens if an SVG has no viewBox?

The drawing is placed at its intrinsic size. If width and height are absent, that size is 300 by 150 CSS pixels, which is why an icon without a viewBox often appears huge or clipped. CSS can resize the element, but the contents do not scale with it the way a viewBox makes them scale.

Does viewBox change the aspect ratio?

The viewBox defines the user coordinate system and therefore the aspect ratio of the content. The rendered box can have a different aspect ratio, and preserveAspectRatio decides whether the content is letterboxed or stretched. The default, xMidYMid meet, fits the whole drawing without distortion.

Why does my stroke get thicker when the icon is large?

Stroke width is expressed in user units, so it scales with the viewBox. An icon drawn with stroke-width 2 on a 24-unit viewBox shows a 2 px line at 24 px and a 4 px line at 48 px. Use vector-effect="non-scaling-stroke" when the stroke must stay constant.

Paste a file with no viewBox into the optimizer and the accessibility panel will flag it.

Open the optimizer

Written against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.

Back to the guides