🔒 100% private — runs in your browser, nothing is uploaded

SVG viewBox Calculator

Compute the right viewBox for an SVG — from content bounds plus padding, fitted to a target aspect by expanding or cropping, with a live preview.

The viewBox is the four-number contract between an SVG’s internal coordinates and whatever box it is displayed in: viewBox="min-x min-y width height" declares which slice of the drawing’s plane the viewport shows, and the browser scales that slice to fit. Most viewBox bugs are arithmetic done by eye — a window that clips a stroke, or one framed for the wrong aspect so preserveAspectRatio letterboxes the art unexpectedly. This calculator does the arithmetic explicitly. From content bounds plus padding the window is direct: subtract the padding from x and y, add twice the padding to width and height (a percentage padding is taken from the larger content side, so a 200 × 100 drawing at 10% pads by 20 units, not by 20 horizontally and 10 vertically). Worked example at the defaults: content 10, 20, 200 × 100 with 10 units of padding gives exactly viewBox="0 10 220 120".

The numbers you type are plain geometry parameters — they are never uploaded, never stored and live only in page memory on your device, and because they carry no text or business data they are also carried in the page link so you can share a specification. The preview draws your content bounds (solid) inside the computed viewBox window so you can see the padding and any aspect fit before you paste the attribute.

Share this exact result — the link carries these inputs, nothing is stored.

Quick answers

How do I calculate a viewBox from content bounds?

Subtract the padding from the content’s x and y, and add twice the padding to its width and height. Content at 10, 20 sized 200 × 100 with 10 units of padding gives viewBox="0 10 220 120". A percentage padding is taken from the larger content side.

Fitting that window to a square by expanding grows the height to 220 around the centre: viewBox="0 −40 220 220" — the calculator shows both steps and previews them.

What preserveAspectRatio goes with a fitted viewBox?

Match the fit you chose: an expanded (meet) window pairs with preserveAspectRatio="xMidYMid meet", a cropped (slice) window with "xMidYMid slice", and "none" only when you truly want the drawing stretched to fill its box. This page generates the matching pair together, because the two attributes behave as one decision.

Alignment variants (xMinYMin, xMaxYMax) anchor the letterboxing to a corner instead of the centre.

How to use

  1. Enter the bounds of your artwork in its own coordinate system: the left edge x, the top edge y, and the width and height of the content itself (from your editor’s bounding box, or the outermost elements of the drawing).
  2. Add padding in user units, or as a percentage of the larger content side — padding is what stops strokes at the artwork’s edge from being clipped by the viewport.
  3. Optionally set a target aspect (for example 1 × 1 for a square icon slot, or 16 × 9 for a banner): Expand grows the window symmetrically until the aspect matches and nothing is lost; Crop shrinks it symmetrically to fill the aspect, cutting the overflow.
  4. Copy the viewBox together with the suggested preserveAspectRatio, and check the preview: the dashed frame is the computed window and the solid block is your content bounds inside it.

About this tool

Aspect fitting is the second half of the job, and the page keeps its two directions honestly labelled. Expanding (the meet behaviour) grows the shorter dimension around the centre until the window matches the target aspect — for the default window fitted to 1:1, the height grows from 120 to 220 and y moves from 10 to −40, giving "0 −40 220 220", with all artwork still visible. Cropping (the slice behaviour) does the opposite, shrinking the longer dimension around the centre and accepting that the overflow is cut. The suggested preserveAspectRatio is generated to match the mode you chose, because a viewBox and its preserveAspectRatio only behave as expected as a pair. The neighbouring SVG Optimizer is a different tool: it shrinks SVG code, while this page computes geometry — an icon set usually wants both, in either order.

How it works

Padded window: x’ = x − p, y’ = y − p, w’ = w + 2p, h’ = h + 2p, where p is the padding in user units, or p = percent ÷ 100 × max(w, h) for a percentage padding.

Expand to aspect A = tw ÷ th: if w’ ÷ h’ < A, widen — w″ = h’ × A, x″ = x’ − (w″ − w’) ÷ 2; otherwise heighten symmetrically. Crop is the mirror operation, narrowing the longer dimension around the centre.

Worked check: bounds 10, 20, 200 × 100, padding 10 → "0 10 220 120"; fitted to 1:1 by expanding → "0 −40 220 220"; by cropping → "50 10 120 120".

What this tool can’t do

The calculator works from the bounds you give it — it does not parse an SVG file to measure the artwork. Bounds that ignore a filter’s glow, a marker, or a stroke’s outer half will still clip those effects; measure generously in your editor.

Fitting is always centred, matching xMidYMid. Artwork whose subject sits deliberately off-centre may need a hand-shifted min-x / min-y after the fit — the aspect arithmetic here is the correct starting point, not a composition tool.

Numbers are rounded to two decimal places in the output, which is far beyond display precision; the underlying geometry in the preview uses the same rounded values, so what you preview is exactly what you paste.

Worked examples

Icon with breathing room: Bounds 10, 20, 200 × 100 with 10 units of padding → viewBox="0 10 220 120" — strokes at the artwork’s edge now sit a full 10 units inside the window.

Square slot, expand: The same padded window fitted to 1:1 by expanding → "0 −40 220 220": the window grows vertically around the centre and nothing is cropped.

Square slot, crop: Fitted to 1:1 by cropping instead → "50 10 120 120": the window narrows to the central square and the sides of the artwork fall outside it — the right choice for a background, the wrong one for a logo.

preserveAspectRatio quick reference

preserveAspectRatioBehaviourPair it with
xMidYMid meetWhole window visible, letterboxed, centredAn expanded (meet) fit — icons, logos, diagrams
xMidYMid sliceWindow fills the box, overflow cropped, centredA cropped (slice) fit — backgrounds, textures
xMinYMin / xMaxYMax …Letterboxing anchored to a cornerArtwork with a deliberate off-centre subject
noneWindow stretched to the box, aspect ignoredAlmost nothing — distortion is rarely the intent

Frequently asked questions

What do the four viewBox numbers mean?

min-x, min-y, width, height — the rectangle of the drawing’s own coordinate plane that the viewport will display. The browser maps that rectangle onto the element’s box; preserveAspectRatio then decides what happens when the two rectangles have different aspect ratios (letterboxing with meet, overflow cropping with slice, or distortion with none).

Why does my SVG get clipped at the edges?

Usually because the viewBox ends exactly at the artwork’s bounds while strokes are drawn centred on their paths — half of an edge stroke falls outside the window. Adding padding of at least half the widest stroke width (this page’s padding field) is the standard fix, and the preview’s dashed frame shows the margin you have bought.

Expand or crop — which fit should I choose?

Expand (meet-style) when all the artwork must survive: icons, logos, diagrams. It changes the window, never the art, at the price of empty space when the result is placed in a differently-shaped box. Crop (slice-style) when the artwork is a background or texture that should fill its slot edge to edge and can afford to lose its outskirts.

How is this different from the SVG Optimizer?

The Optimizer rewrites SVG markup to make the file smaller; it does not decide geometry. This calculator decides the viewBox numbers — from bounds, padding and a target aspect — and does not touch your markup beyond the one attribute. They are usually used together on the same asset.

📦 Embed this tool on your site

<iframe src="https://verystep.com/embed/svg-viewbox-calculator/" title="SVG viewBox Calculator" width="100%" height="520" loading="lazy" style="border:0"></iframe>

Free to use. The embed shows a small "Powered by Verystep" link.

First time embedding? Read the step-by-step guide →

More free tools