Skip to content

fix: enable zoom for SVGs without explicit width/height dimensions#304973

Merged
mjbvz merged 2 commits intomicrosoft:mainfrom
yogeshwaran-c:fix/svg-zoom-no-dimensions-v2
Mar 26, 2026
Merged

fix: enable zoom for SVGs without explicit width/height dimensions#304973
mjbvz merged 2 commits intomicrosoft:mainfrom
yogeshwaran-c:fix/svg-zoom-no-dimensions-v2

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

SVG images that rely solely on a viewBox attribute (without explicit width/height attributes) cannot be zoomed in the image preview. When attempting to zoom, only the checkerboard background scales but the SVG content stays the same size, effectively ignoring zoom.

This happens because:

  1. image.naturalWidth is 0 for SVGs without intrinsic dimensions, causing firstZoom() to compute Infinity as the scale (division by zero)
  2. CSS zoom has no effect on the SVG content because the SVG renders at 100% of whatever container size it is given -- when zoom changes the container, the SVG just re-renders to fill the new size

Closes #240638

What is the new behavior?

  • When naturalWidth/naturalHeight are 0 (no intrinsic dimensions), explicit minWidth/minHeight are set on the image element to lock in concrete pixel dimensions before applying CSS zoom, so the SVG content scales proportionally
  • firstZoom() defaults to scale 1x when naturalWidth is 0, instead of computing Infinity
  • Explicit dimensions are cleared when returning to "scale-to-fit" mode so the image can flow naturally again

Additional context

Tested with SVGs that have:

  • Only viewBox (no width/height) -- e.g. <svg viewBox="0 0 100 100" xmlns="...">
  • width="100%" (no height) -- e.g. mermaid CLI output
  • Normal raster images are unaffected since they always have valid naturalWidth/naturalHeight

SVG images that rely solely on a viewBox attribute (without explicit
width/height) could not be zoomed in the image preview because they
have no intrinsic dimensions. CSS zoom had no effect since the SVG
would simply re-render to fill whatever container size it was given.

- Set explicit minWidth/minHeight on the image element when
  naturalWidth/naturalHeight are 0, giving CSS zoom concrete pixel
  dimensions to scale
- Handle firstZoom() division by zero when naturalWidth is 0 by
  defaulting to scale 1x
- Clear explicit dimensions when returning to scale-to-fit mode

Fixes microsoft#240638
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 26, 2026
@mjbvz mjbvz enabled auto-merge March 26, 2026 07:06
@mjbvz mjbvz merged commit d5370df into microsoft:main Mar 26, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVG without set width or height can't be zoomed

4 participants