@font-face
Registers a typeface under a chosen name that you then use in font-family. It lets you use fonts from your own site’s files.
Example
#@font-face {
font-family: "Lora";
src: url("/fonts/lora-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Lora";
src: url("/fonts/lora-bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
body {
font-family: "Lora", Georgia, serif;
}The files live in the /fonts directory on your server. This example has no preview because the frame does not contain such files. You will find working variants with system fonts below, in the descriptors section.
Definition and usage
#The @font-face rule tells the browser: under this name sits this font file. On its own it changes nothing on the page. Only when an element has that name in font-family does the browser download the file and draw the text with it, so unused fonts do not weigh the page down.
The two most important descriptors are font-family, the name you make up yourself, and src, the list of sources. The browser checks the sources in order and takes the first one it can use: local() looks for a font installed on the system, and url() points to a file on the server, ideally in WOFF2, which is the most strongly compressed format.
One rule describes one variant of a typeface. If you have separate files for regular, bold, and italic, you write several rules with the same name and different font-weight and font-style. The browser picks the right file for <strong> or <em> on its own, and when a variant is missing, it fakes it by artificially emboldening or slanting the letters.
The font-display descriptor decides what is visible before the file downloads. The swap value shows the text immediately in a fallback font and swaps it after loading, so the content does not disappear. That is why it is always worth providing a good fallback font in font-family, e.g. "Lora", Georgia, serif.
Syntax
#@font-face {
font-family: "Name";
src: local("System font"), url("/fonts/file.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}Descriptors
#"Papirus"
The typeface name you will use in the ordinaryfont-familyproperty. You make it up yourself: here “Papirus” is simply Georgia under a new name.local(), url()
A list of sources checked in order. The first font is not on the system, so the browser takes the next one, Courier New. In a real project the list usually has aurl()with a WOFF2 file.400, 700
Says what weight the file has. Here the 700 version deliberately points to a different typeface, so you can see that the bold part inside<strong>comes from the second rule. In a real project both files belong to the same family.normal, italic
Marks an italic file. The part inside<em>uses the second rule, and for bold, which has no separate file, the browser fakes it from the regular version.swap
Decides what to show before the font loads.swapimmediately displays the text in a fallback font, whileoptionaluses the new font only if it loads almost instantly. A local font is available right away, so the preview shows no difference.U+0030-0039
The range of characters the font should be used for. Here only the digits (U+0030-0039) come from Courier New, and the rest of the text from Georgia. The browser downloads a file with such a range only when a character from the range appears on the page.
Descriptors in practice
#font-family
font-family property. You make it up yourself: here “Papirus” is simply Georgia under a new name.@font-face {
font-family: "Papirus";
src: local("Georgia"),
local("Times New Roman");
}
.demo { font-family: "Papirus", serif; }src
url() with a WOFF2 file.@font-face {
font-family: "Kod";
src: local("Brak Takiego Fontu"),
local("Courier New"), local("Courier");
}
.demo { font-family: "Kod", monospace; }font-weight
<strong> comes from the second rule. In a real project both files belong to the same family.@font-face {
font-family: "Papirus";
src: local("Georgia");
font-weight: 400;
}
@font-face {
font-family: "Papirus";
src: local("Courier New Bold"),
local("Courier New");
font-weight: 700;
}
.demo { font-family: "Papirus", serif; }font-style
<em> uses the second rule, and for bold, which has no separate file, the browser fakes it from the regular version.@font-face {
font-family: "Papirus";
src: local("Georgia");
}
@font-face {
font-family: "Papirus";
src: local("Courier New Italic"),
local("Courier New");
font-style: italic;
}
.demo { font-family: "Papirus", serif; }font-display
swap immediately displays the text in a fallback font, while optional uses the new font only if it loads almost instantly. A local font is available right away, so the preview shows no difference.@font-face {
font-family: "Papirus";
src: local("Georgia");
font-display: swap;
}
.demo { font-family: "Papirus", serif; }unicode-range
@font-face {
font-family: "Cyfry";
src: local("Courier New");
unicode-range: U+0030-0039;
}
.demo { font-family: "Cyfry", Georgia, serif; }More examples
#@font-face {
font-family: "Inter";
src: url("/fonts/inter-variable.woff2") format("woff2");
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
h1 {
font-family: "Inter", system-ui, sans-serif;
font-weight: 800;
}The 100 900 range says that a single file supports every weight in that interval. This is a so-called variable font: instead of several files, the browser downloads one.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Provide fonts in WOFF2 format. Older formats such as TTF or EOT are unnecessary today and much heavier.
- Load only the weights and styles you really use. Each variant is a separate file to download.
- You can fetch the most important font earlier: add
<link rel="preload" href="/fonts/lora-regular.woff2" as="font" type="font/woff2" crossorigin>in<head>.
Related entries
#- font-familyPicks a font, with a list of fallback fonts in case the first one is unavailable.
- font-weightSets the font weight, from thin to extra bold.
- font-styleTurns on italics or an oblique slant for text.
- fontShorthand that sets font style, weight, size, line height, and family all at once.
- @importIncludes the contents of another CSS stylesheet in the current one. It can place it straight into a layer or load it only for chosen media.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.