HTML & CSS
/Intermediate
Font Optimization
Definition
Techniques used to ensure custom web fonts do not severely impact website performance or cause aggressive layout shifts when they finally load.
Explain Like I'm New
Custom fonts (like Google Fonts) are heavy files. When a user visits your site, their browser uses a default font (like Arial), then 2 seconds later the custom font finishes downloading, and the entire text changes shape and size. This is called the 'Flash of Unstyled Text' (FOUT), and it causes the layout to jump around wildly.
Real World Example
Using `font-display: swap` to tell the browser: 'Use Arial instantly so the user can start reading. As soon as my custom font downloads, swap it in smoothly.'
Common Use Cases
- •Improving Cumulative Layout Shift (CLS)
- •Faster text rendering
Interactive Example
Interview Questions
basic
- What CSS property dictates how a font is displayed while it is downloading?
intermediate
- Why might you 'preload' a font?