Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

postcss-inline-svg

TrySound235.2kMIT6.0.0TypeScript support: definitely-typed

PostCSS plugin to reference an SVG file and control its attributes with CSS syntax

postcss, css, postcss-plugin, svg, inline

readme

postcss-inline-svg CI

PostCSS plugin to reference an SVG file and control its attributes with CSS syntax.

@svg-load nav url(img/nav.svg) {
    fill: #cfc;
    path:nth-child(2) {
        fill: #ff0;
    }
}
.nav {
    background: svg-inline(nav);
}
.up {
    background: svg-load('img/arrow-up.svg', fill=#000, stroke=#fff);
}
.nav {
    background: url("data:image/svg+xml;charset=utf-8,%3Csvg fill='%23cfc'%3E%3Cpath d='...'/%3E%3Cpath d='...' fill='%23ff0'/%3E%3Cpath d='...'/%3E%3C/svg%3E");
}
.up {
    background: url("data:image/svg+xml;charset=utf-8,%3Csvg fill='%23000' stroke='%23fff'%3E...%3C/svg%3E");
}

PostCSS parsers allow to use different syntax (but only one syntax in one svg-load() definition):

.up {
    background: svg-load('img/arrow-up.svg', fill: #000, stroke: #fff);
}
.down {
    background: svg-load('img/arrow-down.svg', fill=#000, stroke=#fff);
}

Usage

postcss([ require('postcss-inline-svg')(options) ])

See PostCSS docs for examples for your environment.

Options

options.paths

Array of paths to resolve URL. Paths are tried in order, until an existing file is found.

Default: false - path will be relative to source file if it was specified.

options.removeFill

Default: false - with true removes all fill attributes before applying specified. Passed RegExp filters files by ID.

options.removeStroke

Default: false - with true removes all stroke attributes before applying specified. Passed RegExp filters files by ID.

options.encode(svg)

Processes SVG after applying parameters. Default will be ommited if passed false.

Default:

function encode(code) {
    return code
        .replace(/%/g, '%25')
        .replace(/</g, '%3C')
        .replace(/>/g, '%3E')
        .replace(/&/g, '%26')
        .replace(/#/g, "%23")
        .replace(/{/g, "%7B")
        .replace(/}/g, "%7D");
}

options.transform(svg, path)

Transforms SVG after encode function and generates URL.

options.xmlns

type: boolean default: true

Adds xmlns attribute to SVG if not present.

Frequently asked questions

Why svg-load() doesn't work and the color still black (or red or whatever)?

That's because svg-load() overrides attributes only in <svg> element and children inherit that color. But if there is already color on children nothing will be inherited.

For example

<svg>
    <path fill="#ff0000" d="..." />
</svg>

after inline-svg (fill: #000) will looks like

<svg fill="#000">
    <path fill="#ff0000" d="..." />
</svg>

There are three solutions: to remove that attribute (preferable), to use extended @svg-load notation or to use removeFill option.

How to optimize svg on build step?

There is a plugin. :)

You are able to add postcss-svgo in your PostCSS plugins list which will detect every URL which contains data SVG URI and minify via svgo.

postcss([
    require('postcss-inline-svg'),
    require('postcss-svgo')
])

Or if you use cssnano your SVG URLs already minified as cssnano includes postcss-svgo.

postcss([
    require('postcss-inline-svg'),
    require('cssnano')
])

License

MIT © Bogdan Chadkin

Support on Beerpay

Hey dude! Help me out for a couple of :beers:!

Beerpay Beerpay

changelog

Changelog

6.0.0

  • Dropped Node.js 10 and 12 support.
  • Fixed security vulnerabilities in dependencies.

5.0.0

  • Migrated to PostCSS 8.
  • Dropped Node.js 8 support.

4.1.0

  • Added removeStroke option.

4.0.0

  • Option path was replaced with paths. Allows to specify multiple paths for URL resolve.
  • Default encode function now escapes curly brackets.
  • Dropped Node.js 6 support. Node.js 8.7.0 or greater is now required.
  • Upgraded to PostCSS 7.

3.1.0

  • Added xmlns option to add xmlns attribute if not present

3.0.0

  • Upgrade to postcss 6

2.3.1

  • Warn more descriptive messages

2.3.0

  • Add parent prop in dependency message

2.2.0

  • Add dependency message for bundlers

2.1.2

  • Allow color functions in short syntax

2.1.1

  • Customize ENOENT error
  • Codebase refactoring

2.1.0

  • Add removeFill option to force color in pure icons

2.0.1

  • Stringify decl values

2.0.0

  • Code and tests refactoring
  • encode is not included in transform and runs before it
  • encode is function or false
  • transform can't return false
  • Fix bug with quotes #19

1.4.0

  • Support "=" separator in svg-load() definition

1.3.2

  • Fix URI malformed error

1.3.1

  • Add read-cache

1.3.0

  • Refactoring
  • Loaded files will be cached

1.2.1

  • Fix relative path detecting

1.2.0

  • Add transform option

1.1.1

  • Improve documentation

1.1.0

  • Fix ie strict data uri by adding custom url encode
  • Add encode option

1.0.0

  • Initial release