An SVGO plugin to add
viewBoxattribute based onwidthandheightattributes
This plugin adds the viewBox attribute to your SVGs based on the width and height attributes. The difference between this plugin and the built-in removeDimensions plugin is that svgo-add-viewbox does not remove the width and height of your SVGs.
In order to use this plugin correctly, your SVGs should have their width and height attributes specified.
v2.x.x or 3.x.x of this plugin should be used with SVGO v3 and above.
v1.x.x of this plugin should be used with SVGO v2.
- Create a
svgo.config.jsfile following the official configuration guide - Use the option to specify a custom plugin.
- Install this module from NPM
npm install svgo-add-viewbox --save-dev
# or
yarn add svgo-add-viewbox -Dimportthe module in yoursvgo.config.jsfile:
import addViewBox from 'svgo-add-viewbox';- In the
pluginsarray in yoursvgo.config.jsfile add the following:
plugins: [
// ... more plugins
addViewBox
// ... more plugins
];- Arguments
overwrite- boolean - defaults totrue- If set tofalsewill preserve any existingviewBoxattribute on your input SVG files.
Usage:
plugins: [
// ... more plugins
{
...addViewBox,
params: {
overwrite: false
}
}
// ... more plugins
];- Execute your SVG transformation NPM script.
MIT