Skip to content

Commit

Permalink
Use the CSS Font Loading API to emulate font-synthesis (Roman is prel…
Browse files Browse the repository at this point in the history
…oaded)
  • Loading branch information
zachleat committed Nov 28, 2018
1 parent ba83cbe commit e94baa7
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions emulate-font-synthesis.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Emulate font-synthesis (CSS Font Loading API with one preload)</title>
<link rel="preload" href="font-lato/lato-regular-webfont.woff2" as="font" type="font/woff2" crossorigin>
<style>
body {
font-family: Lato, sans-serif;
}
</style>
<script>
if ("fonts" in document) {

// Make two
var fontRoman = new FontFace("Lato", "url('font-lato/lato-regular-webfont.woff2') format('woff2'),url('font-lato/lato-regular-webfont.woff') format('woff')");
var fontBold = new FontFace("Lato", "url('font-lato/lato-bold-webfont.woff2') format('woff2'),url('font-lato/lato-bold-webfont.woff') format('woff')", {
weight: "700"
});
var fontItalic = new FontFace("Lato", "url('font-lato/lato-italic-webfont.woff2') format('woff2'),url('font-lato/lato-italic-webfont.woff') format('woff')", {
style: "italic"
});
var fontBoldItalic = new FontFace("Lato", "url('font-lato/lato-bolditalic-webfont.woff2') format('woff2'),url('font-lato/lato-bolditalic-webfont.woff') format('woff')", {
weight: "700",
style: "italic"
});


fontRoman.load().then(function(font) {
document.fonts.add(font);
});
fontBold.load().then(function(font) {
document.fonts.add(font);
});
fontItalic.load().then(function(font) {
document.fonts.add(font);
});
fontBoldItalic.load().then(function(font) {
document.fonts.add(font);
});
}
</script>
</head>
<body>
<h1>Emulate font-synthesis (CSS Font Loading API with one preload)</h1>
<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>

<hr style="margin-top: 2em">
<ul>
<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>
<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>
</ul>
</body>
</html>

0 comments on commit e94baa7

Please sign in to comment.