font-family

  • Revision slug: CSS/font-family
  • Revision title: font-family
  • Revision id: 333681
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{CSSRef}}

Summary

The font-family CSS property allows for a prioritized list of font family names and/or generic family names to be specified for the selected element. Unlike most other CSS properties, values are separated by a comma to indicate that they are alternatives. The browser will select the first font on the list that is installed on the computer, or that can be downloaded using the information provided by a {{cssxref("@font-face")}} at-rule.

Web authors should always add at least one generic family in a font-family list, since there's no guarantee that a specific font is intalled on the computer or can be downloaded using a {{cssxref("@font-face")}} at-rule. The generic family lets the browser select an acceptable fallback font when needed.

It is often convenient to use the shorthand property {{cssxref("font")}} to set font-size and other font related properties all at once.

Note: The font-family property specifies a list of fonts, from highest priority to lowest. Font selection does not simply stop at the first font named in the list that is on the user's system. Rather, font selection is done one character at a time, so that if an available font does not have a glyph that can display a character needed, the later available fonts are tried. However, this doesn't work in Internet Explorer.

When a font is only available in some styles, variants, or sizes, those properties may also influence which font family is chosen.
  • {{Xref_cssinitial}} depends on user agent
  • Applies to all elements
  • {{Xref_cssinherited}} yes
  • Media {{Xref_cssvisual}}
  • {{Xref_csscomputed}} as specified

Syntax

font-family:  family-or-generic-name [, family-or-generic-name]* 

Values

family-or-generic-name

Is either a:

family-name The name of a font family. For example, "Times" and "Helvetica" are font families. Font family names containing whitespace should be quoted.
generic-family

Generic font families are a fallback mechanism, a means of preserving some of the style sheet author's intent in case when none of the specified fonts are available. Generic family names are keywords and must not be quoted. A generic font family should be the a last alternative in the list of font family names.

serif
Glyphs have finishing strokes, flared or tapering ends, or have actual serifed endings.
E.g.  Palatino, "Palatino Linotype", Palladio, "URW Palladio", serif
sans-serif
Glyphs have stroke endings that are plain.
E.g. 'Trebuchet MS', 'Liberation Sans', 'Nimbus Sans L', sans-serif
cursive
Glyphs in cursive fonts generally have either joining strokes or other cursive characteristics beyond those of italic typefaces. The glyphs are partially or completely connected, and the result looks more like handwritten pen or brush writing than printed letterwork.
fantasy
Fantasy fonts are primarily decorative fonts that contain playful representations of characters.
monospace
All glyphs have the same fixed width.
E.g. "DejaVu Sans Mono", Menlo, Consolas, "Liberation Mono", Monaco, "Lucida Console", monospace

Valid family names

Font family names must either be given quoted as strings, or unquoted as a sequence of one or more identifiers. This means that punctuation characters and digits at the start of each token must be escaped in unquoted font family names.

For example, the following declarations are valid:

font-family: Gill Sans Extrabold, sans-serif;
font-family: "Goudy Bookletter 1911", sans-serif;

The following declarations are invalid:

font-family: Goudy Bookletter 1911, sans-serif;
font-family: Red/Black, sans-serif;
font-family: "Lucida" Grande, sans-serif;
font-family: Ahem!, sans-serif;
font-family: test@foo, sans-serif;
font-family: #POUND, sans-serif;
font-family: Hawaii 5-0, sans-serif;

Examples

{{CSSRefExampleLink("font-family")}}

body     { font-family: "Gill Sans Extrabold", Helvetica, sans-serif }

.receipt { font-family: Courier, "Lucida Console", monospace }

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1")}} 3.0 3.5 1.0
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 1.0 {{CompatGeckoMobile("1")}} 6.0 6.0 1.0

Specifications

Specification Status Comment
{{SpecName('CSS3 Fonts', '#font-family-prop', 'font-family')}} {{Spec2('CSS3 Fonts')}} No significant change.
{{SpecName('CSS2.1', 'fonts.html#propdef-font-family', 'font-family')}} {{Spec2('CSS2.1')}} No significant change.
{{SpecName('CSS1', '#font-family', 'font-familiy')}} {{Spec2('CSS1')}}  

See also

  • Font-related CSS properties: {{CSS_Reference:Fonts}}

Revision Source

<div>{{CSSRef}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>font-family</code> CSS property allows for a prioritized list of font family names and/or generic family names to be specified for the selected element. Unlike most other CSS properties, values are separated by a comma to indicate that they are alternatives. The browser will select the first font on the list that is installed on the computer, or that can be downloaded using the information provided by a {{cssxref("@font-face")}} at-rule.</p>
<p>Web authors should always add at least one generic family in a font-family list, since there's no guarantee that a specific font is intalled on the computer or can be downloaded using a {{cssxref("@font-face")}} at-rule. The generic family lets the browser select an acceptable fallback font when needed.</p>
<p>It is often convenient to use the shorthand property {{cssxref("font")}} to set <code>font-size</code> and other font related properties all at once.</p>
<div class="note">
  <strong>Note:</strong>&nbsp;The <code>font-family</code> property specifies a list of fonts, from highest priority to lowest. Font selection does <strong>not</strong> simply stop at the first font named in the list that is on the user's system. Rather, font selection is done <strong>one character at a time</strong>, so that if an available font does not have a glyph that can display a character needed, the later available fonts are tried. However, this doesn't work in Internet Explorer.<br />
  <br />
  When a font is only available in some <a href="/en-US/docs/CSS/font-style" title="CSS/font-style">styles</a>, <a href="/en-US/docs/CSS/font-variant" title="CSS/font-variant">variants</a>, or <a href="/en-US/docs/CSS/font-size" title="CSS/font-size">sizes</a>, those properties may also influence which font family is chosen.</div>
<ul class="cssprop">
  <li><dfn>{{Xref_cssinitial}}</dfn> depends on user agent</li>
  <li><dfn>Applies to </dfn>all elements</li>
  <li><dfn>{{Xref_cssinherited}}</dfn> yes</li>
  <li><dfn>Media</dfn> {{Xref_cssvisual}}</li>
  <li><dfn>{{Xref_csscomputed}}</dfn> as specified</li>
</ul>
<h2 id="Syntax">Syntax</h2>
<pre class="eval syntaxbox">
font-family:  <em>family-or-generic-name</em> [, <em>family-or-generic-name</em>]* 
</pre>
<h3 id="Values"><nobr>Values</nobr></h3>
<dl>
  <dt>
    <strong><em>family-or-generic-name</em></strong></dt>
</dl>
<p style="margin-left: 40px;">Is either a:</p>
<table class="standard-table">
  <tbody>
    <tr>
      <td><em><strong>family-name</strong></em></td>
      <td>The name of a font family. For example, "Times" and "Helvetica" are font families. Font family names containing whitespace should be quoted.</td>
    </tr>
    <tr>
      <td><em><strong>generic-family</strong></em></td>
      <td>
        <p>Generic font families are a fallback mechanism, a means of preserving some of the style sheet author's intent in case when none of the specified fonts are available. Generic family names are keywords and must not be quoted. A generic font family should be the a last alternative in the list of font family names.</p>
        <dl>
          <dt>
            <code>serif</code></dt>
          <dd style="font-family:Palatino,'Palatino Linotype',Palladio,'URW Palladio',serif">
            Glyphs have finishing strokes, flared or tapering ends, or have actual serifed endings.<br />
            E.g.&nbsp; Palatino, "Palatino Linotype", Palladio, "URW Palladio", serif</dd>
          <dt>
            <code>sans-serif</code></dt>
          <dd style="font-family:'Trebuchet MS','Liberation Sans','Nimbus Sans L',sans-serif">
            Glyphs have stroke endings that are plain.<br />
            E.g. 'Trebuchet MS', 'Liberation Sans', 'Nimbus Sans L', sans-serif</dd>
          <dt>
            <code>cursive</code></dt>
          <dd>
            Glyphs in cursive fonts generally have either joining strokes or other cursive characteristics beyond those of italic typefaces. The glyphs are partially or completely connected, and the result looks more like handwritten pen or brush writing than printed letterwork.</dd>
          <dt>
            <code>fantasy</code></dt>
          <dd>
            Fantasy fonts are primarily decorative fonts that contain playful representations of characters.</dd>
          <dt>
            <code>monospace</code></dt>
          <dd style="font-family:'DejaVu Sans Mono', Menlo, Consolas, 'Liberation Mono', Monaco, 'Lucida Console', monospace">
            All glyphs have the same fixed width.<br />
            E.g. "DejaVu Sans Mono", Menlo, Consolas, "Liberation Mono", Monaco, "Lucida Console", monospace</dd>
        </dl>
      </td>
    </tr>
  </tbody>
</table>
<h3 id="Valid_family_names">Valid family names</h3>
<p>Font family names must either be given quoted as strings, or unquoted as a sequence of one or more identifiers. This means that punctuation characters and digits at the start of each token must be escaped in unquoted font family names.</p>
<p>For example, the following declarations are valid:</p>
<pre>
font-family: Gill Sans Extrabold, sans-serif;
font-family: "Goudy Bookletter 1911", sans-serif;</pre>
<p>The following declarations are <strong>invalid</strong>:</p>
<pre>
font-family: Goudy Bookletter 1911, sans-serif;
font-family: Red/Black, sans-serif;
font-family: "Lucida" Grande, sans-serif;
font-family: Ahem!, sans-serif;
font-family: test@foo, sans-serif;
font-family: #POUND, sans-serif;
font-family: Hawaii 5-0, sans-serif;</pre>
<h2 id="Examples"><nobr>Examples</nobr></h2>
<p>{{CSSRefExampleLink("font-family")}}</p>
<pre>
body     { font-family: "Gill Sans Extrabold", Helvetica, sans-serif }

.receipt { font-family: Courier, "Lucida Console", monospace }
</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1")}}</td>
        <td>3.0</td>
        <td>3.5</td>
        <td>1.0</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 Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoMobile("1")}}</td>
        <td>6.0</td>
        <td>6.0</td>
        <td>1.0</td>
      </tr>
    </tbody>
  </table>
</div>
<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('CSS3 Fonts', '#font-family-prop', 'font-family')}}</td>
      <td>{{Spec2('CSS3 Fonts')}}</td>
      <td>No significant change.</td>
    </tr>
    <tr>
      <td>{{SpecName('CSS2.1', 'fonts.html#propdef-font-family', 'font-family')}}</td>
      <td>{{Spec2('CSS2.1')}}</td>
      <td>No significant change.</td>
    </tr>
    <tr>
      <td>{{SpecName('CSS1', '#font-family', 'font-familiy')}}</td>
      <td>{{Spec2('CSS1')}}</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="See_also">See also</h2>
<ul>
  <li>Font-related CSS properties: {{CSS_Reference:Fonts}}</li>
</ul>
Revert to this revision