Code Monkey home page Code Monkey logo

postcss-inline-svg's Introduction

postcss-inline-svg Build Status

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.path

Path to resolve url.

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.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');
}

options.transform(svg, path)

Transforms svg after encode function and generates url.

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

postcss-inline-svg's People

Contributors

fabiocaccamo avatar greenkeeperio-bot avatar trysound avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.