Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Self-Host a Web Font

In this lesson, you sent a font’s file along with the garden’s site, so visitors’ browsers can download it for the headings without having it installed, and show the fallback until it arrives.

Follow Along

The video starts from events/upcoming.html and events/events.css as you left them in Choose Fonts Every Visitor Gets. The heading font, Fraunces Bold, is in the course’s code download: put its fonts/ folder in your garden/ folder, next to images/. It holds fraunces-bold.woff2 and the font’s license, OFL.txt. Fraunces is by the Fraunces Project Authors and is free to use and share under the SIL Open Font License.

Open the page through your editor’s live preview, in Chrome or Edge, with DevTools docked beside it.

Key Points

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, you saw that a font in a stack only shows for visitors who already have it. In this video, you’ll send a font along with the site, so visitors don’t need it installed.

00:12 Right now, the headings’ stack is Georgia, then serif. On this Linux computer, Liberation Serif is the font the heading comes out in. The garden wants Fraunces, a friendlier font, for its headings.

00:24 Its open license lets you send the file to every visitor. The course’s code download has a fonts folder with the file fraunces-bold.woff2 in it. Put that folder in your garden folder, next to images.

00:36 A woff2 file is a font file compressed for the web, and every current browser reads it.

00:44 Before a stack can use the font, describe it in an @font-face rule, at the end of events.css. It has no selector and styles nothing. Its lines describe the font file.

00:57 Its font-family names the font for your stacks. Any name works if they use the same one. Its src uses url() to say where the file is. A url() in a stylesheet starts from the stylesheet’s folder, as the banner’s photo did.

01:11 So the path goes up one level, then into fonts. Here, font-weight makes nothing bold. It says this file is the bold face.

01:22 Save. The headings don’t change.

01:25 Check the Network panel, which lists every file the page downloads. Reload, and there’s no request for the font. The rule only declares the font. Nothing downloads until some text asks for it.

01:40 So ask for it. In the headings’ rule, put Fraunces at the front of the stack, and save. The headings change to the new font. Now the Network panel has a new row, fraunces-bold.woff2, a font.

01:55 It comes from your own site’s server. In Rendered Fonts, the heading’s font is now Fraunces, and its origin is a network resource.

02:04 That’s self-hosting. The font file lives with your site. A font service, such as Google Fonts, keeps the file on its own server instead. It’s quick to set up.

02:14 However, every visitor’s browser then connects to another company’s server, which sees the visitor’s IP address. And your headings depend on a site you don’t run.

02:24 Self-hosting keeps the page and its font in one place.

02:30 A font file takes time to arrive. Until it does, CSS lets each browser decide what the heading shows, and some hide the text for a few seconds while they wait.

02:40 Add font-display: swap to the @font-face rule, and save. It tells the browser to show the text in the next available font of the stack right away, then swap in Fraunces once it arrives.

02:53 Your browser may do that already. With swap, every browser does.

02:58 To watch it, slow the page down. In the Network panel, tick Disable cache, so each reload downloads every file again. In the throttling menu, choose 3G, a slow connection.

03:10 Then reload. The old page, already in Fraunces, stays up while the slow stylesheet downloads. Then the new page shows its heading in the fallback serif, while the font’s row stays pending.

03:24 About three seconds later, the font arrives, and the heading swaps to the new font. Set the menu back to No throttling when you’re done.

03:34 So, to self-host a web font, put its woff2 file in your site. Declare it in an @font-face rule that finds the file from the stylesheet’s folder and swaps the font in when it arrives.

03:45 Then put its name at the front of a stack that ends in a generic family.

03:51 Now visitors’ browsers download Fraunces for the headings and show the fallback until it arrives. Next is Set Readable Lines and a Type Scale, where you’ll size the headings and give the text comfortable lines to read.

Become a Member to join the conversation.