1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
---
title: '@font-face'
slug: Web/CSS/@font-face
translation_of: Web/CSS/@font-face
---
<div>{{CSSRef}}</div>
<p><span class="seoSummary">The <strong><code>@font-face</code></strong> <a href="/en-US/docs/CSS">CSS</a> <a href="/en-US/docs/CSS/At-rule">at-rule</a> specifies a custom font with which to display text; the font can be loaded from either a remote server or a locally-installed font on the user's own computer.</span></p>
<h2 id="Syntax">Syntax</h2>
<pre class="brush: css no-line-numbers notranslate">@font-face {
font-family: "Open Sans";
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),
url("/fonts/OpenSans-Regular-webfont.woff") format("woff");
}</pre>
<h3 id="Descriptors">Descriptors</h3>
<dl>
<dt>{{cssxref("@font-face/font-display", "font-display")}}</dt>
<dd>Determines how a font face is displayed based on whether and when it is downloaded and ready to use.</dd>
<dt>{{cssxref("@font-face/font-family", "font-family")}}</dt>
<dd>Specifies a name that will be used as the font face value for font properties.</dd>
<dt>{{cssxref("@font-face/font-stretch", "font-stretch")}}</dt>
<dd>A {{cssxref("font-stretch")}} value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example <code>font-stretch: 50% 200%;</code></dd>
<dt>{{cssxref("@font-face/font-style", "font-style")}}</dt>
<dd>A {{cssxref("font-style")}} value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example <code>font-style: oblique 20deg 50deg;</code></dd>
<dt>{{cssxref("@font-face/font-weight", "font-weight")}}</dt>
<dd>A {{cssxref("font-weight")}} value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example <code>font-weight: 100 400;</code></dd>
<dt>{{cssxref("@font-face/font-variant", "font-variant")}}</dt>
<dd>A {{cssxref("font-variant")}} value.</dd>
<dt>{{cssxref("font-feature-settings", "font-feature-settings")}}</dt>
<dd>Allows control over advanced typographic features in OpenType fonts.</dd>
<dt>{{cssxref("@font-face/font-variation-settings", "font-variation-settings")}}</dt>
<dd>Allows low-level control over OpenType or TrueType font variations, by specifying the four letter axis names of the features to vary, along with their variation values.</dd>
<dt>{{cssxref("@font-face/src", "src")}}</dt>
<dd>
<p>Specifies the resource containing the font data. This can be a URL to a remote font file location or the name of a font on the user's computer.</p>
<p>To provide the browser with a hint as to what format a font resource is — so it can select a suitable one — it is possible to include a format type inside a <code>format()</code> function:</p>
<pre class="brush: css notranslate">src: url(ideal-sans-serif.woff) <strong>format("woff")</strong>,
url(basic-sans-serif.ttf) <strong>format("truetype")</strong>;</pre>
<p>The available types are: <code>"woff"</code>, <code>"woff2"</code>, <code>"truetype"</code>, <code>"opentype"</code>, <code>"embedded-opentype"</code>, and <code>"svg"</code>.</p>
</dd>
<dt>{{cssxref("@font-face/unicode-range", "unicode-range")}}</dt>
<dd>The range of Unicode code points to be used from the font.</dd>
</dl>
<h2 id="Description">Description</h2>
<p>If the <code>local()</code> function is provided, specifying a font name to look for on the user's computer, and the {{Glossary("user agent")}} finds a match, that local font is used. Otherwise, the font resource specified using the <code>url()</code> function is downloaded and used.</p>
<p>By allowing authors to provide their own fonts, <code>@font-face</code> makes it possible to design content without being limited to the so-called "web-safe" fonts (that is, the fonts which are so common that they're considered to be universally available). The ability to specify the name of a locally-installed font to look for and use makes it possible to customize the font beyond the basics while making it possible to do so without relying on an Internet connection.</p>
<p>It's common to use both <code>url()</code> and <code>local()</code> together, so that the user's installed copy of the font is used if available, falling back to downloading a copy of the font if it's not found on the user's device.</p>
<p>The <code>@font-face</code> at-rule may be used not only at the top level of a CSS, but also inside any <a href="/en-US/docs/Web/CSS/At-rule#Conditional_group_rules">CSS conditional-group at-rule</a>.</p>
<h3 id="Font_MIME_Types">Font MIME Types</h3>
<table>
<thead>
<tr>
<th>Format</th>
<th>MIME type</th>
</tr>
</thead>
<tbody>
<tr>
<td>TrueType</td>
<td><code>font/ttf</code></td>
</tr>
<tr>
<td>OpenType</td>
<td><code>font/otf</code></td>
</tr>
<tr>
<td>Web Open Font Format</td>
<td><code>font/woff</code></td>
</tr>
<tr>
<td>Web Open Font Format 2</td>
<td><code>font/woff2</code></td>
</tr>
</tbody>
</table>
<h3 id="Notes">Notes</h3>
<ul>
<li>Web fonts are subject to the same domain restriction (font files must be on the same domain as the page using them), unless <a href="/en-US/docs/HTTP_access_control">HTTP access controls</a> are used to relax this restriction.</li>
<li><code>@font-face</code> cannot be declared within a CSS selector. For example, the following will not work:
<pre class="brush: css; example-bad notranslate">.className {
@font-face {
font-family: MyHelvetica;
src: local("Helvetica Neue Bold"),
local("HelveticaNeue-Bold"),
url(MgOpenModernaBold.ttf);
font-weight: bold;
}
}</pre>
</li>
</ul>
<h2 id="Formal_syntax">Formal syntax</h2>
<pre class="syntaxbox notranslate">{{csssyntax}}</pre>
<h2 id="Examples">Examples</h2>
<h3 id="Specifying_a_downloadable_font">Specifying a downloadable font</h3>
<p>This example simply specifies a downloadable font to use, applying it to the entire body of the document:</p>
<p><a href="https://mdn.mozillademos.org/files/7775/webfont-sample.html">View the live example</a></p>
<pre class="brush: html notranslate"><html>
<head>
<title>Web Font Sample</title>
<style type="text/css" media="screen, print">
@font-face {
font-family: "Bitstream Vera Serif Bold";
src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf");
}
body { font-family: "Bitstream Vera Serif Bold", serif }
</style>
</head>
<body>
This is Bitstream Vera Serif Bold.
</body>
</html>
</pre>
<p>In this example, the user's local copy of "Helvetica Neue Bold" is used; if the user does not have that font installed (two different names are tried), then the downloadable font named "MgOpenModernaBold.ttf" is used instead:</p>
<pre class="brush: css notranslate">@font-face {
font-family: MyHelvetica;
src: local("Helvetica Neue Bold"),
local("HelveticaNeue-Bold"),
url(MgOpenModernaBold.ttf);
font-weight: bold;
}
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('WOFF2.0', '', 'WOFF2 font format')}}</td>
<td>{{Spec2('WOFF2.0')}}</td>
<td>Font format specification with new compression algorithm</td>
</tr>
<tr>
<td>{{SpecName('WOFF1.0', '', 'WOFF font format')}}</td>
<td>{{Spec2('WOFF1.0')}}</td>
<td>Font format specification</td>
</tr>
<tr>
<td>{{SpecName('CSS3 Fonts', '#font-face-rule', '@font-face')}}</td>
<td>{{Spec2('CSS3 Fonts')}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("css.at-rules.font-face")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/WOFF">About WOFF</a></li>
<li><a href="https://everythingfonts.com/font-face">Everythingfonts font-face generator</a></li>
<li><a class="external" href="http://www.fontsquirrel.com/fontface/generator">FontSquirrel @font-face generator</a></li>
<li><a class="external" href="http://hacks.mozilla.org/2009/06/beautiful-fonts-with-font-face/">Beautiful fonts with @font-face</a></li>
<li><a class="external" href="http://openfontlibrary.org/">Open Font Library</a></li>
<li><a class="external" href="http://caniuse.com/woff">When can I use WOFF?</a></li>
<li><a class="external" href="http://caniuse.com/svg-fonts">When can I use SVG Fonts?</a></li>
<li><a href="https://coolfont.org">Free Fancy Cool Fonts</a></li>
<li><a href="https://nicknames.name/">Best Nicknames</a></li>
</ul>
|