NH

Loading .ttf fonts from server

October 06, 2020

Problem

Browser is unable to decode custom font. The setup is as follows:

@font-face {
  font-family: "FinkHeavy";
  font-weight: 400;
  font-style: normal;
  src: url("/FinkHeavy.ttf") format("truetype");
  font-display: swap;
}

Even though Chrome reportedly can support .ttf fonts with @font-face, it still threw a decoding error

Solution

Convert the font to .woff and .woff2 formats. The Transfonter tool can do the conversion

@font-face {
  font-family: "FinkHeavy";
  font-weight: 400;
  font-style: normal;
  src: url("/FinkHeavy.woff") format("woff"),
			 url("/FinkHeavy.woff2") format("woff2");
  font-display: swap;
}

Using .woff and .woff2 offers modern browser support. Including .ttf offers slightly deeper browser support according to CSS-Tricks

References


Hey, I'm Nancy Huynh
I'm a self-taught front-end developer relaxing in Toronto
This is a collection of new things I'm learning