Art, design & a little curiosity
Creative coding ↗

SVG viewBox: design a drawing that scales with its frame

Build a small SVG illustration in a logical coordinate system, then choose how that drawing should meet square, wide, and cropped frames. Learn what viewBox and preserveAspectRatio actually control.

Original abstract visual study for creative coding.
Original visual study / Creative coding

Separate the drawing space from its displayed frame

An SVG illustration has a viewport: the rectangular region in which it is displayed. Its viewBox names a rectangle in the drawing’s own user-coordinate space. The attribute takes four values: min-x, min-y, width, and height. A value such as viewBox="0 0 240 160" describes a logical canvas that starts at the origin, extends 240 units horizontally, and 160 vertically. These units describe the drawing coordinate system; the displayed width and height of the SVG are a separate sizing decision.

This separation is useful when one drawing must appear at different sizes. Place a circle at x=120, y=80 in the example coordinate system, then display the file in a 480 by 320 pixel frame or a smaller responsive frame. The relationship between shapes is retained because the viewport mapping scales the user space. Treat the viewBox as a camera frame around the drawing, not as a command to redraw every object at a new size. A non-zero min-x or min-y is valid and shifts which part of the user-space drawing is included.

Choose what happens when the frame changes shape

A square viewBox placed in a wide rectangle creates a proportion mismatch. SVG’s preserveAspectRatio attribute controls the mapping. The default alignment, xMidYMid meet, keeps proportions, fits the entire viewBox inside the viewport, and centers any leftover space. That can leave bands at the sides or top. Choosing slice also preserves proportions but fills the viewport, so some of the viewBox can be cropped. Choosing none fills the frame by scaling x and y independently; circles can become ellipses and lettering can stretch.

None is not inherently a bug: distortion may be part of an intentional transformation. But for an illustration whose geometry should remain recognizable, start with the default meet behavior. Use slice when a frame must be covered and a planned crop is acceptable, then move important marks away from the edges. Inspect the artwork at the actual narrow and wide placements. A mathematically valid mapping can still cut a hand, title, or decorative line in an awkward place. The attribute controls geometry, while the composition still needs a designer’s crop decisions.

Make a coordinate system you can reason about

Choose dimensions that help you think. A 240 by 160 landscape canvas gives easy landmarks: its center is (120, 80), and its corner guides are (0, 0), (240, 0), (240, 160), and (0, 160). Draw a margin rectangle and a few recognizable shapes before adding detail. When the viewBox starts at zero, these coordinates are direct. If you move the origin, remember that the first two values define the top-left of the visible user-space rectangle; they do not translate each child shape independently.

Avoid changing width, height, and viewBox together until you know which behavior you are testing. Width and height help establish the rendered viewport; viewBox defines which user-space rectangle maps into it. If the picture seems too large, first check whether the viewBox is smaller than the content you intended to show. If one shape seems disproportionately large, check its user-unit dimensions against the coordinate space. Edit one variable, reload, and compare the same known landmark. This turns a vague “SVG scaling problem” into a testable framing question.

Review the drawing in its real contexts

A logo-like emblem, an icon, and a decorative banner may share an SVG file, but they do not necessarily share one framing policy. For an icon, an even square viewport and a centered meet mapping often preserve the silhouette. A wide banner might use a wide viewBox. A card image designed to fill a crop can use slice if the key content remains within a safe area. Record the intended fit beside the asset so a later person does not mistake unused space for a mistake or remove essential edges to eliminate it.

Test more than one embedding size and aspect ratio. Compare the result against the original logical drawing, check that strokes and small details remain legible, and look for clipping where nested viewports or container overflow add another crop. The specification explains how the viewBox and preserveAspectRatio values determine a scale and translation; it cannot decide whether the resulting composition is good. Keep a copy of the uncropped source artwork and make your presentation frame an explicit design choice.

Put it into practice

Try it yourself

  1. Draw a 240-by-160 guide rectangle and mark the center at (120, 80).
  2. Set the SVG viewBox to "0 0 240 160" and place three simple shapes using those coordinates.
  3. Preview the drawing in a 240-by-240 frame using the default meet behavior; note the empty bands.
  4. Try slice and none one at a time. Record whether the result crops, leaves space, or distorts the shapes.
  5. Choose a behavior for the actual design goal, add a safe margin if cropping is intended, and check it at two display sizes.
A worked example

Worked example: a paper kite emblem

Make a fictional emblem with a diamond kite centered at (120, 78), a short tail, and three small bows. The logical drawing is 240 by 160. First place it in a square frame with xMidYMid meet: the whole landscape drawing remains visible, with unused space above and below. Next try xMidYMid slice: the square is covered, but the left and right edges of the logical frame are cropped. Move the kite inward only if the square crop is a real required placement. Finally try none and observe how the diamond changes proportions as the square frame changes. Keep the original horizontal version and label each intended fit; do not silently stretch the mark to make every frame look full.

References & further reading

Examples and exercises in this guide are original teaching scenarios.

Keep following the thread.

More creative coding ↗