Skip to content

SVG

Recolour an inline SVG background image from Sass: a mixin that URL-encodes the colour as %23 so one logo works in any colour.

3 min readUpdated

An SVG used as a CSS background-image cannot be styled from the page: fill, stroke and currentColor set in your stylesheet do not reach inside it. To show the same logo or icon in several colours without one file per colour, you can embed the SVG as a data URL and let Sass write the colour into it at build time.

This note explains a Sass mixin that does exactly that, and the one encoding detail that makes it work.

Write the colour into an inline SVG

The mixin logo puts the whole SVG into the background-image as a data: URL and inserts the colour into its fill attribute. The helper function encodeColor turns a Sass colour into text that is safe inside the URL:

@function encodeColor($string) {
@if type-of($string) == 'color' {
$hex: str-slice(ie-hex-str($string), 4);
$string: unquote("#{$hex}");
}
$string: '%23' + $string;
@return $string;
}
@mixin logo($color) {
background-image: url('data:image/svg+xml;utf8,<svg width="22.385792mm" height="4.2337036mm" fill="#{encodeColor($color)}" viewBox="0 0 84.607716 16.0014" xmlns="http://www.w3.org/2000/svg"><path d="M 0,2 A 2,2 0 0 1 2,0 h 8 a 2,2 0 0 1 2,2 v 2 h 2 a 2,2 0 0 1 2,2 v 8 a 2,2 0 0 1 -2,2 H 6 A 2,2 0 0 1 4,14 V 12 H 2 A 2,2 0 0 1 0,10 Z M 12,4 H 5 A 1,1 0 0 0 4,5 v 7 h 7 a 1,1 0 0 0 1,-1 z" /><text style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:16px;line-height:1.25;font-family:Roboto;stroke-width:1.00001" x="19.771776" y="15.84515"><tspan x="19.771776" y="15.84515" style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:16px;font-family:Roboto;stroke-width:1.00001">Logo</tspan></text></svg>');
}
.branding {
@include logo(#f00);
width: 22.385792mm;
height: 4.2337036mm;
}

Why the colour is encoded as %23

In a URL, # starts the fragment identifier. If the SVG inside a data: URL contains fill="#ff0000", the browser treats everything from the # onwards as the fragment, the SVG markup is cut off at that point, and the image does not render. The # must therefore be written in its percent-encoded form, %23. The other characters in this SVG (<, >, =, spaces and the double quotes) are accepted by browsers in an unencoded SVG data URL, as long as the URL itself is wrapped in single quotes.

encodeColor produces that form in three steps:

  1. ie-hex-str($string) converts any Sass colour (#f00, red, rgb(255 0 0)) to the eight-digit #AARRGGBB format, for example #FFFF0000.
  2. str-slice(..., 4) keeps the string from the fourth character on, dropping # and the two alpha digits: FF0000.
  3. '%23' + $string adds the encoded #: %23FF0000.

If you pass a string instead of a colour, the function only adds the prefix, so pass the hex digits without #, for example logo('ff0000').

Use the mixin

Include the mixin with the colour you want, and give the element the size of the image:

.branding {
@include logo(#f00);
width: 22.385792mm;
height: 4.2337036mm;
}
.branding--inverse {
@include logo(white);
width: 22.385792mm;
height: 4.2337036mm;
}

To adapt the mixin to your own image:

  • Paste your SVG between utf8, and the closing '. Replace every single quote in it with a double quote, and remove any # (inline styles with colours, url(#id) references), or write it as %23.
  • Replace the fixed fill value on the root element with #{encodeColor($color)}. Remove fill attributes from the child elements, otherwise they override the inherited colour.
  • For an icon drawn with strokes, put the colour in stroke instead, or add a second parameter for it.

Limitations

  • ie-hex-str drops the transparency: the alpha digits are removed, so a semi-transparent colour becomes opaque.
  • The colour is fixed at build time. Each colour is a separate copy of the SVG in the compiled CSS, and the image cannot follow currentColor or a CSS custom property at run time.
  • Current Dart Sass deprecates the global functions used here in favour of modules: meta.type-of, string.slice, string.unquote and color.ie-hex-str after @use "sass:meta", @use "sass:string" and @use "sass:color". The global names still compile but print deprecation warnings.
  • Text inside the SVG, such as “Logo” here, is drawn with whatever fonts the system has; a web font loaded by the page is not available inside a background image.

References

Related notes

13 min read

Frontend Checklist

A pre-launch checklist for a landing website: meta tags, HTML, CSS, JavaScript, media, performance, SEO, accessibility, security and Hugo builds.

  • Web & Frontend
13 min read

Jakarta Faces (JSF)

Jakarta Faces (JSF) recipes: WebSocket push, validators with injection, resource handling, component binding, java.time and scopes.

  • Java & Jakarta EE
  • Web & Frontend
2 min read

Web Components

The lifecycle of a custom element: when the browser calls constructor, connectedCallback, attributeChangedCallback, adoptedCallback and disconnectedCallback.

  • Web & Frontend

Type to search the whole site.