Font icons like a boss with Sass & Font Custom

Updated

Overview

Here we take "Fun with Sass & font icons" to the next level by automating the tedious stuff, including the actual font generation.

Manage all your project's font icons in one folder as nicely named SVG files. Add icons to this folder to have your fonts magically rebuilt and your Sass automatically set up for you to start using the icons via their nice names; without touching your markup or dealing with non-semantic class names.

What are the perks?

Requirements

You will need to install:

  1. The Font Custom ruby gem. Installation instructions are available on Github. a couple of dependencies including FontForge will also be installed.
  2. Sass ruby gem version 3.3+, we need the recently added SassScript maps feature.

Setup

1. Setup the file structure

Create the following folders and files:

2. Prepare the fontcustom.yml config file

This file contains:

# Font Custom config: http://jaydenseric.com/blog/font-icons-like-a-boss-with-sass-and-font-custom
font_name: icons
no_hash: true
input:
  vectors: vectors
  templates: templates
templates: [_font-icons.scss, _icon-font.scss]
output:
  fonts: fonts
  _font-icons.scss: ../scss
  _icon-font.scss: ../scss

The paths are relative to the config file's location. Tweaking this structure may give you headaches until a particular bug is fixed.

3. Prepare the _font-icons.scss template

/fontcustom/_font-icons.scss is not a Sass file. Font Custom will use this template to create a Sass file in the location configured (/scss/_font-icons.scss) each compile.

$font-icons: (<% @glyphs.each do |name, value| %>
    <%= name.to_s %>: "\<%= value[:codepoint].to_s(16) %>",<% end %>
);

4. Prepare the _icon-font.scss template

Again, this is a template; not yet an actual Sass file.

@font-face {
    font-family: Icons;
    src: url("data:application/x-font-woff;charset=utf-8;base64,<%= woff_base64 %>") format("woff");
    font-weight: normal;
    font-style: normal;
}

5. Prepare your main Sass file

In /scss/styles.scss, allow easy use of font icons in your main stylesheet by importing the two Sass partials, along with a nifty mixin:

@import 'font-icons';
@import 'icon-font';

@mixin icon($position: before, $icon: false, $styles: true) {
    @if $position == both {
        $position: 'before, &:after';
    }
    &:#{$position} {
        @if $icon {
            content: "#{map-get($font-icons, $icon)}";
        }
        @if $styles {
            speak: none;
            font-style: normal;
            font-weight: normal;
            font-family: Icons;
        }
        @content;
    }
}

// The rest of your sexy styles here...

Usage

1. Add new icons to the project

To add a new icon, save it to /fontcustom/vectors as an SVG named what you want to call it by in your Sass.

I highly recommend square 32 × 32px SVGs with an accuracy of at least 3 decimal places.

The IcoMoon web app is the most convenient way to get new icons. Search from a huge range of great collections, select the ones you like and click the SVG download button. This will give you a nice zip of SVG files ready for you to drop into your project.

It's always a good idea to optimize SVG with a tool like SVGO before use.

Here is what the project looks like with a few new icons (highlighted):

2. Run Font Custom

You must run Font Custom whenever you update your icons. In Terminal, navigate to the /fontcustom folder and run fontcustom compile. If you are doing a lot of work on your icons use fontcustom watch instead to automatically recompile any time a change is detected within the /fontcustom folder. watch seems to be a bit broken these days, so I tend not to use it much.

Our project now, highlighting the compiled files and folders:

Note the new /fontcustom/fonts folder. We don't need it since we are embedding the font in the CSS as base64 WOFF. Unfortunately, Font Custom always creates these files. You can safely delete the folder, or if you use Git just add it to your .gitignore file like I do:

fontcustom/fonts
.fontcustom-manifest.json # Ignore this temporary file too

3. Use the icons in the Sass

You are now ready to use the new icons throughout your Sass. Please refer to the "Usage" section of my detailed article about the icon Sass mixin for code examples.

IE8 support

To tailor the same workflow for IE8 support, skip the _icon-font.scss template and use a standard @font-face declaration referencing the actual font files Font Custom generates instead. You can see this legacy setup in the Barebones ie8-support branch on Github.