Inline SVG for Sass.
Switch branches/tags
Clone or download
davidkpiano Merge pull request #15 from ColinEberhardt/patch-1
docs: indicate that sassdash needs to be imported
Latest commit d50e753 Jun 28, 2018


Inline SVG for Sass.

(Made using Sassdash!)

Quick Start

  • npm install sass-svg --save
// Will likely be ../node_modules/sassdash/index
@import 'path/to/sassdash';

// Will likely be ../node_modules/sass-svg/index
@import 'path/to/sass-svg';

.arrow {
  @include svg {
    @include svg('polygon', (
      fill: green,
      points: (50,100 0,0 0,100)


.arrow {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns

Sassier Example

This shows how you can use nesting and variables to create complex SVG shapes.

@import 'path/to/sass-svg';

$sass-color: #CC6699;

.sass {
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;

  @include svg((
    x: 0px,
    y: 0px,
    viewBox: 0 0 410.9 410.9,
    'xml:space': preserve
  )) {
    @include svg('path', (
      fill-rule: evenodd,
      clip-rule: evenodd,
      fill: $sass-color,
      d: 'M205.4,0c113.5,0,205.4,92,205.4,205.4c0,113.5-92,205.4-205.4,205.4C92,410.9,0,318.9,0,205.4 C0,92,92,0,205.4,0L205.4,0z'

    @include svg('g') {
      @include svg('path', (
        fill: white,
        d: 'M334.3,87.9c-9.3-36.5-69.8-48.5-127.1-28.1c-34.1,12.1-71,31.1-97.5,55.9c-31.5,29.5-36.6,55.2-34.5,65.9 c7.3,37.9,59.2,62.6,80.5,81v0.1c-6.3,3.1-52.3,26.4-63.1,50.2c-11.4,25.1,1.8,43.1,10.5,45.6c27,7.5,54.7-6,69.6-28.2
        c-2.1,1.2-0.8,4.5,0,6.5c2,5.1,10,14.2,23.8,18.7c12.1,4,41.5,6.2,77.2-7.6C312.3,166.7,343.4,123.8,334.3,87.9z M164.6,273.9


Sass logo


The API is simple. There's one mixin: @include svg($type, $attrs).

  • $type (String) is the SVG element type; e.g. for <path />, it would be $type: 'path'.
  • $attrs (Map) are all the attributes for the SVG element, such as $attrs: (fill: white).

For SVG elements with text inside, such as <text>Hello, world</text>, use the (non-standard) content attribute. E.g. @include svg('text', (content: 'Hello, world'));

SVG elements can be (infinitely) nested, as well:

@include svg {
  @include svg('g', (...)) {
    @include svg('path', (...));
    @include svg('rect', (...));

Always remember to include the root <svg> element! You can simply:

  • @include svg { ... } (which defaults to type: 'svg'), and/or...
  • provide just attributes: @include svg((x: 0, y: 0, ...)) { ... } (which also defaults to type: 'svg').

Warnings and Notes

Please note: use this system with caution. While it provides for easy organization, access and editing of SVG within your stylesheets, it can cause performance delays in production due to the data URI encoding of the SVG within the CSS itself. You can read more about this issue here.

The more complex the SVG, the more code is converted to CSS, thus bloating your output stylesheet. To prevent issues, only use small, non-complex SVG assets and be cognizant of your CSS output.