mozilla

Revision 340635 of @font-face

  • Revision slug: CSS/@font-face
  • Revision title: @font-face
  • Revision id: 340635
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment

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.

{{seeCompatTable}}

Syntax

@font-face {
  [font-family: <family-name>;]?
  [src: [ <uri> [format(<string>#)]? | <font-face-name> ]#;]?
  [unicode-range: <urange>#;]?
  [font-variant: <font-variant>;]?
  [font-feature-settings: normal|<feature-tag-value>#;]?
  [font-stretch: <font-stretch>;]?
  [font-weight: <weight>];
  [font-style: <style>];
}

Values

<family-name>
Specifies a font name that will be used as font face value for font properties.
<uri>
URL for the remote font file location, or the name of a font on the user's computer in the form local("Font Name").
<weight>
A font weight value.
<style>
A font style value.

You can specify a font on the user's local computer by name using the local() syntax. If that font isn't found, other sources will be tried until one is found.

Examples

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

View live sample

<html>
<head>
  <title>Web Font Sample</title>
  <style type="text/css" media="screen, print">
    @font-face {
      font-family: "Bitstream Vera Serif Bold";
      src: url("http://developer.mozilla.org/@api/deki/files/2934/=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 doesn't 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

  • In Gecko, 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.
  • Note: 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.
  • When Gecko displays a page that uses web fonts, it initially displays text using the best CSS fallback font available on the user's computer while it waits for the web font to finish downloading.  As each web font finishes downloading, Gecko updates the text that uses that font.  This allows the user to read the text on the page more quickly.

Specifications

Specification Status Comment
{{SpecName('WOFF1.0', '', 'WOFF font format')}} {{Spec2('WOFF1.0')}} Font format specification
{{SpecName('CSS3 Fonts', '#font-face-rule', '@font-face')}} {{Spec2('CSS3 Fonts')}}  

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
SVG Font {{CompatNo}}
{{unimplemented_inline(119490)}}
yes {{CompatNo}} yes yes
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 {{CompatNo}} {{CompatGeckoMobile("5.0")}} {{CompatUnknown}} {{CompatNo}} 11.0 {{CompatNo}}
SVG fonts {{CompatNo}} {{CompatNo}}
{{unimplemented_inline(119490)}}
{{CompatUnknown}} {{CompatNo}} 10.0 {{CompatVersionUnknown}}

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.

See also

MDN

  • About WOFF
  • {{CSS_at_rules}}
  • {{CSS_Reference:Fonts}}

External

Revision Source

<div>
  {{CSSRef}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>@font-face</code> <a href="/en-US/docs/CSS" title="CSS">CSS</a> <a href="/en-US/docs/CSS/At-rule" title="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" title="CSS/At-rule#Conditional_Group_Rules">CSS conditional-group at-rule</a>.</p>
<p>{{seeCompatTable}}</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
@font-face {
  [font-family: &lt;family-name&gt;;]?
  [src: [ &lt;uri&gt; [format(&lt;string&gt;#)]? | &lt;font-face-name&gt; ]#;]?
  [unicode-range: &lt;urange&gt;#;]?
  [font-variant: &lt;font-variant&gt;;]?
  [font-feature-settings: normal|&lt;feature-tag-value&gt;#;]?
  [font-stretch: &lt;font-stretch&gt;;]?
  [font-weight: &lt;weight&gt;];
  [font-style: &lt;style&gt;];
}
</pre>
<h3 id="Values">Values</h3>
<dl>
  <dt>
    &lt;family-name&gt;</dt>
  <dd>
    Specifies a font name that will be used as font face value for font properties.</dd>
  <dt>
    &lt;uri&gt;</dt>
  <dd>
    URL for the remote font file location, or the name of a font on the user's computer in the form <code>local("Font Name")</code>.</dd>
  <dt>
    &lt;weight&gt;</dt>
  <dd>
    A <a href="/en-US/docs/CSS/font-weight" title="CSS/font-weight">font weight</a> value.</dd>
  <dt>
    &lt;style&gt;</dt>
  <dd>
    A <a href="/en-US/docs/CSS/font-style" title="CSS/font-style">font style</a> value.</dd>
</dl>
<p>You can specify a font on the user's local computer by name using the <code>local()</code> syntax. If that font isn't found, other sources will be tried until one is found.</p>
<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="/@api/deki/files/2935/=webfont-sample.html" title="/@api/deki/files/2935/=webfont-sample.html">View live sample</a></p>
<pre>
&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("http://developer.mozilla.org/@api/deki/files/2934/=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 doesn't 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>In Gecko, 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" title="HTTP access control">HTTP access controls</a> are used to relax this restriction.</li>
  <li>
    <div class="note">
      <strong>Note:</strong> 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.</div>
  </li>
  <li>When Gecko displays a page that uses web fonts, it initially displays text using the best CSS fallback font available on the user's computer while it waits for the web font to finish downloading.&nbsp; As each web font finishes downloading, Gecko updates the text that uses that font.&nbsp; This allows the user to read the text on the page more quickly.</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('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>&nbsp;</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><a href="/en-US/docs/WOFF" title="About_WOFF">WOFF</a></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>SVG Font</td>
        <td>{{CompatNo}}<br />
          {{unimplemented_inline(119490)}}</td>
        <td>yes</td>
        <td>{{CompatNo}}</td>
        <td>yes</td>
        <td>yes</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>{{CompatNo}}</td>
        <td>{{CompatGeckoMobile("5.0")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatNo}}</td>
        <td>11.0</td>
        <td>{{CompatNo}}</td>
      </tr>
      <tr>
        <td>SVG fonts</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}<br />
          {{unimplemented_inline(119490)}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatNo}}</td>
        <td>10.0</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h3 id="Notes">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>
</ul>
<h2 id="See_also">See also</h2>
<h3 id="MDN">MDN</h3>
<ul>
  <li><a href="/en-US/docs/WOFF" title="About WOFF">About WOFF</a></li>
  <li>{{CSS_at_rules}}</li>
  <li>{{CSS_Reference:Fonts}}</li>
</ul>
<h3 id="External">External</h3>
<ul>
  <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/" title="http://openfontlibrary.org/">Open&nbsp;Font Library</a></li>
  <li><a class="external" href="http://msdn.microsoft.com/en-us/library/ms530757(VS.85).aspx">Microsoft Developer Network (MSDN) @font-face reference</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>
</ul>
Revert to this revision