Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1132361 of @font-face

  • Revision slug: Web/CSS/@font-face
  • Revision title: @font-face
  • Revision id: 1132361
  • Created:
  • Creator: Didglee
  • Is current revision? No
  • Comment Improve some of the grammar

Revision Content

{{CSSRef}}

Summary

The @font-face CSS at-rule allows authors to specify online fonts to display text on their web pages. By allowing authors to provide their own fonts, @font-face eliminates the need to depend on the limited number of fonts users have installed on their computers. The @font-face at-rule may be used not only at the top level of a CSS, but also inside any CSS conditional-group at-rule.

Syntax

Descriptors

{{cssxref("@font-face/font-family", "font-family")}}
Specifies a name that will be used as the font face value for font properties.
{{cssxref("@font-face/src", "src")}}
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.
{{cssxref("@font-face/font-variant", "font-variant")}}
A {{cssxref("font-variant")}} value.
{{cssxref("@font-face/font-stretch", "font-stretch")}}
A {{cssxref("font-stretch")}} value.
{{cssxref("@font-face/font-weight", "font-weight")}}
A {{cssxref("font-weight")}} value.
{{cssxref("@font-face/font-style", "font-style")}}
A {{cssxref("font-style")}} value.
{{cssxref("@font-face/unicode-range", "unicode-range")}}
The range of Unicode code points to be used from the font.

Formal syntax

{{csssyntax}}

Examples

This example simply specifies a downloadable font to use, applying it to the entire body of the document:

View the live example

<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>

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:

@font-face {
  font-family: MyHelvetica;
  src: local("Helvetica Neue Bold"),
       local("HelveticaNeue-Bold"),
       url(MgOpenModernaBold.ttf);
  font-weight: bold;
}

Notes

  • Web fonts are subject to the same domain restriction (font files must be on the same domain as the page using them), unless HTTP access controls are used to relax this restriction.
  • Because there are no defined MIME types for TrueType, OpenType, and Web Open File Format (WOFF) fonts, the MIME type of the file specified is not considered.
  • @font-face cannot be declared within a CSS selector. For example, the following will not work:
  • .className { 
       @font-face { 
       font-family: MyHelvetica; 
       src: local("Helvetica Neue Bold"), 
       local("HelveticaNeue-Bold"), 
       url(MgOpenModernaBold.ttf); 
       font-weight: bold; 
       } 
    }

Specifications

Specification Status Comment
{{SpecName('WOFF2.0', '', 'WOFF2 font format')}} {{Spec2('WOFF2.0')}} Font format specification with new compression algorithm
{{SpecName('WOFF1.0', '', 'WOFF font format')}} {{Spec2('WOFF1.0')}} Font format specification
{{SpecName('CSS3 Fonts', '#font-face-rule', '@font-face')}} {{Spec2('CSS3 Fonts')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{CompatGeckoDesktop("1.9.1")}} 4.0 4.0 10.0 3.1
WOFF {{CompatGeckoDesktop("1.9.1")}} 6.0 9.0 11.10 5.1
WOFF2 {{CompatGeckoDesktop("39")}}[1] 38 {{CompatNo}} 24 {{CompatNo}}
SVG Fonts[2] {{CompatNo}}[3] {{CompatVersionUnknown}} {{CompatNo}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
unicode-range

{{CompatGeckoDesktop("36")}}

{{CompatVersionUnknown}} 9.0 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mini Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("1.9.1")}} {{CompatUnknown}} {{CompatNo}} 10.0 {{CompatVersionUnknown}}
WOFF 4.4 {{CompatGeckoMobile("5.0")}} 10.0 {{CompatNo}} 11.0 5.0
WOFF2 {{CompatNo}} {{CompatGeckoMobile("39.0")}}[1] {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}}
SVG Fonts[2] {{CompatUnknown}} {{CompatNo}}[3] {{CompatUnknown}} {{CompatNo}} 10.0 {{CompatVersionUnknown}}
unicode-range {{CompatUnknown}} {{CompatGeckoMobile("36.0")}} {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatVersionUnknown}}

[1] WOFF2 supports from Gecko 35 to Gecko 38 included is only activated by default on Aurora and Nightly versions. To use it on Beta and Release versions, the user needs to set the gfx.downloadable_fonts.woff2.enabled preference to true.

[2] Support for SVG Fonts was dropped in favor of allowing SVG glyphs within OpenType font packages.

[3] For Gecko there was {{Bug("119490")}} filed for implementing SVG Fonts, though as SVG Fonts got superseded by SVG glyphs within OpenType font packages, the bug got closed.

Notes

  • Support of the Embedded OpenType font format is not included in the compatibility table because it is a proprietary feature. Prior to IE 9.0, IE supported only this format.
  • TrueType and OpenType are not included because they are superseded by WOFF.
  • The same origin policy is not implemented in WebKit based browsers like Safari and Mobile Safari. This was also true for Blink based browsers, until September, 2014 - Chrome 37 and Opera 24.

See also

Revision Source

<div>{{CSSRef}}</div>

<h2 id="Summary">Summary</h2>

<p>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> allows authors to specify online fonts to display text on their web pages. By allowing authors to provide their own fonts, <code>@font-face</code> eliminates the need to depend on the limited number of fonts users have installed on their computers. 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/CSS/At-rule#Conditional_Group_Rules">CSS conditional-group at-rule</a>.</p>

<h2 id="Syntax">Syntax</h2>

<h3 id="Descriptors">Descriptors</h3>

<dl>
 <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/src", "src")}}</dt>
 <dd>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.</dd>
 <dt>{{cssxref("@font-face/font-variant", "font-variant")}}</dt>
 <dd>A {{cssxref("font-variant")}} value.</dd>
 <dt>{{cssxref("@font-face/font-stretch", "font-stretch")}}</dt>
 <dd>A {{cssxref("font-stretch")}} value.</dd>
 <dt>{{cssxref("@font-face/font-weight", "font-weight")}}</dt>
 <dd>A {{cssxref("font-weight")}} value.</dd>
 <dt>{{cssxref("@font-face/font-style", "font-style")}}</dt>
 <dd>A {{cssxref("font-style")}} value.</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>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">
{{csssyntax}}</pre>

<h2 id="Examples">Examples</h2>

<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">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Web Font Sample&lt;/title&gt;
  &lt;style type="text/css" media="screen, print"&gt;
    @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 }
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  This is Bitstream Vera Serif Bold.
&lt;/body&gt;
&lt;/html&gt;
</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">
@font-face {
  font-family: MyHelvetica;
  src: local("Helvetica Neue Bold"),
       local("HelveticaNeue-Bold"),
       url(MgOpenModernaBold.ttf);
  font-weight: bold;
}
</pre>

<h2 id="Notes">Notes</h2>

<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>Because there are no defined MIME types for TrueType, OpenType, and Web Open File Format (WOFF) fonts, the MIME type of the file specified is not considered.</li>
 <li>@font-face cannot be declared within a CSS selector.&nbsp;For example, the following will not work:</li>
</ul>

<ul>
 <li>
  <pre class="brush: css">
.className { 
   @font-face { 
&nbsp;  font-family: MyHelvetica; 
&nbsp;  src: local("Helvetica Neue Bold"), 
&nbsp;  local("HelveticaNeue-Bold"), 
&nbsp;  url(MgOpenModernaBold.ttf); 
&nbsp;  font-weight: bold; 
&nbsp;  } 
}</pre>
 </li>
</ul>

<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>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Firefox (Gecko)</th>
   <th>Chrome</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>4.0</td>
   <td>4.0</td>
   <td>10.0</td>
   <td>3.1</td>
  </tr>
  <tr>
   <td>WOFF</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>6.0</td>
   <td>9.0</td>
   <td>11.10</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td>WOFF2</td>
   <td>{{CompatGeckoDesktop("39")}}<sup>[1]</sup></td>
   <td>38</td>
   <td>{{CompatNo}}</td>
   <td>24</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>SVG Fonts<sup>[2]</sup></td>
   <td>{{CompatNo}}<sup>[3]</sup></td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>unicode-range</code></td>
   <td>
    <p>{{CompatGeckoDesktop("36")}}</p>
   </td>
   <td>{{CompatVersionUnknown}}</td>
   <td>9.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mini</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1.9.1")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>10.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>WOFF</td>
   <td>4.4</td>
   <td>{{CompatGeckoMobile("5.0")}}</td>
   <td>10.0</td>
   <td>{{CompatNo}}</td>
   <td>11.0</td>
   <td>5.0</td>
  </tr>
  <tr>
   <td>WOFF2</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile("39.0")}}<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>SVG Fonts<sup>[2]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}<sup>[3]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>10.0</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td><code>unicode-range</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile("36.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] WOFF2 supports from Gecko 35 to Gecko 38 included is only activated by default on Aurora and Nightly versions. To use it on Beta and Release versions, the user needs to set the <code>gfx.downloadable_fonts.woff2.enabled</code> preference to <code>true</code>.</p>

<p>[2] Support for SVG Fonts was dropped in favor of allowing SVG glyphs within OpenType font packages.</p>

<p>[3] For Gecko there was {{Bug("119490")}} filed for implementing SVG Fonts, though as SVG Fonts got superseded by SVG glyphs within OpenType font packages, the bug got closed.</p>

<h3 id="Notes_2">Notes</h3>

<ul>
 <li>Support of the Embedded OpenType font format is not included in the compatibility table because it is a proprietary feature. Prior to IE 9.0, IE supported only this format.</li>
 <li>TrueType and OpenType are not included because they are superseded by WOFF.</li>
 <li>The same origin policy is not implemented in WebKit based browsers like Safari and Mobile Safari. This was also true for Blink based browsers, until September, 2014 - Chrome 37 and Opera 24.</li>
</ul>

<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="https://www.fontsquirrel.com/fontface/generator">FontSquirrel @font-face generator</a></li>
 <li><a class="external" href="https://hacks.mozilla.org/2009/06/beautiful-fonts-with-font-face/">Beautiful fonts with @font-face</a></li>
 <li><a class="external" href="https://openfontlibrary.org/">Open&nbsp;Font Library</a></li>
 <li><a class="external" href="https://msdn.microsoft.com/en-us/library/ms530757(VS.85).aspx">Microsoft Developer Network (MSDN) @font-face reference</a></li>
 <li><a class="external" href="https://caniuse.com/woff">When can I use WOFF?</a></li>
 <li><a class="external" href="https://caniuse.com/svg-fonts">When can I use SVG Fonts?</a></li>
</ul>
Revert to this revision