SVG viewBox Explained
Understand SVG viewBox coordinates, aspect ratio, cropping, and responsive sizing with practical markup examples.
Quick Summary
- The viewBox defines the SVG coordinate region mapped into its rendered viewport.
- Changing width and height changes display size; changing viewBox can crop, reveal, or rescale artwork.
- Preserve a correct viewBox during optimization and test non-square containers before shipping.
- Category
- Fundamentals
- Difficulty
- Beginner
- Reading Time
- 4 min
- Related Tool
- SVG Optimizer
- Best For
- Responsive icons, Cropping issues, SVG layout
- Avoid For
- Blind viewBox removal
Introduction
The viewBox attribute connects an SVG's internal coordinate system to the space where the browser renders it. Many scaling bugs happen because width, height, and viewBox are treated as interchangeable. They are not.
width and height describe the viewport size. viewBox describes which internal coordinates should be visible inside that viewport.
Read the four numbers
<svg viewBox="0 0 200 100" width="400" height="200">
<rect x="0" y="0" width="200" height="100" fill="tomato" />
</svg>The values are min-x, min-y, viewBox-width, and viewBox-height. This example exposes a 200 by 100 coordinate region and renders it into a 400 by 200 viewport. The aspect ratios match, so the artwork scales evenly by a factor of two.
What changes do
| Change | Effect | Common use | Risk |
|---|---|---|---|
| Change CSS width | Resizes the viewport | Responsive layout | Can distort if height is forced incorrectly |
| Change viewBox width | Shows a wider or narrower coordinate range | Intentional crop or padding | Can clip artwork |
| Change min-x or min-y | Shifts the visible coordinate region | Remove offset or focus an area | Can hide strokes and shadows |
| Remove viewBox | Loses scalable coordinate mapping | Rare fixed-size export | Often breaks responsive behavior |
| Change `preserveAspectRatio` | Controls fitting and alignment | Crop or letterbox behavior | Can stretch or unexpectedly crop |
Responsive sizing
A common responsive pattern keeps the viewBox and lets CSS control width.
<svg
viewBox="0 0 200 100"
width="200"
height="100"
style="width: 100%; height: auto"
aria-hidden="true"
>
<!-- artwork -->
</svg>The attributes provide an intrinsic ratio and fallback dimensions. CSS allows the graphic to shrink or grow with its container without changing the internal coordinates.
Cropping whitespace
Large empty margins often come from a viewBox that is much larger than the artwork bounds. Tightening it can improve alignment, but visible bounds are not always the full required bounds. Strokes extend around paths, filters can draw outside geometry, and animation may move elements beyond the initial shape.
Before changing a viewBox, inspect strokes, filters, masks, and motion across every intended state.
Common mistakes
Copying path bounds directly
Path geometry does not automatically include stroke width, markers, shadows, or filtered pixels. A mathematically tight box can clip visible output.
Setting both CSS dimensions without preserving ratio
Forcing unrelated width and height values can stretch the viewport. Use height: auto, aspect-ratio, or a deliberate preserveAspectRatio strategy.
Removing width and height everywhere
A viewBox makes scaling possible but does not always reserve layout space by itself. Explicit dimensions or CSS aspect ratio help prevent layout movement.
Optimizing away the viewBox
A smaller file is not useful if the graphic no longer scales. Treat viewBox as functional data.
Practical workflow
- 1Record the current viewBox, width, height, and visible output.
- 2Identify whether the problem is viewport sizing, internal coordinates, or empty margins.
- 3Inspect strokes, filters, masks, and animation outside basic path bounds.
- 4Change one viewBox value at a time and compare the result.
- 5Test square, wide, and narrow containers.
- 6Confirm intrinsic dimensions and layout stability.
- 7Preserve the reviewed values during optimization and component conversion.
Best practices
- Keep a correct viewBox on responsive SVG.
- Preserve the intended aspect ratio unless distortion is deliberate.
- Reserve layout space with dimensions or CSS.
- Include stroke and filter overflow when cropping.
- Test at small and large rendered sizes.
- Store the original export before editing coordinates.
FAQ
Is viewBox required?
SVG can render without it, but a viewBox is the normal foundation for responsive scaling and a stable internal coordinate system.
Can viewBox contain decimals or negative values?
Yes. Negative origins and decimal dimensions are valid when they describe the required coordinate region.
What does `preserveAspectRatio="none"` do?
It allows non-uniform scaling to fill the viewport, which can distort the graphic. Use it only when stretching is intentional.
Why is a stroke clipped after tightening the viewBox?
Strokes extend beyond the underlying path. Add enough coordinate space for half the stroke width and any markers or filters.
See the W3C SVG coordinate systems specification for the normative model.
Preserve scaling during cleanup
Optimize a copy of the SVG and confirm that its viewBox and responsive behavior remain intact.
Open SVG Optimizer