Skip to content

Repository files navigation

svg-strip

Built with Rust License: MIT Platform

Fast and aggressive SVG minification CLI tool for shrinking SVGs for inline HTML, Vue and Svelte component templates, and standalone files.

demo

Features

  • Extreme Minification (automatic): Removes title, desc, metadata, comments, unused ids, hidden elements (display="none", opacity="0"), empty groups, and the <?xml...?> declaration, collapsing all whitespace and linebreaks into a single-line string.
  • Color Shrink (automatic): Identifies 6-digit hex color codes with identical byte pairs (e.g., #FF0000, #aabbcc) across all styling attributes (fill, stroke, etc.) and losslessly converts them into their 3-digit shorthands (e.g., #f00, #abc). Leaves non-matching hex codes safely untouched.
  • Inline Optimization (-i / --inline): Strips the xmlns attributes which are unnecessary overhead for browsers when embedding SVGs directly into HTML5 code.
  • Component Mode (-c / --component): Produces paste-ready standard SVG markup for HTML, Vue, and Svelte templates. It removes namespaces and fixed root dimensions, safely converts simple embedded class rules to SVG presentation attributes, and preserves the viewBox.
  • Icon Mode (--icon SIZE / --icon WIDTHxHEIGHT): Includes Component Mode and adds a fixed pixel size plus fill: currentColor on the root SVG.
  • Decimal Precision (-dp / --decimal-precision): Aggressively rounds all path coordinates and attributes (like viewBox, x, y, transform) down to a user-specified number of decimal places (0-4), stripping trailing zeros.

Usage

svg-strip [OPTIONS] <input.svg> [output.svg]

If no output.svg is specified, the tool will automatically save the minified file in the same directory as [ORIGINAL_NAME]_stripped.svg.

Options

Flag Name Description
-i, --inline Inline Mode Strips xmlns attributes for optimal inline HTML usage.
-c, --component Component Mode Inlines supported class styles and removes component-unsafe SVG overhead.
--icon <SIZE|WIDTHxHEIGHT> Icon Mode Produces a square or explicitly sized component-ready icon with currentColor fill.
-o, --output Stdout Mode Prints the minified SVG directly to the terminal (stdout) instead of writing to a file.
-dp, --decimal-precision <0-4> Decimal Precision Rounds all floating point numbers inside paths and attributes to the specified number of decimal places to save bytes.

Component mode

Convert an exported SVG with an embedded class stylesheet:

<svg xmlns="http://www.w3.org/2000/svg" width="800px" height="800px" viewBox="0 0 24 24">
  <style>.st0 { fill-rule: evenodd; clip-rule: evenodd; }</style>
  <path class="st0" d="M0 0h24v24z" />
</svg>
svg-strip -c -o exported-icon.svg

The serialized output is deliberately one line; formatted semantically, it is:

<svg viewBox="0 0 24 24">
  <path fill-rule="evenodd" clip-rule="evenodd" d="M0 0h24v24z" />
</svg>

Component Mode supports simple class selectors such as .st0 and .st0, .st1. It rejects complex selectors, at-rules, unsupported properties, and !important instead of retaining a <style> element or silently changing its meaning. The root SVG must have a non-empty viewBox; Component Mode will fail rather than remove fixed dimensions from an SVG that cannot be scaled safely.

Icon mode

Set the rendered size while allowing CSS color to control inherited fills:

svg-strip --icon 20 exported-icon.svg
svg-strip --icon 20x20 exported-icon.svg

A single positive, unitless integer or decimal sets both dimensions. Use two values separated by x or X for a non-square icon, such as 20.5X16. Zero, explicit units, exponents, and spaces are rejected.

Icon Mode adds this root style after applying all Component Mode transformations:

style="width: 20px; height: 20px; overflow: hidden; fill: currentColor"

For a shared icon color, define an appropriate global rule:

svg {
  color: var(--your-icon-color);
}

Inline minification

Minify an icon for use in a web template, stripping all namespace overhead and rounding coordinates to 2 decimal places:

svg-strip -i -dp 2 raw_icon.svg

Output:

Stripped SVG written to raw_icon_stripped.svg
• Inline SVG with zero overhead
• Decimal Precision for paths rounded down to 2 decimals
• Color Shrink to convert 6-digit hex codes to 3-digit shorthands

Installation

Local user install and update script

Linux / macOS:

curl -fsSL https://raw.githubusercontent.com/madLinux7/svg-strip/main/install.sh | sh

Installs to ~/.local/bin by default. Override this with INSTALL_DIR=/custom/path.

Windows (PowerShell):

irm https://raw.githubusercontent.com/madLinux7/svg-strip/main/install.ps1 | iex

Installs to %LOCALAPPDATA%\svg-strip and adds it to your user PATH. Restart the terminal before using svg-strip.

From GitHub Releases

Download the latest binary for your platform from Releases and place it in your PATH.

# Example for Linux x86_64
curl -fL https://github.com/madLinux7/svg-strip/releases/latest/download/svg-strip-linux-x86_64 -o svg-strip
chmod +x svg-strip
sudo mv svg-strip /usr/local/bin/

From source

You must have Rust and Cargo installed. Clone the repository and build the project:

git clone https://github.com/madLinux7/svg-strip.git
cd svg-strip
make build

Optimized local build

For an optimized, UPX-compressed binary, install UPX and run:

make release

The release binary will be available at target/release/svg-strip. Override RELEASE_BINARY when targeting a different executable path.

About

Strip SVG overhead and metadata. Dead-simple, cross-platform, no dependencies.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages