GithubHelp home page GithubHelp logo

electriccode / sass-inline-svg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from franzheidl/sass-inline-svg

0.0 2.0 0.0 129 KB

Inline url-encoded SVG with Sass. Optional variable string replacement included!

License: MIT License

Ruby 100.00%

sass-inline-svg's Introduction

Sass-Inline-Svg

Gem Version

Inline url-encoded SVG with Sass. Optional variable string replacement included!

Installation

gem install sass_inline_svg

Add this line to your application's Gemfile:

gem 'sass_inline_svg'

Usage

Sass-inline-svg adds a inline-svg function you can use with Sass. It url-encodes the contents of the specified file and inlines it in your CSS (Url-encoded SVG is about 30% smaller than base64).

###Basic

.my-thing {
    background-image: inline-svg('my-file.svg');
}

When working with plain Sass, you'll have to use the full path to the svg file, when using Rails the path will be resolved by the Rails asset pipeline.

The svg-inline() function as it was named in earlier versions is now aliased to inline-svg() so either of both can be used.

###Replace Variable Strings

Replacing variable strings in SVG when inlining them with Sass makes sense e.g. if you need multiple variants of the same graphic with different fill colors.

With Sass-Inline-Svg you only need one source svg file with a variable string for fill:

…
<polygon fill="fillcolor" points="29.43 25.19 20.24 16 29.43 6.81 25.19 2.57 16 11.76 6.81 2.57 2.57 6.81 11.76 16 2.57 25.19 6.81 29.44 16 20.24 25.19 29.44 "/>
…

The variants needed can be created during inlinig with Sass. Pass a Sass map of replacements as a second parameter:

.my-thing {
    background-image: inline-svg('my-file.svg', ( fillcolor: '#fff' ));
}

This will replace all occurences of fillcolor in your SVG with #fff:

…
<polygon fill="#fff" points="29.43 25.19 20.24 16 29.43 6.81 25.19 2.57 16 11.76 6.81 2.57 2.57 6.81 11.76 16 2.57 25.19 6.81 29.44 16 20.24 25.19 29.44 "/>
…

Note: If you use $- or @-prefixed variable names in your SVG, you'll have to quote the keys in your raplement Sass map:

.my-thing {
    background-image: inline-svg('my-file.svg', ( '$fillcolor': '#fff' ));
}

sass-inline-svg's People

Contributors

franzheidl avatar edda avatar

Watchers

James Cloos avatar Pankaj Phartiyal 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.