Using Font Face In CSS

Master custom web typography with @font-face. Learn to embed, optimize, and deliver professional fonts for modern websites.

What Is @font-face in CSS?

Typography defines how your content feels. While system fonts like Arial or Times New Roman have served the web for decades, modern websites demand visual distinction. The @font-face CSS at-rule unlocks that creative potential by letting you embed custom fonts directly into your projects, transforming generic layouts into memorable brand experiences.

The @font-face 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. This powerful CSS feature has been part of the specification since CSS2 and gained universal browser support by 2015, making it a reliable tool for modern web development.

Before @font-face, web designers were limited to a small set of "web-safe" fonts. This constraint led to visually homogeneous websites. The @font-face rule changed everything by allowing developers to define and use fonts that are not installed on the user's system, opening unlimited typographic possibilities.

Basic @font-face Declaration
1@font-face {2 font-family: "CustomFont";3 src: url("fonts/customfont.woff2") format("woff2"),4 url("fonts/customfont.woff") format("woff");5 font-weight: normal;6 font-style: normal;7}8 9/* Using the custom font */10body {11 font-family: "CustomFont", sans-serif;12}

Essential @font-face Descriptors

Understanding the various descriptors within @font-face gives you fine-grained control over how fonts load and render. Each descriptor serves a specific purpose, and mastering them is key to professional typography implementation.

font-family

The font-family descriptor specifies a name that will be used as the font face value for font properties. A font-family name is required for the @font-face rule to be valid. This name acts as an identifier that you reference throughout your CSS when applying the font to elements.

src

The src descriptor specifies references to font resources including hints about the font format and technology. A src is required for the @font-face rule to be valid. You can combine local() references to system fonts with url() references to hosted font files.

font-weight and font-style

These descriptors define the weight and style characteristics of the font file being loaded. The font-weight accepts values from 100 to 900, with normal mapping to 400 and bold to 700. The font-style accepts normal, italic, or oblique values.

font-display

The font-display descriptor determines how a font face is displayed based on whether and when it is downloaded and ready to use. This property significantly impacts perceived performance and user experience. The swap value is commonly recommended because it shows fallback text immediately and swaps to the custom font once loaded.

Key @font-face Descriptors

Master these descriptors for professional font implementation

font-family

The name used to reference the font in your CSS styles. Required for valid @font-face rules.

src

Specifies font resource locations using url() for hosted fonts and local() for system fonts.

font-weight

Defines the font weight (100-900). Separate declarations needed for different weights.

font-style

Specifies normal, italic, or oblique styles for the font face.

font-display

Controls font loading behavior: auto, block, swap, fallback, or optional.

unicode-range

Limits which Unicode characters the font file covers for optimized loading.

Font Formats and Browser Compatibility

Modern web development primarily relies on two font formats that provide excellent compression and universal browser support.

WOFF2 Format

Web Open Font Format 2 (WOFF2) is the modern, highly compressed format designed specifically for the web. It offers the best compression, typically 30% smaller than WOFF, resulting in faster page loads. All major browsers support WOFF2, making it the preferred format.

WOFF Format

Web Open Font Format (WOFF) serves as an older compressed format that provides good browser support as a fallback for browsers that do not support WOFF2.

Deprecated Formats

TrueType (TTF) and OpenType (OTF) are traditional desktop font formats with larger file sizes. EOT was used exclusively for Internet Explorer and is no longer relevant. SVG fonts were for older iOS devices and are deprecated.

Modern projects should focus on WOFF2 as the primary format with WOFF as a fallback.

Web Font Format Comparison
FormatCompressionBrowser SupportUse Case
WOFF2Best (30% smaller than WOFF)All modern browsersPrimary format for modern web
WOFFGoodAll browsersFallback for older browsers
TTF/OTFNone (larger files)All browsersDesktop fonts, use sparingly
EOTLimitedInternet Explorer onlyDeprecated, avoid using
SVGN/AOlder iOS devicesDeprecated, avoid using

Performance Optimization for Custom Fonts

Custom fonts add visual appeal but can impact page performance if not properly optimized. Implementing best practices ensures your typography enhances rather than hinders the user experience. Slow-loading fonts affect Core Web Vitals metrics like Largest Contentful Paint (LCP), which is a factor in search rankings.

Preloading Critical Fonts

For fonts that appear above the fold or are essential to initial page render, use the preload link relation to prioritize their loading.

Subsetting Fonts

Many fonts include character sets for multiple languages that your website may not need. Subsetting reduces file sizes by including only the characters your content actually uses.

Font Loading Strategies

The font-display descriptor combined with proper CSS fallback stacks creates robust font loading behavior. Always define fallback fonts that closely match your custom font's characteristics to minimize visual shifts when fonts swap.

Preloading Critical Fonts
1<!-- Add to <head> for critical fonts -->2<link rel="preload" href="/fonts/main-font.woff2" 3 as="font" type="font/woff2" crossorigin="anonymous">4 5/* CSS with proper fallback stack */6@font-face {7 font-family: "BrandFont";8 src: url("brand-font.woff2") format("woff2");9 font-display: swap;10}11 12body {13 font-family: "BrandFont", "FallbackFont", system-ui, sans-serif;14}

Variable Fonts: The Modern Approach

Variable fonts represent a significant advancement in web typography, allowing multiple variations of a typeface to be contained in a single font file. This technology dramatically reduces the number of font files needed while providing unprecedented design flexibility.

Instead of loading separate font files for each weight and style, a variable font contains all variations in a single optimized file. You define a range of acceptable values, and the browser renders the appropriate variation based on your CSS declarations.

The weight axis (wght) allows any value between the minimum and maximum, enabling smooth gradations from thin to extra bold. The slant axis (slnt) provides control over italic angles without requiring separate font files. For fonts that support them, axes like width (wdth), optical size (opsz), and custom axes offer even more typographic control.

Variable Font Implementation
1@font-face {2 font-family: "VariableRoboto";3 src: url("roboto-variable.woff2") format("woff2");4 font-weight: 100 900;5 font-style: normal italic;6}7 8h1 {9 font-family: "VariableRoboto", sans-serif;10 font-weight: 750; /* Extra bold */11}12 13p {14 font-family: "VariableRoboto", sans-serif;15 font-weight: 400; /* Regular */16}17 18em {19 font-family: "VariableRoboto", sans-serif;20 font-style: italic; /* Uses italic axis */21}

Advanced Descriptors for Fine Control

Beyond the basic descriptors, @font-face provides several advanced options for optimizing font rendering and behavior.

Ascent and Descent Overrides

The ascent-override and descent-override descriptors adjust how fonts align with the baseline, which is particularly useful when fonts from different foundries have inconsistent vertical metrics.

Size Adjust

The size-adjust descriptor defines a multiplier for glyph outlines and metrics, making it easier to harmonize the designs of various fonts when rendered at the same font size.

Font Feature Settings

For fonts with OpenType features, font-feature-settings provides access to advanced typographic capabilities like ligatures, small caps, old-style figures, and discretionary ligatures.

Common Mistakes and Debugging

Even experienced developers encounter issues with custom fonts. Understanding common pitfalls helps you avoid them and troubleshoot effectively.

Cross-Origin Restrictions

Web fonts are subject to the same domain restriction unless HTTP access controls (CORS) are used. When hosting fonts on a CDN, ensure proper CORS headers are configured.

Mismatched Font Weights

A frequent issue occurs when you define a font with one weight in @font-face but apply a different weight in your CSS. The browser will synthetically bold or fail to render the intended style.

Forgetting Font Styles

Similar to weight mismatches, forgetting to define italic variants means browsers must artificially slant fonts, which looks unprofessional.

Ignoring Layout Shift

Fonts that load after initial render can cause content to reflow as fallback fonts are replaced. Mitigate this by using appropriate font-display values and sizing fallback fonts to match.

Best Practices for Production

Implementing custom fonts in production requires attention to caching, delivery, and fallback strategies that ensure consistent, performant experiences. Optimized typography is a key component of professional web development services that deliver both aesthetic appeal and technical excellence.

Host fonts on a CDN for global distribution with low latency. Configure proper cache headers while implementing cache-busting through filename versioning when fonts update.

Test across different browsers, devices, and network conditions. Use browser DevTools to monitor font loading behavior and identify issues with loading order or fallback behavior.

Organize your @font-face declarations in a dedicated CSS file loaded before any styles that use the fonts. This ensures fonts are defined before they are referenced.

Production-Ready @font-face Setup
1/* fonts.css - Load this early in your CSS cascade */2 3/* Base regular weight */4@font-face {5 font-family: "Brand";6 src: url("brand-regular.woff2") format("woff2");7 font-weight: 400;8 font-style: normal;9 font-display: swap;10}11 12/* Bold weight */13@font-face {14 font-family: "Brand";15 src: url("brand-bold.woff2") format("woff2");16 font-weight: 700;17 font-style: normal;18 font-display: swap;19}20 21/* Italic variant */22@font-face {23 font-family: "Brand";24 src: url("brand-italic.woff2") format("woff2");25 font-weight: 400;26 font-style: italic;27 font-display: swap;28}

Frequently Asked Questions

What is the difference between @font-face and Google Fonts?

@font-face is the CSS mechanism for defining custom fonts, while Google Fonts is a service that provides hosted font files. Google Fonts uses @font-face internally. You can self-host the same fonts or use @font-face with your own font files.

Does @font-face work in all browsers?

Yes, @font-face has universal browser support since 2015. Modern formats like WOFF2 are supported in all major browsers (Chrome, Firefox, Safari, Edge). Include WOFF as a fallback for maximum compatibility.

How do I optimize font file size?

Use WOFF2 format for best compression, subset fonts to include only needed characters, remove unused glyphs and OpenType features, and consider variable fonts to replace multiple font files with one.

What causes Flash of Invisible Text (FOIT)?

FOIT occurs when the browser hides text while waiting for a custom font to load. Use font-display: swap to show fallback text immediately instead of invisible text during loading.

Can I use @font-face with local fonts?

Yes, use the local() function within the src descriptor to reference system fonts. This allows you to prefer local installations when available and fall back to downloaded fonts otherwise.

Need Custom Web Typography for Your Project?

Our team builds performance-optimized websites with professional typography implementation.

Sources

  1. MDN Web Docs - @font-face - Official web standard reference
  2. CSS-Tricks - @font-face Almanac - Comprehensive descriptor reference
  3. Penpot Blog - Custom Fonts Guide - Best practices for performance