SVGO plugin that reduces viewBox to minimum possible size so no wasted transparent space around svg
npm install svgo-autocropPlugin which can be added to svgo.
Reduces viewBox to minimum possible size so no wasted transparent space around svg.
This plugin focuses on making svgs as consistent as possible.
It's great for taking multiple libraries of 1000s of svgs, removing all padding, removing deprecated/redundant tags and
replacing the arbitrary colors svg libraries tend to use with a set color (we recommend setting 'currentColor' so the color is inherited/easily modifiable from html/css).
Tested with heaps of free svg libraries like Bootstrap Icons, Entypo+, Fontawesome
and Google's Material Design Icons and more using this config file.
Example Input
```
Example Output
``
Example Output if using {setColor: 'currentColor'} as parameters
``
| Parameter | Default | Recommended | Description |
| ------------------------------- | ----------- | --------------------- | ------------|
| autocrop | true | true | Disable auto cropping. Useful if you only want to use the removeClass or setColor functionality described below. |undefined
| includeWidthAndHeightAttributes | | false | If undefined, only updated width/height if SVG already has width/height (this is the default). If true, writes width/height. If false, deletes width/height (which we recommend so your SVG always scales). If true, you should also disable svgo's default removeViewBox plugin and optionally enable svgo's removeDimensions plugin - see example-config.js below for how to do this. |undefined
| padding | | undefined | Either number, object defining {top, bottom, left, right} or function accepting the parameters (viewboxNew, viewbox?, ast?, params?, info?) where viewboxNew should be updated with the required padding. We don't recommend specifying padding on your SVG - padding should be defined in your css. |false
| removeClass | | true (typically can be safely set to true) | If true, removes class attribute if encountered. Good for Bootstrap Icons which unnecessarily include the svg name in the class. |false
| removeStyle | | true (typically can be safely set to true) | If true, removes style/font-family attribute if encountered. Good for lower quality false | true | If true, then deletes
Based off the above, your parameters should typically be {} or {includeWidthAndHeightAttributes: false} or something like {includeWidthAndHeightAttributes: false, removeClass: true, removeStyle: true, removeDeprecated: true, setColor: 'currentColor', setColorIssue: 'fail'}`.
See example-config.js config.
Then run with svgo;
> svgo --input 'input.svg' --output 'output.svg' --config 'example-config.js'
See index.js and AutocropUtils.js for starting points.