CKTIS / CSS
The font-display descriptor: swap in @font-face
The `swap` value keeps the invisible-text period brief and sets no deadline for a successfully loaded web font to replace the visible fallback.
Version: Current CSS specifications; browser support varies by feature
Syntax
@font-face {
font-family: "DemoWeb";
src: url("demo.woff2") format("woff2");
font-display: swap;
}“Deskryptor font-display” is Polish for the font-display descriptor. In an @font-face rule, it controls how text is rendered while a particular font face loads. It is not a property on elements: use font-family to select families for an element. The timer starts when the browser first tries to use the face, not necessarily when the download begins.
block period. While the target face is still pending, the browser lays out the text using an available fallback’s metrics but does not paint the glyphs. The text can take up space without being visible. If the target face loads during this period, the browser uses it. With swap, this period is very short: the draft recommends 100 ms or less in most cases, but neither an exact duration nor a visible blank interval is guaranteed.
swap period. If the target face is still pending, the browser paints the text in a visible fallback. It looks for the first already available face in the font-family list without initiating downloads of fallback fonts for this step. monospace is a generic family, not a particular font. When the target face loads, the browser uses it; different font metrics can change line breaks or shift the layout.
failure period. If a finite swap period ends before the face is ready, the browser treats the load as failed for rendering and uses normal font fallback. The swap value sets no such deadline: a font is not ruled out just because it takes a long time to load. It does not, however, prevent real failures such as an invalid font file.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Font display demo</title>
<style>
@font-face {
font-family: "DemoWeb";
src: url("data:font/woff2;base64,AAAA") format("woff2");
font-display: swap;
}
.demo {
font-family: "DemoWeb", monospace;
}
</style>
</head>
<body>
<p class="demo">Font display demo</p>
</body>
</html>Result
Expected: “Font display demo” is ultimately rendered in the browser’s generic monospace fallback. The embedded data is not a valid font, so no swap to DemoWeb occurs. The exact fallback face and whether a brief blank interval is visible are not fixed.
The embedded `src` data is deliberately invalid: the offline example shows the final `monospace` fallback, not a successful swap. To see a swap, use a valid same-origin WOFF2 file and throttle its request with the cache disabled.
Related lessons
- CSS · Lesson 15 · Advanced · Version: 2