Skip to main content

Collapse Groups

Default

Finds groups that effectively do nothing and flattens them, preserving the contents of the groups.

Groups can be formed using the <g> element. They're used for organizing the document, or applying presentation attributes to all children contained in a group.

info

This plugin doesn't remove empty groups. That's handled by the Remove Empty Containers plugin which is enabled by default.

Usage

svgo.config.js
module.exports = {
plugins: [
"collapseGroups"
]
}

Demo

Live Editor
const svg = `
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox=" 0 0  150 100 " width="150">
  <!-- Created with love! -->
  <defs>
    <ellipse cx="50" cy="50.0" rx="50.00" ry="auto" fill="black" id="circle"/>
  </defs>
  <g>
    <use href="#circle" transform="skewX(16)"/>
    <rect id="useless" width="0" height="0" fill="#ff0000"/>
  </g>
</svg>
`;

const svgoConfig = {
  js2svg: { indent: 2, pretty: true },
  plugins: [
    "collapseGroups"
  ]
}

render(<SvgoPreview svg={svg} svgoConfig={svgoConfig}/>);
Result
Loading...

Implementation