Custom web fonts in 2019
I finally updating my old somebits.com CSS to use a wider text box and a modern font. Goodbye, Arial 400px wide! I’ve not paid much attention the development of CSS3 font technology so this is all new to me. I’m using free fonts.
Which font?
Free, not ugly, and with decent Unicode range. I looked through this Wikipedia page and hit on several likely options: Bitstream Vera, Liberation fonts, Noto fonts, and Source Serif Pro. I decided I liked how Source Serif Pro looked best. I don’t know how great its Unicode coverage is; from what I’ve read it’s mostly western scripts. Fine for normal use, and hopefully the browser fallbacks will suffice for the occasional CJK or obscure other stuff.
Update: I just realized that Feedly uses ITC Charter, which is free. There’s also a Unicode-enhanced version from SIL called Charis. I converted their WOFF to WOFF2 using an online tool and tried it on my blog. It’s stylistically very similar to Source Serif Pro but not quite as good looking and significantly larger files, so I won’t use it.
Hosted fonts
The easiest way to use a custom font is use a hosted solution like Google Fonts or Adobe Fonts (nee Typekit). They have you include a single CSS stylesheet and then you can refer to the fonts by name in normal CSS. The problem with this is they then get tracking info on every page that uses the font, which is bad for privacy. So I’d prefer to self host.
Self hosting
First you have to download the font in the right format. Choices are EOT, TTF, OTF, WOFF, WOFF2, and SVG. This website says EOT and SVG are obsolete, and TTF and OTF are unnecessary, and that WOFF is basically a wrapper for TTF/OTF and WOFF2 is just an improvement on WOFF. Confusingly, Adobe’s distribution of Source Serif includes both “TTF WOFF” and “OTF WOFF” versions. This page says OTF is better. WOFF2 seems to be nearly universally supported so I’m going to go with that with the OTF variant. (A lot of sites still serve WOFF as well, but I think that mostly only matters to MSIE 11 at this point.)
You can get a clue how to self-host fonts by just copying what Google or Adobe serves as CSS. See Adobe, Google. You can get pretty far just by copying what they do but changing the URL. But starting from first principles...
The simplest thing that work is this little bit of CSS
@font-face {
font-family: "source-serif-pro";
src:url("SourceSerifPro-Regular.otf.woff2");
}
body { font-family: source-serif-pro }
This says “I’m declaring a new font-face named source-serif-pro and you can find it at this URL”. Then tell the body of the document to use that font. And it all works, done! It also does reasonable Unicode fallbacks for things that aren’t in the font. But not quite done; this font only is normal font; the browser will fake italic and bold versions (ie, ugly). This is better:
@font-face {
font-family: "source-serif-pro";
font-style: normal;
font-weight: 400;
src:url("SourceSerifPro-Regular.otf.woff2");
}
@font-face {
font-family: "source-serif-pro";
font-style: italic;
font-weight: 400;
src:url("SourceSerifPro-It.otf.woff2");
}
... versions for Bold and BoldItalic as well ...
That will let the browser use a properly designed italic font when necessary instead of just slanting the letters to the right. That really matters for bold, bold is used a lot in CSS (like headers). The key thing here is you’re expanding the options available on the source-serif-pro family, with the cost of now the browser has to download more files (100k each). In theory you can bundle all 4 variants into one file using variable fonts but I think that’s still a little bleedingedge.
There’s some advanced options too. You can tell the browser you only care about certain Unicode ranges, which may help with download sizes. You can tell the browser to preload the fonts or use local copies if the exit. You can fine tune whether it will use a system font until the real font is loaded. You can also tweak font rendering settings and kerning.
Anyway, now I know how to self host a font. You can see a bunch of test cases, etc in this dev test directory. The selfhosted variation is what I will end up using.