removeViewBox: preserve viewBox for responsive scaling or reduce file size?
0 reputation · 08 Mar 2021, 18:08 UTC
0 reputation · 08 Mar 2021, 18:08 UTC
The objective is to configure the removeViewBox plugin so that SVGs remain responsive in browsers while still achieving the desired file‑size reduction.
SVGs that declare explicit width and height often rely on a viewBox to scale correctly. The plugin removes viewBox by default, but recent releases introduce a preserveViewBox option with values false, true, and auto. The exact default value for auto and how it behaves when width or height are omitted remain ambiguous. Additionally, the relative ordering of removeViewBox and cleanupIDs may influence whether IDs referenced by viewBox-dependent elements survive.
1. What is the current default value of preserveViewBox in the latest stable svgo release?
2. Does setting preserveViewBox to auto guarantee that viewBox is retained whenever width or height are omitted?
3. How does the ordering of removeViewBox relative to cleanupIDs affect the preservation of IDs that rely on viewBox?
35275 reputation · 09 Mar 2021, 02:39 UTC
Keep SVGs responsive by not removing viewBox when the SVG is used without fixed width and height. The safest configuration is to disable removeViewBox or set preserveViewBox to true for icons and inline graphics that must scale. Use preserveViewBox: auto only after you verify its heuristic on your exact SVGO version and SVG set.
Cannot be stated with certainty without checking your installed version. Defaults changed across SVGO 2 → 3 → 4 and differ between releases. Verify locally:
svgo --version
# inspect defaults
cat node_modules/svgo/lib/plugins/removeViewBox.js
Look for the plugin params definition and the default for preserveViewBox.
No. auto is a heuristic, not a guarantee. It may retain viewBox when width/height are absent, but the exact condition is implementation-defined per release and can change. Removing viewBox from an SVG without explicit width and height can cause rendering to collapse or scale incorrectly in browsers.
Ordering does not affect whether viewBox is preserved. removeViewBox decides viewBox presence based on its own params. cleanupIDs removes unused IDs and updates references. It does not read viewBox and viewBox does not reference IDs. The only interaction is that cleanupIDs can remove IDs that other elements reference independently of viewBox.
One missing diagnostic that changes the recommendation: what SVGO major version is installed in your build and whether your production SVGs are used inline without width/height. With that, the default can be confirmed and auto can be validated safely.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.