Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SVG Icons with mithril.js

Icons have been part of applications since ages. Also most websites rely on icons. There were several ways to use them. First we used plain files then image sprites to reduce requests. Nowadays everyone uses icon fonts like font-awesome or glyphicons.

They are infinetly scaleable and styleable with css. The downside is they use pseudo elements for displaying. This is not only difficult to handle but also non-optimal for accessibilty.

A famous CSS-Tricks post brings SVG icons into play. The are also scalable and they behave like normal images. But we also want to have a sprite to not load any images seperatly and kill our servers and our sites performance. The proposed version is to create sprites with grunt or gulp using the symbol-trick. It's basically add every icon to a hidden sprite-image and give every icon an id-property.

<svg xmlns="" style="display: none;">
  <symbol id="beaker" viewBox="214.7 0 182.6 792">
    <!-- <path>s and whatever other shapes in here -->  
  <symbol id="shape-icon-2" viewBox="0 26 100 48">
    <!-- <path>s and whatever other shapes in here -->  

Then you can do this

<svg class="icon">
  <use xlink:href="#shape-icon-1" />

This is a pretty clever idea if you don't have a framework like mithril. You still have the burden to create svg-sprites and you have to have on hidden element that holds the icons (which is no really problem, but verbose)

If you use browserify and mithril (what I strongly entourage you to do) there is an even simpler way. Browserify lets you include any file you want and base64 encodes them into the build. So first put your icons in a directory of your choice. Then you create a module called icons.js:

var fs = require('fs');
var m = require('mithril');

function icon(svg) {
  return m('.icon',;

module.exports = {
  icon0: icon(fs.readFileSync('directory_of_your_choice/icon0.svg', 'utf8')),
  icon1: icon(fs.readFileSync('directory_of_your_choice/icon1.svg', 'utf8')),
  icon2: icon(fs.readFileSync('directory_of_your_choice/icon2.svg', 'utf8'))

Browserify spots these files are read with fs.readFileSync loads them as Base64-encoded strings. The module then creates a div with class icon and embeds the svg into that div. The function prevents the svg from beeing html-encoded.

Now you have a module with all your icons in it. To use them simply embed them into your view functions:

var icons = require('./icons');

function myView(scope) {
  return m('.content', [
    'My fancy icon div'

The output then looks something like this

<div class="icon">
    <!-- <path>s and whatever other shapes in here -->

Pay attention to use the brfs-transform along with browserify. This embeds the readFileSync-ed files.

browserify -t brfs index.js

The advantages of this usage are:

  • No building svg sprites
  • Advantages of SVG over Fonts (better handling and styling)
  • One less file, since the icons are embedded in your javascript file
  • Easy to maintain
  • Minimal code
  • Pretty output
  • Mithril-way of doing things :)

This comment has been minimized.

Copy link

limdauto commented Dec 1, 2015

👍 I'm gonna try this tomorrow. This is very clever.


This comment has been minimized.

Copy link

ArthurClemens commented Dec 1, 2015

mmsvg uses the same method to embed SVG files, but generates js files for easy (ad hoc) inclusion.


This comment has been minimized.

Copy link
Owner Author

StephanHoyer commented Jun 14, 2016

Added encoding parameter to readFileSync so that browserify don't have to encode this to base64 strings


This comment has been minimized.

Copy link

connelevalsam commented Nov 13, 2017

i have this:

<div class="bg-white tc pv2">
    <svg version="1.1" width="74px" height="74px" xmlns="" xmlns:xlink="" x="0px" y="0px" viewBox="0 0 612 612" style="enable-background:new 0 0 612 612 fill:green; padding: 20px;" xml:space="
    <path d="M21.474,377.522V117.138c0-14.469,11.729-26.199,26.199-26.199h260.25c14.469,0,26.198,11.73,26.198,26.199v260.385
	c0,4.823-3.909,8.733-8.733,8.733H30.207C25.383,386.256,21.474,382.346,21.474,377.522z M231.634,466.724 
									C207.305,412.386,231.634,436.713,231.634,466.724z M204.464,466.724c0-15.005-12.164-27.169-27.169-27.169
									s-27.17,12.164-27.17,27.169s12.165,27.17,27.17,27.17S204.464,481.729,204.464,466.724z M130.495,412.385H8.733
									c0-30.011,24.328-54.338,54.338-54.338C491.609,412.385,515.938,436.713,515.938,466.724z M488.77,466.724
									S488.77,481.729,488.77,466.724z M612,421.118v26.495c0,4.823-3.91,8.733-8.733,8.733h-70.704
									C608.09,412.385,612,416.295,612,421.118z M523.408,256.635l-42.501-60.393c-1.636-2.324-4.3-3.707-7.142-3.707H407.47
<div class="ws-normal ph2">
	<span class="measure mw3">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodtempor incididunt ut</span>

and it's not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.