Skip to content

Add Classes to Elements

Overrides the class attribute in the outer most <svg> element, omitting duplicates or null classes if found in your configuration.

WARNING

If there is no class attribute to begin with, it will be added. However, if the there were already classes assigned, these are removed and replaced with the classes configured in this plugin.

If you have a standalone SVG, this is not an optimization and will increase the size of the SVG document. This plugin is only relavent when considering multiple SVGs that may be inlined a parent document.

By adding classes, if the parent document is aware, you can share styles across all inlined SVG elements.

Usage

js
module.exports = {
  plugins: ["addClassesToSVGElement"],
};
module.exports = {
  plugins: ["addClassesToSVGElement"],
};

Parameters

classNamesAdds the specified class names to the outer most <svg> element.
classNameAdds the specified class name to the outer most <svg> element. If classNames is specified, this is ignored.
Usage example
js
module.exports = {
  plugins: [
    {
      name: "addClassesToSVGElement",
      params: {
        classNames: null
      }
    }
  ]
}
module.exports = {
  plugins: [
    {
      name: "addClassesToSVGElement",
      params: {
        classNames: null
      }
    }
  ]
}

Demo

Implementation

https://github.com/svg/svgo/blob/main/plugins/addClassesToSVGElement.js