Skip to content

Instantly share code, notes, and snippets.

@leonp
Created August 24, 2017 06:55
Show Gist options
  • Save leonp/eb0e249f71d9b7b726dc9e7eb08937f8 to your computer and use it in GitHub Desktop.
Save leonp/eb0e249f71d9b7b726dc9e7eb08937f8 to your computer and use it in GitHub Desktop.
Defer loading of local fonts, cancel after 2 second wait
# HTML
<body class="font-class-name">
## CSS
.[font-class-name] {
font-family: [fallback stack]
}
.c-font-loaded .c-interface {
font-family: [preferred stack]
}
// Example fonts
@font-face {
font-family: 'Interface';
font-style: normal;
font-weight: 400;
src: url("/fonts/Interface-Regular.woff2?v=1.1") format("woff2"),
url("/fonts/Interface-Regular.woff?v=1.1") format("woff");
}
@font-face {
font-family: 'Interface';
font-style: italic;
font-weight: 400;
src: url("/fonts/Interface-RegularItalic.woff2?v=1.1") format("woff2"),
url("/fonts/Interface-RegularItalic.woff?v=1.1") format("woff");
}
@font-face {
font-family: 'Interface';
font-style: normal;
font-weight: 700;
src: url("/fonts/Interface-Bold.woff2?v=1.1") format("woff2"),
url("/fonts/Interface-Bold.woff?v=1.1") format("woff");
}
## Javascript in footer
<script src="/path/to/font-face-observer.js"></script>
<script>
var font = new FontFaceObserver('Font name in stylesheet');
font.load(null, 2000).then(function () {
document.documentElement.className += " c-font-loaded";
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment