Optimize Font Awesome for only used classes

2020-05-11 20:54发布

I am using Font Awesome Sass file https://github.com/FortAwesome/Font-Awesome/blob/master/sass/font-awesome.sass to make it _font-awesome.sass so I can @import in my Sass project. I am also using http://middlemanapp.com/ to convert Sass to Css. Questions:

  1. Is there a way to bring only used icon classes into my converted .css? Because right now it carried all classes from _font-awesome.sass

  2. BONUS: Is it possible to recompile the fonts somehow with used icon classes to make it smaller on production use?

If I can get some tips on #1 above, that would be awesome enough.

Thanks.

6条回答
叼着烟拽天下
2楼-- · 2020-05-11 21:33

Fontastic worked for me (it was listed on Font Awesome github page). Select glyphs that you need and download them as a new custom font. Excellent tool.

查看更多
Juvenile、少年°
3楼-- · 2020-05-11 21:34

fontello is very good but IcoMoon is even more awesome.

查看更多
戒情不戒烟
4楼-- · 2020-05-11 21:35

I use LESS and not SASS so you might have to adapt your implementation.

Environment:

  • Font awesome 4.5.0 (current version)
  • Ubuntu 14.04 LTS
  • bash

Use this to generate the list of Unicode character numbers that you need:

fa_icons="globe|vimeo|youtube|facebook|twitter|google-plus"
for code in $(egrep "^@fa-var-($fa_icons):" less/font-awesome/variables.less | cut -d ':' -f 2 | sed -e 's/^ "\\//' | sed -e 's/";/,/' | sort ); do echo -n $code; done

You then use this with FontSquirrel in the expert mode where you select custom subsetting: http://www.fontsquirrel.com/tools/webfont-generator

In Unicode ranges enter the comma separated values from above.

Then to remove unnecessary stuff from the CSS:

egrep "@fa-var-($fa_icons);" less/font-awesome/icons.less

You'll need to open less/font-awesome/icons.less and paste the output from the grep into the file.

查看更多
Bombasti
5楼-- · 2020-05-11 21:36

Well, the sass can certainly be jiggled a little to make the selectors % based so they are extendable only. Once this is done, classes can be made to match the wanted icons, and then can @extend the font-awesome classes.

Personally, I do this, and don't actually use the classes in the markup, and just use selectors to the relevant elements and @extend them with these classes.

Example:

// _icons.scss
%#{$fa-css-prefix}-glass:before { content: $fa-var-glass; }
...

// _core.scss
%#{$fa-css-prefix} {
    ...
}

Then in your scss

a.search {
    @extend %fa;
    @extend %fa-search;
}

Et voila.

查看更多
祖国的老花朵
6楼-- · 2020-05-11 21:39

Sass has no idea what classes you are actually using. This is something you will have to manually trim down yourself. Open up the provided .scss file and hack out anything you don't need.

Editing the font file itself to eliminate unneeded glyphs requires a 3rd party application to do so and is beyond the scope of this question.


Fontello is an online web service that can do all of this for you. It lets you mix and match between multiple icon font collections to create the perfect font file for your project. In addition to the customized font file, it provides multiple .css files containing styles already generated for you (changing the extension to .scss will allow you to import them into your existing Sass project).

查看更多
一夜七次
7楼-- · 2020-05-11 21:40

You can now subset icons from Font-awesome for production use. There is now an official subsetting tool called icnfnt, which allows you to pick and package just the icons you need from the current version of Font-awesome (v3.0.2).

The custom download also includes all CSS, LESS, SCSS and SASS code!

查看更多
登录 后发表回答