Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
title: Inline CSS web fonts
date: "2013-08-28 18:34 CEST"
tags: performance, webfonts, css
published: true
If you serve web fonts via URL, the browser makes a separate request for every web font.
This is what the timeline looks like for a URL referenced font:
<%= article_image_tag 'tl-body.png', class: 'img-fluid' %>
<h4 class="text-center">
<span style='color: rgba(0, 0, 255, 0.67)'>Ready</span> <span style='color: rgba(255, 0, 0, 0.78)'>Load</span>
Fonts included from stylesheets cause extra requests that only starts once the DOM has loaded.
This causes a delay and flash of missing text or a font change flash while the fonts are loading.
However, you can avoid extra requests *and* rendering issues if you inline the fonts as data-uris.
Inline web fonts with this script:
~~~ bash
# download inliner and mark it as executable
curl > inline-fonts.rb; chmod +x inline-fonts.rb
# provide URL or path to convert
./inline-fonts.rb ''
[The script][inline-css-font.rb] outputs CSS with all the `url` values inlined in data-uris, `woff` format.
You can now deliver the fonts and CSS together in 1 request and the rendering is perfect,
no more <a href=""><def title="Flash of Unstyled Content">FOUC</def></a> or font change.
### There be dragons
* base64 encoding causes a +33% file size increase, but gzip mitigates this
* when there are more fonts files at some point it is faster to download them in parallel, depending on the font sizes and latency / bandwidth of the audience.
* public CDN files may already be in browser cache (e.g. the most popular Google Web Fonts)
* the most common format woff is [supported][caniuse-woff] everywhere but IE 8, stock Anroid Browser,
and Chrome on Windows, which has a [general rendering issue][chrome-font-issue] with some fonts, and base64 [is slow on Android 2 and iOS 5][mobify-data-uris-article].
You will have to serve other formats to these browsers
Most individual font sizes are in 10 - 20 KB range; if you only use a few font files this could easily cut down initial load times by 5-20%.
Measure with any client-side monitoring tool to find out (it is best if your tool shows the distribution, and not just the average).
This is what the timeline looks like once the fonts are inlined:
<%= article_image_tag 'tl-inline.png', class: 'img-fluid' %>
When inlined, all the fonts are finished downloading before DOMContentReady fires.
Alternatively, you could try using [Google Page Speed Module][mod-pagespeed], which may intellegently apply this and other optimizations, but has it's own drawbacks.
[SPDY]( should already be on for both timelines.
#### See also
* [Discussion on Hacker News](