Load the stylesheet of the icon font and draw an icon with a class. Installation, size and color, accessibility and the folder of the font.
Introduction
The icon font draws an icon with a class on an empty element. One stylesheet loads the font and holds a class per icon, so a page needs no markup beyond that element. Choose the font for icons inside text and controls, where an icon is sized and colored as a letter is. Choose the sprite when an icon needs the features of SVG, such as a title of its own.
The examples use @acme/glyphs, the set of the team Acme, whose font is acme-glyphs and whose prefix is ag. For another set, use the name of its package, its font and its prefix in their place.
Installation
The files of the font are in the dist/web/ folder of the output. There are three ways to get that folder.
The package
A set that its team publishes is a package, which holds dist/ as the build wrote it:
npm install @acme/glyphsThe archive of a release
Each release of a repository of icons on GitHub has an archive, <font>-web-<version>.zip, with the folders web/ and svgs/, the index icons.json and the page of the set, preview/, in it. Download it from the releases of the repository of the set, and unpack it into the folder that your project serves static files from.
The folders of a build
A set of your own is the output of your repository of icons after npm run build: the folders web/ and svgs/ of its dist/. Copy the two folders as they are, from the root of the repository:
cp -r dist/web dist/svgs /path/to/your-project/static/icons/The stylesheet loads the font from its own folder, so keep the files of web/ together.
Basic usage
Load the stylesheet, and put the class of an icon, <prefix>-<name>, on an empty element:
<link rel="stylesheet" href="node_modules/@acme/glyphs/dist/web/acme-glyphs.min.css" />
<i class="ag-ring-line"></i>dist/web/<font>.css and dist/web/<font>.min.css hold the same rules. With a bundler, import the package, which loads the same stylesheet and its font:
import '@acme/glyphs'The stylesheet has one rule per icon, .<prefix>-<name>::before, whose content is the character of the icon in the font. The element itself stays empty.
Size and color
An icon of the font is a character, so it takes the font-size and the color of its element:
<i class="ag-ring-line" style="font-size: 2rem; color: rebeccapurple;"></i>Inside a heading, a link or a button the icon needs neither: it has the size and the color of the text around it. The stylesheet sets line-height: 1 and a vertical-align on the ::before of the element, so that an icon sits on the line of the text.
Accessibility
A screen reader does not know what the character of an icon means. Hide an icon that repeats the text beside it, and name one that stands alone.
A decorative icon is hidden:
<button type="button"><i class="ag-ring-line" aria-hidden="true"></i> Notifications</button>An icon that carries meaning gets a role and a label:
<i class="ag-ring-line" role="img" aria-label="Notifications"></i>Names in the font
A set built with "ligatures": true in chassis.build draws an icon for its name too. Put the name in an element of the class <prefix>:
<i class="ag" aria-hidden="true">ring-line</i>The class sets the font on the element and turns on its ligatures, so the font draws the characters of the name as one icon. The class of each icon and its code point stay as they are, and a page can use both.
The name is text. A screen reader reads it, so hide a decorative icon, and give an icon that carries meaning a role and a label, as with a class. While the font loads, the stylesheet hides the name for up to three seconds, and shows it after that until the font is there.
Customization
The stylesheet has no settings of its own. What it holds follows from the configuration of the set, and Sass sets where the font is loaded from.
The folder of the font
The CSS loads the font from the folder it is in. To serve the font from another folder, use the Sass file and set the folder there, as Sass describes.
The formats
The font is written in each format of chassis.build.formats: woff2, woff and ttf. The stylesheet lists them in the order of that setting, and a browser takes the first it can read.
A set of your own
The font name and the prefix are the settings name and prefix of chassis.build. With "name": "acme-glyphs" and "prefix": "ag" the stylesheet is dist/web/acme-glyphs.css, and the class of an icon is ag-<name>. With the settings of your set, the stylesheet and the classes take your names.
Best practices
The class of an icon is its name with the prefix, and the font is found beside the stylesheet. These practices keep both true in a project.
- Copy the whole
web/folder: the stylesheet asks for./<font>.woff2beside itself. - Hide a decorative icon: add
aria-hidden="true"to an icon that sits beside its text. - Take the name from the set:
dist/icons.jsonlists the name of each icon, and the page of the set thatchassis-icons previewwrites shows its class.
Avoid the habits that undo this:
- Do not start a class of your own with the prefix: the stylesheet sets the font on the
::beforeof every element whose class starts with<prefix>-, and withligatureson every element of the class<prefix>. - Do not write a code point into a page:
dist/web/<font>.jsonand the Sass map hold the code points, and a class or the map reads them by name.
Troubleshooting
Each entry gives the message or the symptom, then the cause and the fix.
Icon shows as a square
The page shows an empty square in place of the icon, and the browser logs a 404 for the .woff2 file. The stylesheet loads the font from its own folder, and the font was not copied with it. Copy the whole web/ folder, or set $<font>-font-dir in Sass.
Nothing is shown
The element is empty and the browser logs no error. The class is not a class of the stylesheet: the name is misspelled, or the icon is not in the version of the set that the page loads. Find the name in dist/icons.json of that version.
Name shows as text
The element shows the name of the icon, or nothing, in place of the icon. The set is built without ligatures, the element does not have the class <prefix>, or the name is not an icon of the set. Check chassis.build.ligatures of the set, and find the name in dist/icons.json.
Old icon after an update
A browser shows the icons of the earlier version. The address of the font ends in a hash of the font, so a changed font is asked for again, but the stylesheet itself is cached. Serve the stylesheet under a new address, or with a cache that revalidates.
Next steps
Continue with one of these pages:
- SVG sprite: draw the same icons as SVG.
- Sass: the folder of the font, and the map of the icons.
- Chassis CSS: the class
iconand its utilities.