Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include only part of font-awesome

I need font-awesome to have spinner icons.

I could include the whole font-awesome library. But wouldn't that be an overkill for just one icon? Is there a way to include just one icon or component? The problem seems to be, that the library is not modularised to different files. For example, if using scss, all icons are in _icons.scss file, rather than each icon in their own file. If you use pure css, then everything is in one file.

like image 361
Ville Miekk-oja Avatar asked Jun 11 '16 19:06

Ville Miekk-oja


3 Answers

You can't. A font is a single file, much like an image or a document. It doesn't matter how you include it in your CSS -- users will still download the whole font file. The CSS definitions just make the font available on your web site.

There are some things that you could do as an alternative. There are some companies that will allow you to generate partial font sets using custom applications (like https://icomoon.io/, for example). That might suit your needs. But, once you create a custom version of their fonts it's still a file that you can't break up. Still, a custom version of icomoon can be very small and streamlined and would likely fit the scenario you describe.

Another alternative would be to not host the font yourself but use cloud-based fonts that are more likely to be cached by your users. It's not a solution per se but would increase the chances somewhat that your users wouldn't have to download the fonts specifically for your site.

like image 91
hightempo Avatar answered Oct 19 '22 03:10

hightempo


Font awesome v5 supports partial styles, in the getting-started page (https://fontawesome.com/v5.15/how-to-use/on-the-web/referencing-icons/basic-use), you could include this essential file first:

<link href=/your-path-to-fontawesome/css/fontawesome.css" rel="stylesheet">

and then, you could include one or some of those:

<link href="/your-path-to-fontawesome/css/brands.css" rel="stylesheet">
<link href="/your-path-to-fontawesome/css/regular.css" rel="stylesheet">
<link href="/your-path-to-fontawesome/css/solid.css" rel="stylesheet">
like image 5
MewX Avatar answered Oct 19 '22 01:10

MewX


Two years on after this question was asked, I would suggest you use SVGs rather than an icon font. You can concatenate all your SVGs into an SVG spritesheet, so they all get loaded (and cached) using a single HTTP request. The file will be much smaller than an entire icon font, which was your requirement.

Here are some reasons to pick SVGs over icon fonts (also see here):

  • You're including only the icons that you want, obviously.
  • SVG icons allow you to create multicolored icons.
  • Icons fonts are anti-aliased by the browser. SVGs are not, so they look sharper.
  • Font icons can be hard to position. SVGs are easy.
  • SVG icons can have baked-in titles and descriptions, which is good for accessibility.

To get high performance, you'll need to place all your SVG icons in a sprite sheet. You can do this using svgstore (grunt and gulp and webpack versions are available) so that it's part of your build process.

FontAwesome makes all its icons available as SVG files, so you can pick the ones you want and add them to your spritesheet build.

like image 2
Alexander van Oostenrijk Avatar answered Oct 19 '22 03:10

Alexander van Oostenrijk