fix: enable zoom for SVGs without explicit width/height dimensions#304973
Merged
mjbvz merged 2 commits intomicrosoft:mainfrom Mar 26, 2026
Merged
fix: enable zoom for SVGs without explicit width/height dimensions#304973mjbvz merged 2 commits intomicrosoft:mainfrom
mjbvz merged 2 commits intomicrosoft:mainfrom
Conversation
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
mjbvz
approved these changes
Mar 26, 2026
bpasero
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
SVG images that rely solely on a
viewBoxattribute (without explicitwidth/heightattributes) 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:
image.naturalWidthis 0 for SVGs without intrinsic dimensions, causingfirstZoom()to computeInfinityas the scale (division by zero)zoomhas 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 sizeCloses #240638
What is the new behavior?
naturalWidth/naturalHeightare 0 (no intrinsic dimensions), explicitminWidth/minHeightare set on the image element to lock in concrete pixel dimensions before applying CSS zoom, so the SVG content scales proportionallyfirstZoom()defaults to scale 1x whennaturalWidthis 0, instead of computingInfinityAdditional context
Tested with SVGs that have:
viewBox(no width/height) -- e.g.<svg viewBox="0 0 100 100" xmlns="...">width="100%"(no height) -- e.g. mermaid CLI outputnaturalWidth/naturalHeight