CSS @font-face At-Rule

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The @font-face at-rule registers a custom font face for your page. You give it a family name and a source, then use that name with font-family.

Sources can be a file URL (url()) or a font installed on the device (local()). Prefer WOFF2 for downloaded fonts. Always keep a fallback stack so text stays readable if the file fails.

Baseline Widely available.

Standard CSS Fonts feature. Required descriptors: font-family and src. Optional helpers include font-weight, font-style, and font-display.

CSS Syntax

syntax.css
@font-face {
  font-family: <family-name>;
  src: <url> | <local> [, ...];
  /* optional: font-weight, font-style, font-display, ... */
}

List preferred sources first. Browsers try them in order.

Common Descriptors

DescriptorDescriptionTry it
font-familyRequired name used later in CSSTry it
srcRequired local() and/or url() sourcesTry it
font-weightMaps a file to a weight such as 400 or 700Try it
font-styleMaps a file to normal or italicTry it
font-displayLoading behavior; swap is a common choiceTry it
apply with stackUse the name plus fallbacks in font-familyTry it

How to Use @font-face

Register the face, then apply the family name.

  1. Add an @font-face block with font-family and src.
  2. Prefer url("font.woff2") format("woff2") for web delivery (demos below use local() so they work offline).
  3. Optionally set font-weight, font-style, and font-display: swap.
  4. Use the same family name in font-family, with system fallbacks after it.
PieceRequired?Role
font-family descriptorYesName you will reference later
srcYesWhere the font data comes from
font-family propertyYesApplies the face to elements
Fallback fontsStrongly recommendedKeeps text readable if loading fails

Live Preview

This demo registers a study family with local() (no download). Switch the applied family and compare the look.

StudySerif and StudySans are defined with @font-face + local() in shared tutorial CSS.

Sample text

The quick brown fox

Custom faces start with @font-face, then font-family uses the same name.

Using StudySerif from @font-face

CSS Examples

Each snippet focuses on @font-face. Live demos use local() so they work without font files. Production sites usually use url(...woff2).

Example 1 — Basic Custom Family

Register a name, then apply it with a fallback stack.

CSS
@font-face {
  font-family: "StudySerif";
  src: local("Georgia");
}

h1 {
  font-family: "StudySerif", Georgia, serif;
}
Try It Yourself

How It Works

  1. Name the face. font-family: "StudySerif" is the public name.
  2. Point to a source. local("Georgia") uses an installed font for this demo.
  3. Apply the name. Elements use the same string in font-family.
  4. Keep fallbacks. Georgia, serif cover missing faces.

Example 2 — local() then url()

Try a local font first; download WOFF2 if needed.

CSS
@font-face {
  font-family: "BrandSans";
  src:
    local("Segoe UI"),
    local("Helvetica Neue"),
    url("/fonts/brand-sans.woff2") format("woff2");
  font-display: swap;
}

p {
  font-family: "BrandSans", system-ui, sans-serif;
}
Try It Yourself

How It Works

  1. Order matters. The browser walks the src list from left to right.
  2. Local first. Avoids a download when the font is already installed.
  3. WOFF2 next. format("woff2") helps browsers skip unsupported files.
  4. Use swap. font-display: swap keeps text visible while loading.

Example 3 — Separate Weight Faces

One family name, two files mapped with font-weight.

CSS
@font-face {
  font-family: "StudySans";
  src: local("Arial");
  font-weight: 400;
}

@font-face {
  font-family: "StudySans";
  src: local("Arial Bold"), local("Arial");
  font-weight: 700;
}

h2 {
  font-family: "StudySans", Arial, sans-serif;
  font-weight: 700;
}
Try It Yourself

How It Works

  1. Reuse the family name. Both faces share "StudySans".
  2. Tag each file. font-weight: 400 and 700 pick the right face.
  3. Style with weight. font-weight: 700 selects the bold face.
  4. Avoid fake bold. Mapping real files looks cleaner than synthetic bolding.

Edge Cases & Pitfalls

Most font bugs are naming, path, or missing-fallback issues.

required

Need both font-family and src

Without those two descriptors, the @font-face rule is invalid.

name

Names must match exactly

The string in @font-face and in font-family must match, including quotes and spelling.

path

Check the font URL

A wrong path or CORS block means the browser falls back to the next font in the stack.

fof

Flash of invisible/unstyled text

Use font-display: swap (or similar) so users see text while the file loads.

license

Respect font licenses

Only host fonts you are allowed to redistribute on the web.

Browser Support

@font-face is a browser CSS feature for loading font faces. Node.js, Deno, and Bun do not apply it as a layout API.

✓ Baseline · Widely available since 2015

@font-face

Supported in Chrome, Firefox, Safari, Edge, Opera, and Samsung Internet. WOFF2 is supported in all modern browsers.

98% Modern browsers
Google Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer 9+
Partial (older formats)
@font-face Widely supported

Bottom line: Safe for modern sites with WOFF2. Prefer font-display: swap and always include fallbacks. Not a server runtime API.

Key Takeaways

  • Role: register a custom font face with a name and a source.
  • Required: font-family + src. Prefer WOFF2 for downloads.
  • Apply: use the same name in the font-family property with fallbacks.
  • Tip: font-display: swap keeps text visible while the font loads.

One line: @font-face names a font source; font-family applies that name to your text.

Frequently Asked Questions

It registers a custom font face. You set a font-family name and a src (local or url). Then use that name with the font-family property like any other font.
font-family and src are required for a valid @font-face rule. Other descriptors such as font-weight, font-style, and font-display are optional.
WOFF2 is the best default for modern browsers. You can list older formats after it so more browsers have a fallback file.
It shows fallback text right away, then swaps to the custom font when the file is ready. That keeps text readable during loading.
Browsers try sources in order. Put local() first to use an installed font when available, then url() to download a web font if needed.
It is Baseline and widely available since 2015 in Chrome, Firefox, Safari, Edge, and Opera. It is a browser CSS feature, not a Node.js API.

Next: @import

Learn how to pull other stylesheets into your CSS file.

@import tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful