How import Font Awesome in LESS

Using Font Awesome with LESS

This is a short but useful post about working with LESS. If you haven’t tried it yet, LESS is a powerful CSS preprocessor that can save you a lot of time when writing styles. One of the first questions that comes up when starting a LESS-based project is how to include Bootstrap or Font Awesome. Bootstrap v3 has good documentation covering its use with LESS and Sass. Font Awesome is slightly trickier: the standard @import alone compiles, but the icons do not show up.

Problem: After including Font Awesome 4 in a LESS project with a plain @import, the icons are empty squares. The browser cannot find the font files, because the font path in the compiled CSS is wrong.

Solution: override @fa-font-path after the import

Font Awesome builds its @font-face URLs from the @fa-font-path variable, which defaults to "../fonts". That path is relative to the compiled CSS file, not to the LESS file, so it is usually wrong in your project. Set your own value right after the import:

/* Import Font Awesome */
@import "your-path/font-awesome/less/font-awesome.less";

/* Path to the Font Awesome font files, relative to the compiled CSS file */
@fa-font-path: "../fonts/font-awesome";

Why the variable goes after the import

LESS variables are lazy: the last definition in the scope wins, even if it comes after the place where the variable is used. So a definition placed after the @import overrides the default from Font Awesome’s variables.less. This is different from Sass, where you set a variable before the import with !default logic.

Then copy the fonts folder from Font Awesome to the place the path points to, for example css/../fonts/font-awesome/. The same approach works for Bootstrap v3, which uses the @icon-font-path variable for its Glyphicons font.