Skip to content

Sort Attributes Default

Sorts attributes in all elements in the document. This does not reduce the size of the SVG, but improves readability and may improve how compression algorithms perform on it.

Here is a demonstration of SVGs that have been gzipped by NGINX using the default compression level of 1.

SVGUnsorted ¹Sorted ²Reduced (%)
Arch Linux Logo2.61 kB2.61 kB0 kB (0%)
Blobs13.89 kB13.88 kB0.01 kB (0.1%)
Isometric Madness123.87 kB120.09 kB3.78 kB (3.1%)
tldr-pages Banner791 B786 B5 B (0.6%)
Wikipedia Logo53.96 kB53.87 kB0.09 kB (0.2%)

¹ Uses the default plugins preset excluding sortAttr and sortDefsChildren.
² Uses the default plugins preset as-is.

Usage

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

Parameters

orderAn array of attribute keys to order by. Attributes not found in the array are sorted alphabetically.
xmlnsOrderSet to 'front' if XML namespaces should be placed infront of all other attributes.
Usage example
js
module.exports = {
  plugins: [
    {
      name: "sortAttrs",
      params: {
        order: ["id","width","height","x","x1","x2","y","y1","y2","cx","cy","r","fill","stroke","marker","d","points"],
        xmlnsOrder: "front"
      }
    }
  ]
}
module.exports = {
  plugins: [
    {
      name: "sortAttrs",
      params: {
        order: ["id","width","height","x","x1","x2","y","y1","y2","cx","cy","r","fill","stroke","marker","d","points"],
        xmlnsOrder: "front"
      }
    }
  ]
}

Demo

Implementation

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