Keep an SVG icon proportional in a wide slot

Compare meet, slice, and none using the same circle in a 96 × 48 viewport.

Asset preparation2 min read

An icon that looks right in a square preview can stretch or crop when a component gives it a wide viewport. Keep the artwork fixed and change how the SVG fits its viewBox into that viewport.

Compare the same circle in three modes

Open the three-mode SVG sample or save this snippet as an SVG file. Change only preserveAspectRatio between runs.

svg
<svg xmlns="http://www.w3.org/2000/svg" width="96" height="48"
     viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet"
     overflow="hidden">
  <circle cx="12" cy="12" r="10" fill="#ffbd42"/>
</svg>

The viewport has room for four pixels per source unit horizontally and two vertically. The 20-unit circle makes the effect of each scale visible.

SettingHorizontal and vertical scalesResult in the 96 × 48 viewport
xMidYMid meet (default)2× and 2×A complete 40 × 40 px circle, centered.
none4× and 2×A complete 80 × 40 px ellipse.
xMidYMid slice4× and 4×An 80 × 80 px circle with 16 px clipped at the top and bottom.

meet fits the entire viewBox. slice fills the viewport while preserving proportions. The example uses overflow="hidden" to clip the overshoot. See the SVG 2 fitting rules.

Check the icon in its component

For an icon whose silhouette needs to stay complete, start with meet and check that the circle still has equal width and height at the component's smallest supported size. Use slice when the composition is meant to fill the slot and can afford to lose its edges. If part of a stroke is missing even with meet, check whether the painted stroke extends past the viewBox.

Image tools
All guides
Keep an SVG icon proportional in a wide slot | Formatry Media — Formatry