You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -24,34 +24,38 @@ Web Font Loader gives you added control when using linked fonts via `@font-face`
24
24
25
25
To use the Web Font Loader library, just include it in your page and tell it which fonts to load. For example, you could load fonts from [Google Fonts](http://www.google.com/fonts/) using the Web Font Loader hosted on [Google Hosted Libraries](https://developers.google.com/speed/libraries/) using the following code.
Alternatively, you can link to the latest `1.x` version of the Web Font Loader by using `//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js` as the `script` source. Note that the version in this url is less specific. It will always load the latest `1.x` version, but it also has a shorter cache time to ensure that your page gets updates in a timely manner. For performance reasons, we recommend using an explicit version number (such as `1.4.7`) in urls when using the Web Font Loader in production. You can manually update the Web Font Loader version number in the url when you want to adopt a new version.
37
39
38
40
It is also possible to use the Web Font Loader asynchronously. For example, to load [Typekit](http://www.typekit.com) fonts asynchronously, you could use the following code.
var s =document.getElementsByTagName('script')[0];
55
+
s.parentNode.insertBefore(wf, s);
56
+
})();
57
+
</script>
58
+
```
55
59
56
60
Using the Web Font Loader asynchronously avoids blocking your page while loading the JavaScript. Be aware that if the script is used asynchronously, the rest of the page might render before the Web Font Loader is loaded and executed, which can cause a [Flash of Unstyled Text (FOUT)](http://help.typekit.com/customer/portal/articles/6852).
57
61
@@ -74,35 +78,41 @@ Web Font Loader provides an event system that developers can hook into. It gives
74
78
75
79
CSS events are implemented as classes on the `html` element. The following classes are set on the `html` element:
76
80
77
-
.wf-loading
78
-
.wf-active
79
-
.wf-inactive
80
-
.wf-<familyname>-<fvd>-loading
81
-
.wf-<familyname>-<fvd>-active
82
-
.wf-<familyname>-<fvd>-inactive
81
+
```css
82
+
.wf-loading
83
+
.wf-active
84
+
.wf-inactive
85
+
.wf-<familyname>-<fvd>-loading
86
+
.wf-<familyname>-<fvd>-active
87
+
.wf-<familyname>-<fvd>-inactive
88
+
```
83
89
84
90
The `<familyname>` placeholder will be replaced by a sanitized version of the name of each font family. Spaces and underscores are removed from the name, and all characters are converted to lower case. For example, `Droid Sans` becomes `droidsans`. The `<fvd>` placeholder is a [Font Variation Description](https://github.com/typekit/fvd). Put simply, it'sa shorthand for describing the style and weight of a particular font. Here are a few examples:
Keep in mind that `font-weight: normal` maps to `font-weight: 400` and `font-weight: bold` maps to `font-weight: 700`. If no style/weight is specified, the default `n4` (`font-style: normal; font-weight: normal;`) will be used.
93
101
94
102
If fonts are loaded multiple times on a single page, the CSS classes continue to update to reflect the current state of the page. The global `wf-loading` class is applied whenever fonts are being requested (even if other fonts are already active or inactive). The `wf-inactive` class is applied only if none of the fonts on the page have rendered. Otherwise, the `wf-active` class is applied (even if some fonts are inactive).
95
103
96
104
JavaScript events are implemented as callback functions on the `WebFontConfig` configuration object.
97
105
98
-
WebFontConfig = {
99
-
loading: function() {},
100
-
active: function() {},
101
-
inactive: function() {},
102
-
fontloading: function(familyName, fvd) {},
103
-
fontactive: function(familyName, fvd) {},
104
-
fontinactive: function(familyName, fvd) {}
105
-
};
106
+
```javascript
107
+
WebFontConfig = {
108
+
loading:function() {},
109
+
active:function() {},
110
+
inactive:function() {},
111
+
fontloading:function(familyName, fvd) {},
112
+
fontactive:function(familyName, fvd) {},
113
+
fontinactive:function(familyName, fvd) {}
114
+
};
115
+
```
106
116
107
117
The `fontloading`, `fontactive` and `fontinactive` callbacks are passed the family name and font variation description of the font that concerns the event.
108
118
@@ -112,25 +122,29 @@ Since the Internet is not 100% reliable, it's possible that a font will fail to
112
122
113
123
You can change the default timeout by using the `timeout` option on the `WebFontConfig` object.
114
124
115
-
WebFontConfig = {
116
-
google: {
117
-
families: ['Droid Sans']
118
-
},
119
-
timeout: 2000 // Set the timeout to two seconds
120
-
};
125
+
```javascript
126
+
WebFontConfig = {
127
+
google: {
128
+
families: ['Droid Sans']
129
+
},
130
+
timeout:2000// Set the timeout to two seconds
131
+
};
132
+
```
121
133
122
134
The timeout value should be in milliseconds, and defaults to 5000 milliseconds (5 seconds) if not supplied.
123
135
124
136
### Iframes
125
137
126
138
Usually, it's easiest to include a copy of Web Font Loader in every window where fonts are needed, so that each window manages its own fonts. However, if you need to have a single window manage fonts for multiple same-origin child windows or iframes that are built up using JavaScript, Web Font Loader supports that as well. Just use the optional `context` configuration option and give it a reference to the target window for loading:
127
139
128
-
WebFontConfig = {
129
-
google: {
130
-
families: ['Droid Sans']
131
-
},
132
-
context: frames['my-child']
133
-
};
140
+
```javascript
141
+
WebFontConfig = {
142
+
google: {
143
+
families: ['Droid Sans']
144
+
},
145
+
context:frames['my-child']
146
+
};
147
+
```
134
148
135
149
This is an advanced configuration option that isn't needed for most use cases.
136
150
@@ -148,41 +162,45 @@ You can specify a specific font variation or set of variations to load and watch
148
162
by appending the variations separated by commas to the family name separated by
149
163
a colon. Variations are specified using [FVD notation](https://github.com/typekit/fvd).
150
164
151
-
WebFontConfig = {
152
-
custom: {
153
-
families: ['My Font', 'My Other Font:n4,i4,n7'],
154
-
urls: ['/fonts.css']
155
-
}
156
-
};
165
+
```javascript
166
+
WebFontConfig = {
167
+
custom: {
168
+
families: ['My Font', 'My Other Font:n4,i4,n7'],
169
+
urls: ['/fonts.css']
170
+
}
171
+
};
172
+
```
157
173
158
174
In this example, the `fonts.css` file might look something like this:
159
175
160
-
@font-face {
161
-
font-family: 'My Font';
162
-
src: ...;
163
-
}
164
-
@font-face {
165
-
font-family: 'My Other Font';
166
-
font-style: normal;
167
-
font-weight: normal; /* or 400 */
168
-
src: ...;
169
-
}
170
-
@font-face {
171
-
font-family: 'My Other Font';
172
-
font-style: italic;
173
-
font-weight: normal; /* or 400 */
174
-
src: ...;
175
-
}
176
-
@font-face {
177
-
font-family: 'My Other Font';
178
-
font-style: normal;
179
-
font-weight: bold; /* or 700 */
180
-
src: ...;
181
-
}
176
+
```css
177
+
@font-face {
178
+
font-family: 'My Font';
179
+
src: ...;
180
+
}
181
+
@font-face {
182
+
font-family: 'My Other Font';
183
+
font-style: normal;
184
+
font-weight: normal; /* or 400 */
185
+
src: ...;
186
+
}
187
+
@font-face {
188
+
font-family: 'My Other Font';
189
+
font-style: italic;
190
+
font-weight: normal; /* or 400 */
191
+
src: ...;
192
+
}
193
+
@font-face {
194
+
font-family: 'My Other Font';
195
+
font-style: normal;
196
+
font-weight: bold; /* or 700 */
197
+
src: ...;
198
+
}
199
+
```
182
200
183
201
Alternatively, you can load your fonts from a stylesheet not specified in WebFontConfig. As long as the names match those that are declared in the `families` array, the proper loading classes will be applied to the html element.
@@ -202,70 +220,82 @@ Alternatively, you can load your fonts from a stylesheet not specified in WebFon
202
220
203
221
The custom module also supports customizing the test strings that are used to determine whether or not a font has loaded. This can be used to load fonts with custom subsets or glyphs in the private use unicode area.
204
222
205
-
WebFontConfig = {
206
-
custom: {
207
-
families: ['My Font'],
208
-
testStrings: {
209
-
'My Font': '\uE003\uE005'
210
-
}
211
-
}
212
-
};
223
+
```javascript
224
+
WebFontConfig = {
225
+
custom: {
226
+
families: ['My Font'],
227
+
testStrings: {
228
+
'My Font':'\uE003\uE005'
229
+
}
230
+
}
231
+
};
232
+
```
213
233
214
234
Tests strings should be specified on a per font basis and contain at least one character. If not specified the default test string (`BESbswy`) is used.
215
235
216
236
### Fontdeck
217
237
218
238
To use the [Fontdeck](http://fontdeck.com/) module, specify the ID of your website. You can find this ID on the website page within your account settings.
219
239
220
-
WebFontConfig = {
221
-
fontdeck: {
222
-
id: 'xxxxx'
223
-
}
224
-
};
240
+
```javascript
241
+
WebFontConfig = {
242
+
fontdeck: {
243
+
id:'xxxxx'
244
+
}
245
+
};
246
+
```
225
247
226
248
### Fonts.com
227
249
228
250
When using [Fonts.com web fonts](http://webfonts.fonts.com/) specify your Project ID.
version: 12345 // (optional, flushes the CDN cache)
234
-
}
235
-
};
252
+
```javascript
253
+
WebFontConfig = {
254
+
monotype: {
255
+
projectId:'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
256
+
version:12345// (optional, flushes the CDN cache)
257
+
}
258
+
};
259
+
```
236
260
237
261
The Fonts.com module has an optional `version` option which acts as a cache-buster.
238
262
239
263
### Google
240
264
241
265
Using [Google's Font API](https://code.google.com/apis/webfonts/docs/getting_started.html), name the font families you'd like to load. You can use the same [syntax](https://developers.google.com/fonts/docs/getting_started#Syntax) as in the Font API to specify styles:
242
266
243
-
WebFontConfig = {
244
-
google: {
245
-
families: ['Droid Sans', 'Droid Serif:bold']
246
-
}
247
-
};
267
+
```javascript
268
+
WebFontConfig = {
269
+
google: {
270
+
families: ['Droid Sans', 'Droid Serif:bold']
271
+
}
272
+
};
273
+
```
248
274
249
275
You can also supply the `text` parameter to perform character subsetting:
250
276
251
-
WebFontConfig = {
252
-
google: {
253
-
families: ['Droid Sans', 'Droid Serif'],
254
-
text: 'abcdedfghijklmopqrstuvwxyz!'
255
-
}
256
-
};
277
+
```javascript
278
+
WebFontConfig = {
279
+
google: {
280
+
families: ['Droid Sans', 'Droid Serif'],
281
+
text:'abcdedfghijklmopqrstuvwxyz!'
282
+
}
283
+
};
284
+
```
257
285
258
286
The `text` subsetting functionality is only available for the Google module.
259
287
260
288
### Typekit
261
289
262
290
When using [Typekit](http://www.typekit.com), specify the Kit to retrieve by its ID. You can find the Kit ID within Typekit's Kit Editor interface.
263
291
264
-
WebFontConfig = {
265
-
typekit: {
266
-
id: 'xxxxxx'
267
-
}
268
-
};
292
+
```javascript
293
+
WebFontConfig = {
294
+
typekit: {
295
+
id:'xxxxxx'
296
+
}
297
+
};
298
+
```
269
299
270
300
**FYI:** Typekit's own JavaScript is built using the Web Font Loader library and already provides all of the same font event functionality. If you're using Typekit, you should use their embed codes directly unless you also need to load web fonts from other providers on the same page.
271
301
@@ -281,10 +311,12 @@ For fonts loaded from supported providers, the `fontactive` event will be trigge
281
311
282
312
For example:
283
313
284
-
WebFontConfig = {
285
-
providerA: 'Family1',
286
-
providerB: 'Family2'
287
-
};
314
+
```javascript
315
+
WebFontConfig = {
316
+
providerA:'Family1',
317
+
providerB:'Family2'
318
+
};
319
+
```
288
320
289
321
If `providerA` can serve fonts to a browser, but `providerB` cannot, The `fontinactive` event will be triggered for `Family2`. The `fontactive` event will be triggered for `Family1` once it loads, as will the `active` event.
0 commit comments