SVG viewBox, Width and Height Explained With Practical Examples

Understand how the SVG viewBox, width and height work together, why icons appear cropped or tiny, and how to inspect and preview SVG files safely.

Updated September 29, 2026 6 min read

Use the free SVG ViewerPreview SVG code or files safely, check viewBox and export PNG

An SVG icon looks perfect in the design tool, then shows up enormous on the website, squashed into a corner, or cut off at the edges. Another file refuses to scale at all. Nine times out of ten the explanation is in three attributes on the root <svg> element: width, height and viewBox.

This guide explains what those attributes mean, how they interact, and how to read an SVG file to diagnose sizing problems. It also covers why SVG files deserve a little caution and how to preview them without running anything inside them.

SVG is a coordinate system, not a grid of pixels

A PNG or JPG stores a fixed grid of coloured pixels. An SVG stores drawing instructions: "draw a circle with centre (60, 60) and radius 50", "draw this path". Because the shapes are described mathematically, they can be drawn at any size without blurring.

Those instructions use an internal coordinate system. The viewBox defines which part of that coordinate system is visible, and width and height define how big that visible area appears on the page.

The viewBox attribute

viewBox takes four numbers: min-x min-y width height.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120" width="240" height="240">
  <circle cx="60" cy="60" r="50" fill="#4f46e5"/>
</svg>

Here the drawing uses coordinates from 0 to 120 in both directions. The element is displayed at 240 by 240 CSS pixels, so every internal unit becomes two pixels on screen. Change width and height to 48 and the same circle is drawn at icon size, still perfectly sharp.

What width and height do

  • With a viewBox and no width/height, the SVG scales to fill its container, like a responsive image. This is usually what you want for icons and illustrations styled with CSS.
  • With width/height and no viewBox, the drawing is fixed. Setting a different size in CSS crops or pads the drawing instead of scaling it. This is the most common cause of "my SVG will not resize".
  • With both, width and height set the default display size, and the viewBox lets it scale cleanly if CSS overrides that size.

preserveAspectRatio

If the viewBox shape does not match the display shape (a square viewBox in a wide box), preserveAspectRatio decides what happens. The default, xMidYMid meet, keeps proportions and centres the drawing, leaving empty space at the sides. none stretches it to fill, which distorts the shapes.

Inspect an SVG with the SVG Viewer

Our SVG Viewer shows the rendered image next to the code, along with the numbers that explain its size.

  1. Open the SVG Viewer. A sample badge is already loaded.
  2. Paste SVG markup into the SVG code box, or click Open .svg file to load a file from your device (up to 10 MB).
  3. Look at the Preview. Switch the background between Checkerboard, White and Black to see transparent areas and to catch white shapes that disappear on a white page.
  4. Read the stat tiles underneath: Size (the width and height the SVG will display at), viewBox (or "none" if it is missing), Elements (how many shapes and groups it contains) and File size.
  5. Click Download SVG to save the code as a file, or choose a scale from 1x to 4x and click Download PNG for a raster copy. The note under the buttons tells you the exact pixel size and whether the background will be transparent.

The preview is displayed as an image, not as live markup, so any script embedded in the SVG never runs.

Why SVG files deserve caution

SVG is XML, and it can contain <script> elements, event handler attributes such as onload, and links to external resources. When an SVG is opened directly in a browser tab, or inlined into a page, those scripts can run. When it is displayed through an <img> tag, browsers render it as a static picture with scripts disabled.

That difference matters for user uploads. If your site accepts SVG uploads and serves them back as documents, a malicious file could run script on your domain. Display untrusted SVGs through <img>, serve them with the correct content type, or sanitise them first.

Other ways to view SVG files

A web browser

Dragging an SVG into a browser tab shows it at its intrinsic size. It is quick and accurate, but it opens the file as a document, so scripts inside it can run. Only do this with files you trust.

Your operating system

Windows File Explorer and macOS Finder can show SVG previews or thumbnails, depending on version and installed extensions. They are fine for identifying files but show no code or sizing information.

Vector editors

Inkscape, Illustrator, Figma and Affinity Designer open SVG for editing. Use them when you need to change the drawing itself. Be aware that saving from an editor often adds metadata and editor-specific attributes, which the SVG Optimizer can strip again.

A code editor

Any text editor shows the raw XML. Some have preview extensions. This is the best place to make small manual changes such as adding a viewBox.

Fixing common sizing problems

The SVG will not scale with CSS

Add a viewBox. If the file has width="200" height="100" and no viewBox, add viewBox="0 0 200 100" so the internal coordinates are defined, then remove the fixed width and height if you want it fully fluid.

Part of the drawing is cut off

Some shapes sit outside the viewBox area. Either enlarge the viewBox (for example from 0 0 100 100 to -10 -10 120 120) or move the shapes. Exported icons sometimes have a viewBox that ignores stroke width, which clips thick outlines.

The icon is tiny with lots of empty space

The viewBox is much larger than the artwork. Tighten it around the actual shapes so the icon fills its box.

It looks blank

Check the preview on the black background; the artwork may be white. Also check whether shapes use fill="currentColor", which inherits the text colour of wherever the SVG is placed.

Frequently asked questions

What does viewBox="0 0 24 24" mean?

The drawing uses coordinates from 0 to 24 horizontally and vertically, starting at the top-left corner. It is the standard grid for many icon sets and scales to any display size.

Do I need both width/height and viewBox?

The viewBox is what makes an SVG scale properly, so always keep it. Width and height are optional and act as a default size when no CSS sets one.

Can SVG files contain viruses?

An SVG can contain JavaScript that runs when the file is opened as a document. Viewing it as an image, as our viewer does, keeps scripts from running.

How do I convert an SVG to PNG at high resolution?

Choose a scale such as 3x or 4x in the viewer and click Download PNG. For batch or custom sizes, see our guide on exporting SVG as a high-resolution PNG or use SVG to PNG.

Why is the file size so large for a simple icon?

Design tools often embed metadata, editor data and long decimal numbers. An optimiser can usually remove a large share of that without changing how the icon looks.

Try it now: SVG Viewer

Loading tool…