Responsive SVG: sizing, scaling and adapting to screens

Make SVG fit any container with viewBox and CSS, control cropping with preserveAspectRatio, reuse icons with symbol, and swap artwork by screen size.

SVG is resolution independent, but that does not make it responsive on its own. A graphic becomes responsive when its coordinate system, its display size and its behavior at odd aspect ratios are all set on purpose. This guide covers the attributes and patterns that do that work.

Separate the drawing from the display size

Two things decide how an SVG looks on screen:

  1. The coordinate system you draw in, set by viewBox.
  2. The size of the box it is shown in, set by width and height or by CSS.

MDN describes viewBox as four numbers, min-x min-y width height, that map a rectangle of user space onto the SVG viewport. The same numeric value can render at very different sizes depending on that rectangle. A <circle r="4"> is small in a 0 0 100 100 viewBox and huge in a 0 0 10 10 one. The details of that mapping are in SVG viewBox.

For responsive work, set the viewBox and let CSS decide the size:

<svg viewBox="0 0 400 240" role="img" aria-label="Quarterly totals">
  <!-- drawing -->
</svg>
svg.chart {
  width: 100%;
  height: auto;
  max-width: 40rem;
}

With the viewBox present, the browser has an aspect ratio to work from, so the height follows the width. In SVG 2, width and height can be set as CSS properties on the outer <svg> element, which is why the rule above works. Without a viewBox there is no coordinate rectangle to scale, so a different box size crops or pads the drawing instead of scaling it. This is the most common reason an exported SVG refuses to shrink.

If you also keep width and height attributes in the markup, treat them as a default size. CSS can override them. Some exporters add fixed dimensions to every file, so check what your tool produces. SVG optimization covers cleaning those out.

Control the fit with preserveAspectRatio

When the box has a different shape than the viewBox, preserveAspectRatio decides what happens. It takes an alignment and an optional meet or slice. The default is xMidYMid meet.

  • meet scales the whole viewBox to fit inside the box and leaves empty space on one axis.
  • slice scales it to cover the box and crops whatever overflows.
  • none stretches the drawing to fill the box exactly, which distorts it.

The alignment part (xMin, xMid, xMax combined with YMin, YMid, YMax) says where the viewBox sits. MDN also notes that without a viewBox, preserveAspectRatio has no effect on an <svg>.

Each tile below puts the same 100 by 100 drawing into a 90 by 50 viewport. Edit the three preserveAspectRatio values and compare.

Use meet for charts and logos, where nothing may be cut off. Use slice for decorative backgrounds that should always fill their area. Use none rarely: it is mainly useful for stretchable shapes such as dividers, where distortion is the goal.

Reusable pieces with symbol and use

A <symbol> is a template that is not drawn directly. Each <use> that points at it draws an instance. A symbol can carry its own viewBox and preserveAspectRatio, so one definition scales into whatever width and height you give each instance. This is the usual basis for an icon sprite.

Note that every instance keeps the proportions of the 24 by 24 design. If you need an icon to change its detail at small sizes, a single symbol will not do that. Draw separate symbols for each size instead.

Adapting layout, not just scale

Scaling shrinks everything equally. A chart with long labels might need fewer labels on a phone rather than tiny ones. Options, from simplest to most involved:

Switch files with <picture>. The media attribute on a <source> skips that source when its query is false, and the first match wins. MDN uses this for art direction, giving a different image per viewport condition, and it always requires a fallback <img>.

<picture>
  <source srcset="trend-compact.svg" media="(max-width: 40em)" />
  <img src="trend-full.svg" alt="Monthly signups rose through the year." />
</picture>

Hide detail with CSS on inline SVG. Give secondary elements a class, then hide them below a width in your page stylesheet.

@media (max-width: 40em) {
  .chart .minor-labels { display: none; }
}

Query the container, not the viewport. Container queries let a component respond to its own parent’s width. The syntax is container-type: inline-size on the parent and @container (width > 700px) for the rules. Put the container on an HTML wrapper around the SVG, then toggle classes on the SVG’s parts as above. MDN’s container query page does not cover declaring a container on the <svg> element itself, so the wrapper is the safer choice.

.chart-card { container-type: inline-size; }

@container (width < 30em) {
  .chart .minor-labels { display: none; }
}

Limits of SVG used as an image

When an SVG is loaded through <img>, CSS background-image or similar, MDN lists restrictions: JavaScript is disabled, external resources such as images and stylesheets are not loaded unless inlined through data: URLs, and :visited styles are not rendered. For a responsive graphic that depends on your page’s CSS classes, inline the SVG instead. See SVG vs PNG and JPG for the trade-offs between formats and SVG in frontend frameworks for inlining in component code.

A short checklist

  • Every SVG has a viewBox.
  • CSS sets the display size, with max-width where the graphic should not grow forever.
  • You chose preserveAspectRatio instead of accepting the default by accident.
  • Text inside the SVG scales with the drawing. Test the smallest screen you support and increase font size in the source if it becomes unreadable.
  • Strokes scale too. A 2-unit stroke in a 24-unit icon is thin when the icon is small, so test at the smallest size.
  • You tested at 375 pixels wide and at a wide desktop width.

For text sizing, accessible names and titles, read SVG accessibility. To add behavior on top of a responsive graphic, continue with SVG with JavaScript.

Sources

Checked against these references on 2026-11-21.