@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: "Trickster"; src: local("Trickster"), url("trickster-COLRv1.otf") format("opentype") tech(color-COLRv1), url("trickster-outline.otf") format("opentype"), url("trickster-outline.woff") format("woff"); > 

Descriptors

Defines the ascent metric for the font.

Defines the descent metric for the font.

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

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

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

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;

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

Note: The font-variant descriptor was removed from the specification in 2018. The font-variant value property is supported, but there is no descriptor equivalent.

Allows control over advanced typographic features in OpenType fonts.

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.

Defines the line gap metric for the font.

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.

Specifies references to font resources including hints about the font format and technology. It is required for the @font-face rule to be valid.

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

Description

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.

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

Browsers attempt to load resources in their list declaration order, so usually local() should be written before url() . Both functions are optional, so a rule block containing only one or more local() without url() is possible. If a more specific fonts with format() or tech() values are desired, these should be listed before versions that don’t have these values, as the less-specific variant would otherwise be tried and used first.

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.

Note: Fallback strategies for loading fonts on older browsers are described in the src descriptor page.

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

Examples

Specifying a downloadable font

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

doctype html> html lang="en-US"> head> meta charset="utf-8" /> meta name="viewport" content="width=device-width" /> title>Web Font Sampletitle> style media="screen, print"> @font-face  font-family: "Bitstream Vera Serif Bold"; src: url("https://mdn.github.io/css-examples/web-fonts/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 (both the full font name and the Postscript name 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

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jul 7, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

CSS @font-face Rule

Specify a font named «myFirstFont», and specify the URL where it can be found:

More «Try it Yourself» examples below.

Definition and Usage

With the @font-face rule, web designers do not have to use one of the «web-safe» fonts anymore.

In the @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.

To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property:

Browser Support

The @font-face rule is supported in Edge, Chrome, Firefox, Safari, and Opera.

The numbers in the table specifies the first browser version that fully supports the font format.

Font format
TTF/OTF 9.0* 4.0 3.5 3.1 10.0
WOFF 9.0 5.0 3.6 5.1 11.1
WOFF2 14.0 36.0 39.0 10.0 26.0
SVG Not supported Not supported Not supported 3.2 Not supported
EOT 6.0 Not supported Not supported Not supported Not supported

*The font format only works when set to be «installable».

Syntax

Font descriptor Values Description
font-family name Required. Defines the name of the font.
src URL Required. Defines the URL(s) where the font should be downloaded from
font-stretch normal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
Optional. Defines how the font should be stretched. Default value is «normal»
font-style normal
italic
oblique
Optional. Defines how the font should be styled. Default value is «normal»
font-weight normal
bold
100
200
300
400
500
600
700
800
900
Optional. Defines the boldness of the font. Default value is «normal»
unicode-range unicode-range Optional. Defines the range of unicode characters the font supports. Default value is «U+0-10FFFF»

More Examples

Example

You must add another @font-face rule containing descriptors for bold text:

The file «sansation_bold.woff» is another font file, that contains the bold characters for the Sansation font.

Browsers will use this whenever a piece of text with the font-family «myFirstFont» should render as bold.

This way you can have many @font-face rules for the same font.

Источник

CSS Web Fonts

Web fonts allow Web designers to use fonts that are not installed on the user’s computer.

When you have found/bought the font you wish to use, just include the font file on your web server, and it will be automatically downloaded to the user when needed.

Your «own» fonts are defined within the CSS @font-face rule.

Different Font Formats

TrueType Fonts (TTF)

TrueType is a font standard developed in the late 1980s, by Apple and Microsoft. TrueType is the most common font format for both the Mac OS and Microsoft Windows operating systems.

OpenType Fonts (OTF)

OpenType is a format for scalable computer fonts. It was built on TrueType, and is a registered trademark of Microsoft. OpenType fonts are used commonly today on the major computer platforms.

The Web Open Font Format (WOFF)

WOFF is a font format for use in web pages. It was developed in 2009, and is now a W3C Recommendation. WOFF is essentially OpenType or TrueType with compression and additional metadata. The goal is to support font distribution from a server to a client over a network with bandwidth constraints.

The Web Open Font Format (WOFF 2.0)

TrueType/OpenType font that provides better compression than WOFF 1.0.

SVG Fonts/Shapes

SVG fonts allow SVG to be used as glyphs when displaying text. The SVG 1.1 specification define a font module that allows the creation of fonts within an SVG document. You can also apply CSS to SVG documents, and the @font-face rule can be applied to text in SVG documents.

Embedded OpenType Fonts (EOT)

EOT fonts are a compact form of OpenType fonts designed by Microsoft for use as embedded fonts on web pages.

Browser Support for Font Formats

The numbers in the table specifies the first browser version that fully supports the font format.

Font format
TTF/OTF 9.0* 4.0 3.5 3.1 10.0
WOFF 9.0 5.0 3.6 5.1 11.1
WOFF2 14.0 36.0 39.0 10.0 26.0
SVG Not supported Not supported Not supported 3.2 Not supported
EOT 6.0 Not supported Not supported Not supported Not supported

*IE: The font format only works when set to be «installable».

Using The Font You Want

In the @font-face rule; first define a name for the font (e.g. myFirstFont) and then point to the font file.

Tip: Always use lowercase letters for the font URL. Uppercase letters can give unexpected results in IE.

To use the font for an HTML element, refer to the name of the font (myFirstFont) through the font-family property:

Example

@font-face <
font-family: myFirstFont;
src: url(sansation_light.woff);
>

div font-family: myFirstFont;
>

Using Bold Text

You must add another @font-face rule containing descriptors for bold text:

Example

The file «sansation_bold.woff» is another font file, that contains the bold characters for the Sansation font.

Browsers will use this whenever a piece of text with the font-family «myFirstFont» should render as bold.

This way you can have many @font-face rules for the same font.

CSS Font Descriptors

The following table lists all the font descriptors that can be defined inside the @font-face rule:

Descriptor Values Description
font-family name Required. Defines a name for the font
src URL Required. Defines the URL of the font file
font-stretch normal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
Optional. Defines how the font should be stretched. Default is «normal»
font-style normal
italic
oblique
Optional. Defines how the font should be styled. Default is «normal»
font-weight normal
bold
100
200
300
400
500
600
700
800
900
Optional. Defines the boldness of the font. Default is «normal»
unicode-range unicode-range Optional. Defines the range of UNICODE characters the font supports. Default is «U+0-10FFFF»

Источник

@font-face

Правило @font-face позволяет определить настройки шрифтов, а также загрузить специфичный шрифт на компьютер пользователя.

Синтаксис

Значения

Внутри конструкции @font-face может находиться набор свойств для изменения параметров шрифта ( font-family , font-size , font-style и др.), а также ссылка на шрифтовой файл. Ссылка записывается в виде src: url(URI) , где URI — относительный или абсолютный путь к файлу.

HTML5 CSS2.1 CSS3 IE Cr Op 11 Op 12 Sa Fx

     @font-face < font-family: Pompadur; /* Имя шрифта */ src: url(fonts/pompadur.ttf); /* Путь к файлу со шрифтом */ >P 

Протяженность варьирует дорийский микрохроматический интервал, но если бы песен было раз в пять меньше, было бы лучше для всех.

Результат данного примера в браузере Safari показан на рис. 1.

Собственный шрифт на странице

Рис. 1. Собственный шрифт на странице

Браузеры

Браузер Internet Explorer до версии 9.0 поддерживает только шрифты формата EOT (Embedded OpenType).

Opera в некоторых случаях может не показывать на веб-странице текст выбранным шрифтом, заменяя его стандартным. Причём для локальных документов всё работает корректно. Это происходит в тех случаях, когда имя пользователя в Windows написано кириллицей.

Источник

Читайте также:  Trim var in javascript
Оцените статью