Using font-display: optional for Zero Shift
You'll learn how to use font-display: optional to completely eliminate font-related layout shift by rendering text with system fonts immediately when custom fonts aren't already cached.
The Core Idea
font-display: optional is the most aggressive strategy for avoiding layout shift. It tells the browser: "If the custom font isn't already cached, just use the system fallback font and don't swap later." This means zero visual disruption — the page renders once and stays that way.
Think of it like a strict dress code at a restaurant: if you show up without the right outfit (font isn't cached), you eat in the casual dining area (system font) for your entire meal. No changing mid-dinner.
How It Works
When you set font-display: optional in your @font-face rule, the browser gets a tiny window (around 100ms) to check if the font is already available. If it is, great — use it immediately. If not, the browser uses the fallback font and never swaps to the custom font, even after it finishes downloading.
@font-face {
font-family: 'CustomFont';
src: url('/fonts/customfont.woff2') format('woff2');
font-display: optional;
}
body {
font-family: 'CustomFont', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}
When to Use This
Use font-display: optional when zero layout shift is more important than always showing your custom font. Perfect for body text where readability matters more than brand consistency. First-time visitors see system fonts; repeat visitors with cached fonts see your custom typeface.
Key Takeaway: font-display: optional guarantees zero layout shift by using system fonts immediately if custom fonts aren't cached, prioritizing stability over brand consistency.