Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple .woff files into one

On a website I manage we have several .woff files, one for each font. In the interest to save loading time I want to reduce the number of requests made. Is it possible to combine these woff files into one resource?

like image 686
Björn Andersson Avatar asked Oct 30 '12 08:10

Björn Andersson


People also ask

Can you merge font files?

Fast and Easy merging Choose the format of the initial fonts and the format you want to save the combined font to. Upload fonts and click the Merge button. You will get the download link as soon as the fonts are combined.


2 Answers

You can bundle the woff assets into your CSS with base64.

Inside your @font-face declaration:

url('data:application/x-font-woff;base64,myVeryLongBase64StringGoesHere...');

This may increase the asset's file size. In my experience this is usually by around 20% - roughly the same size as the equivalent TTF file. Much of this may be recovered with a gzip-capable server. The tradeoff is acceptable for me, but YMMV.

As is often recommended when embedding blobs in CSS - keep them all in a separate stylesheet, cited after your base style. Otherwise, the client may be waiting for the fonts to load before they see your content as intended.

like image 156
thumphries Avatar answered Sep 20 '22 15:09

thumphries


I don't have the reputation to comment, but to update @thumphries answer, you can now use the following for WOFF and WOFF2:

url('data:application/font-woff;base64,myVeryLongBase64StringGoesHere...');

url('data:font/woff2;base64,myVeryLongBase64StringGoesHere...');

And for reference, on UNIX, you can use the built-in base64 command to directly generate your base64 string like so:

$ base64 myfont.ttf > fontbase64.txt

I find this preferable, since latency is the biggest issue for mobile users, and I add a url fallback for browsers that don't speak WOFF2 (less than 8% of my users) to avoid increasing total size of the site. Like so:

url('data:font/woff2;base64,myVeryLongBase64StringGoesHere...'),
url('/fonts/myFont.woff') format('woff');
like image 20
Nathaniel Hill Avatar answered Sep 20 '22 15:09

Nathaniel Hill