Skip to content

Commit 1b82fd0

Browse files
committed
Simplified FOUT strategy (no class)
1 parent e2d8adc commit 1b82fd0

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

Diff for: README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ As web fonts are a progressive enhancement and with increasing support for the C
3333
* [Demo](https://www.zachleat.com/web-fonts/demos/fout-with-class.html) _(4 web fonts)_
3434
* or [using a polyfill](./fout-with-class-polyfill.html)[Demo](https://www.zachleat.com/web-fonts/demos/fout-with-class-polyfill.html) _(4 web fonts)_
3535

36+
### FOUT
37+
38+
Similar to the above, but without using a class—using only the CSS Font Loading API. This doesn’t require any modification of the CSS, injects the web fonts using JS programmatically. Documented in the [Webfont Handbook from @bramstein](https://abookapart.com/products/webfont-handbook).
39+
40+
* [Code](./fout.html)
41+
* [Demo](https://www.zachleat.com/web-fonts/demos/fout.html) _(4 web fonts)_
42+
43+
* **TODO** Related: `.style.fontFamily` method (only works well with one family per page), first saw this in a [tweet from @simevidas](https://twitter.com/simevidas/status/829016037366566912)
44+
3645
### FOFT
3746

3847
* [Code](./foft.html)
@@ -93,9 +102,6 @@ You’ll probably see blog posts on these at some point.
93102
* [FOUT metric matching with a Variable Font](./variablefont-fout-test.html)
94103
* Reduction in FOUT reflow (reduce text movement on web font render)
95104
* Related: [Font style matcher](https://meowni.ca/font-style-matcher/) from @notwaldorf
96-
* FOUT without a class
97-
* `.style.fontFamily` method (only works well with one family per page), first saw this in a [tweet from @simevidas](https://twitter.com/simevidas/status/829016037366566912)
98-
* CSS Font Loading API `.add()` method: Doesn’t require any modification of the CSS, injects the web fonts using JS programmatically (the Asynchronous Data URI method below also does this). Documented in the [Webfont Handbook from @bramstein](https://abookapart.com/products/webfont-handbook).
99105

100106
## Not Recommended but included for Posterity
101107

Diff for: fout.html

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>FOUT</title>
7+
<style>
8+
body {
9+
font-family: Lato, sans-serif;
10+
}
11+
12+
</style>
13+
<script>
14+
(function() {
15+
if( "fonts" in document ) {
16+
var fontRoman = new FontFace("Lato", "url('font-lato/lato-regular-webfont.woff2') format('woff2'),url('font-lato/lato-regular-webfont.woff') format('woff')");
17+
var fontBold = new FontFace("Lato", "url('font-lato/lato-bold-webfont.woff2') format('woff2'),url('font-lato/lato-bold-webfont.woff') format('woff')", {
18+
weight: "700"
19+
});
20+
var fontItalic = new FontFace("Lato", "url('font-lato/lato-italic-webfont.woff2') format('woff2'),url('font-lato/lato-italic-webfont.woff') format('woff')", {
21+
style: "italic"
22+
});
23+
var fontBoldItalic = new FontFace("Lato", "url('font-lato/lato-bolditalic-webfont.woff2') format('woff2'),url('font-lato/lato-bolditalic-webfont.woff') format('woff')", {
24+
weight: "700",
25+
style: "italic"
26+
});
27+
28+
Promise.all([
29+
fontRoman.load(),
30+
fontBold.load(),
31+
fontItalic.load(),
32+
fontBoldItalic.load()
33+
]).then(function(loadedFonts) {
34+
35+
// Render them at the same time
36+
loadedFonts.forEach(function(font) {
37+
document.fonts.add(font);
38+
});
39+
});
40+
}
41+
})();
42+
</script>
43+
</head>
44+
<body>
45+
<h1>FOUT</h1>
46+
<p>This is a paragraph. <strong>This is heavier text.</strong> <em>This is emphasized text.</em> <strong><em>This is heavier and emphasized text.</em></strong></p>
47+
48+
<hr style="margin-top: 2em">
49+
<ul>
50+
<li>This demo is a companion to <a href="https://www.zachleat.com/web/comprehensive-webfonts/">A Comprehensive Guide to Font Loading Strategies</a></li>
51+
<li>See all demos on <a href="https://github.com/zachleat/web-font-loading-recipes">GitHub <code>zachleat/web-font-loading-recipes</code></a></li>
52+
</ul>
53+
</body>
54+
</html>

0 commit comments

Comments
 (0)