Skip to content

removeDimensions removes width and height from nested SVG elements #2217

Description

@vrugtehagel

Describe the bug
The removeDimensions option removes width and height attributes from nested <svg> elements, even when it is unsafe to do so.

To Reproduce
Take this example SVG:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
	<path fill="pink" d="M0 0h10v10H0Z"/>
	<svg x="1" y="1" width="8" height="8" viewBox="0 0 33 33">
		<path fill="red" d="M1 16a1 1 0 0 0 31 1 1 1 0 0 0-31-1"/>
	</svg>
</svg>

One can use SVGOMG to quickly see that toggling the "Prefer viewBox to width/height" shows a significant change in the rendered SVG.

Expected behavior
It is expected that this option only affects the outermost <svg> element, since the width and height on nested <svg> elements are used to correctly size and position the nested SVG.

Screenshots

Original SVG (rendered as PNG):

Image

Compressed SVG (as PNG):

Image

Desktop (please complete the following information):

  • SVGO 4.0.1
  • Node v22.22.1
  • OS: MacOS Tahoe 26.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions