@font-face
Quick Summary for @font-face
The @font-face CSS at-rule 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.
Code Usage for @font-face
@font-face {   font-family: "Open Sans";   src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),        url("/fonts/OpenSans-Regular-webfont.woff") format("woff"); } 
More Details for @font-face

@font-face

The @font-face CSS at-rule 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.

Syntax

@font-face {   font-family: "Open Sans";   src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),        url("/fonts/OpenSans-Regular-webfont.woff") format("woff"); } 

Descriptors

ascent-override

Defines the ascent metric for the font.

descent-override

Defines the descent metric for the font.

font-display

Determines how a font face is displayed based on whether and when it is downloaded and ready to use.

font-family

Specifies a name that will be used as the font face value for font properties.

font-stretch

A font-stretch value. Accepts two values to specify a range that is supported by a font-face, for example font-stretch: 50% 200%;

font-style

A font-style value. Accepts two values to specify a range that is supported by a font-face, for example font-style: oblique 20deg 50deg;

font-weight

A font-weight value. Accepts two values to specify a range that is supported by a font-face, for example font-weight: 100 400;

font-variant

A font-variant value.

font-feature-settings

Allows control over advanced typographic features in OpenType fonts.

font-variation-settings

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.

line-gap-override

Defines the line gap metric for the font.

size-adjust

Defines a multiplier for glyph outlines and metrics associated with this font. This makes it easier to harmonize the designs of various fonts when rendered at the same font size.

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.

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 format() function:

src: url(ideal-sans-serif.woff) format("woff"),      url(basic-sans-serif.ttf) format("truetype"); 

The available types are: "woff", "woff2", "truetype", "opentype", "embedded-opentype", and "svg".

unicode-range

The range of Unicode code points to be used from the font.

Description

If the local() function is provided, specifying a font name to look for on the user's computer, and the user agent finds a match, that local font is used. Otherwise, the font resource specified using the url() function is downloaded and used.

By allowing authors to provide their own fonts, @font-face 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.

It's common to use both url() and local() 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.

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.

Font MIME Types

Format MIME type
TrueType font/ttf
OpenType font/otf
Web Open Font Format font/woff
Web Open Font Format 2 font/woff2

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. @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;   } } 

Formal syntax

@font-face {   [ font-family: <family-name>; ] ||   [ src: <src>; ] ||   [ unicode-range: <unicode-range>; ] ||   [ font-variant: <font-variant>; ] ||   [ font-feature-settings: <font-feature-settings>; ] ||   [ font-variation-settings: <font-variation-settings>; ] ||   [ font-stretch: <font-stretch>; ] ||   [ font-weight: <font-weight>; ] ||   [ font-style: <font-style>; ] ||   [ size-adjust: <size-adjust>; ] ||   [ ascent-override: <ascent-override>; ] ||   [ descent-override: <descent-override>; ] ||   [ line-gap-override: <line-gap-override>; ] }

where <family-name> = <string> | <custom-ident>+

Examples

Specifying a downloadable font

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

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

The output of this example code looks like so:

Specifying local font alternatives

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

Specifications

Specification
CSS Fonts Module Level 4 # font-face-rule

See also

About WOFF FontSquirrel @font-face generator Beautiful fonts with @font-face Font Library

Last modified: Sep 20, 2021, by MDN contributors

Select your preferred language English (US)EspañolFrançais日本語한국어Português (do Brasil)Русский中文 (简体)正體中文 (繁體) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
“We ought to take outdoor walks in order that the mind may be strengthened and refreshed by the open air and much breathing.".
Seneca
Random CSS Property

<length>

The <length> CSS data type represents a distance value. Lengths can be used in numerous CSS properties, such as width, height, margin, padding, border-width, font-size, and text-shadow.
length#cap css reference