Cleaning up dirty SVG code generated by design tools
Optimize your SVG files for better performance and readability
Introduction
SVG (Scalable Vector Graphics) has become a popular format for graphics on the web due to its scalability and flexibility. However, when using design tools like Adobe Illustrator, Sketch, or Figma to create SVG files, the resulting code can be bloated and inefficient. In this article, we will explore the importance of cleaning up dirty SVG code and provide practical tips on how to do it.
Why clean up SVG code?
Dirty SVG code can lead to several issues, including:
Increased file size, resulting in slower page loading times
Reduced readability and maintainability of the code
Incompatibility with certain browsers or devices
Difficulty in editing or modifying the graphics
Common issues with design tool-generated SVG code
Design tools often generate SVG code that includes unnecessary elements, attributes, and styles. Some common issues include:
Unused IDs and classes: Design tools may assign unique IDs and classes to elements, even if they are not used in the SVG file.
Inline styles: Design tools may use inline styles instead of external stylesheets, resulting in duplicated code and reduced maintainability.
Transforms and matrices: Design tools may use complex transforms and matrices to position and rotate elements, resulting in bloated code.
Embedded fonts and images: Design tools may embed fonts and images directly into the SVG file, increasing its size.
Cleaning up SVG code
To clean up dirty SVG code, follow these steps:
1. Simplify paths and shapes: Use tools like SVGO or SVGOMG to simplify complex paths and shapes, reducing the number of nodes and curves.
2. Remove unnecessary elements and attributes: Use a text editor or an SVG editor like Inkscape to manually remove unused elements and attributes.
3. Optimize inline styles: Use an external stylesheet to define styles, and remove inline styles from the SVG code.
4. Use relative units: Use relative units like em or rem instead of absolute units like px to make the SVG file more flexible.
5. Compress the SVG file: Use tools like Gzip or Brotli to compress the SVG file, reducing its size and improving page loading times.
Example: Cleaning up an SVG file generated by Adobe Illustrator
Suppose we have an SVG file generated by Adobe Illustrator, containing a simple logo with a rectangle and a circle. The original code looks like this:
svg
<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'>
<g id='logo' transform='matrix(1 0 0 1 0 0)'>
<rect x='10' y='10' width='80' height='80' style='fill:FFFFFF;stroke:000000;stroke-width:2'/>
<circle cx='50' cy='50' r='40' style='fill:FFFFFF;stroke:000000;stroke-width:2'/>
</g>
</svg>
After cleaning up the code, it looks like this:
svg
<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'>
<rect x='10' y='10' width='80' height='80' fill='FFFFFF' stroke='000000' stroke-width='2'/>
<circle cx='50' cy='50' r='40' fill='FFFFFF' stroke='000000' stroke-width='2'/>
</svg>
In this example, we removed the unnecessary g element, id attribute, and transform attribute. We also simplified the styles and removed the inline styles.
Tools for cleaning up SVG code
Several tools are available to help clean up SVG code, including:
SVGO: A popular online tool for optimizing and cleaning up SVG code.
SVGOMG: A tool for optimizing and cleaning up SVG code, with a simple and intuitive interface.
Inkscape: A free and open-source vector graphics editor that can be used to manually clean up SVG code.
Gzip: A tool for compressing SVG files, reducing their size and improving page loading times.
Conclusion
Cleaning up dirty SVG code generated by design tools is essential for optimizing SVG files for better performance and readability. By following the tips and using the tools outlined in this article, designers and developers can create efficient, scalable, and maintainable SVG graphics for the web.