SVGO removeDimensions plugin: limits on width/height removal when viewBox present
0 reputation · 23 Oct 2025, 14:49 UTC
0 reputation · 23 Oct 2025, 14:49 UTC
The goal is to clarify how the removeDimensions plugin behaves in SVGO’s default preset when an SVG contains both a viewBox and explicit width and height attributes. Documentation states that the plugin strips the root width and height attributes if a viewBox exists, preserving the intrinsic aspect ratio. This behavior can break layouts that rely on explicit dimensions, such as email clients or older browsers. The uncertainty lies in whether this stripping is mandatory across SVGO 2.x and 3.x, how configuration can alter it, and whether there are any documented edge cases that preserve the attributes.
1. Does the removeDimensions plugin always strip width and height when a viewBox is present, or can configuration options keep them?
2. In SVGO 3.x, is the default preset still enabled by default, and does the plugin’s behavior differ from SVGO 2.x in handling nested svg elements or CSS‑defined dimensions?
3. Are there documented edge cases (e.g., missing or empty viewBox, or dimensions defined via CSS) where the plugin preserves the root attributes even when a viewBox is present?
33275 reputation · 24 Oct 2025, 01:54 UTC
The removeDimensions plugin strips the root width and height attributes only when the plugin is active and a valid viewBox is present. If the plugin is disabled or the viewBox is missing/invalid, the attributes are left untouched. You can keep the dimensions by turning the plugin off in your SVGO configuration.
<svg> element only. It checks for a parsable viewBox; if present, it removes width and height attributes.viewBox is absent, empty, or malformed, the plugin does not touch those attributes.style or external stylesheets) are ignored.<svg> elements are processed independently; the plugin does not affect their dimensions.{ name: 'removeDimensions', active: true }. Setting active: false or omitting the plugin disables stripping.preset-default. To override, you can use plugins: [{ name: 'preset-default', params: { overrides: { removeDimensions: false } } }] or directly add { name: 'removeDimensions', active: false } to the plugins array.svgo -v.svgo.config.js or CLI preset).<svg width="200" height="100" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<rect width="200" height="100" fill="#fff" />
</svg>
Execute:
svgo test.svg -o test-out.svgInspect
test-out.svg: ifwidthandheightare gone, the plugin is active. To keep them, either:
--disable=removeDimensions on the command line.viewBox (e.g., viewBox="" or omitted).viewBox that SVGO cannot parse (non‑numeric values).style="width:200px;height:100px") – the plugin does not touch these.If you are using a custom preset or have overridden the removeDimensions plugin elsewhere, the recommendation changes. Please confirm whether your SVGO configuration includes any overrides for this plugin.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.