CKTIS / CSS
format() in the @font-face src descriptor
`format()` identifies the expected format of one font source so the browser can skip an unsupported source without fetching its data.
Version: Current CSS specifications; browser support varies by feature
Syntax
src = <font-src>#
<font-src> = <url> [ format( <font-format> ) ]? [ tech( <font-tech># ) ]? | local( <font-family-name> )
<font-format> = <string> | collection | embedded-opentype | opentype | svg | truetype | woff | woff2In @font-face, the src descriptor lists font sources in order of preference, separated by commas. The optional format() hint follows url() and applies only to that URL. It does not follow local(), which names a locally available font face.
format() takes one argument. The specification prefers keywords such as format(woff2) but also accepts equivalent legacy strings such as format("woff2") for backward compatibility. Syntax support can vary by browser. tech() is a separate hint for a required font technology; if you use both functions, put tech() after format().
For a src list the browser accepts, the specification calls for skipping sources with unknown or unsupported format hints without fetching their data. Some browsers may instead reject the entire src descriptor when they encounter an unrecognized hint. WOFF2 decoding support alone does not guarantee that the browser will reach a later WOFF2 source in that list. Without a format hint, the browser inspects the data when it reaches that source. The hint neither converts the file nor guarantees that its data is valid or that the fetch will succeed, for example if CORS blocks it.
Put the preferred source first in one src descriptor. If it cannot be used, the browser moves on to the next source it can try. Separately, specify a fallback family in the font-family property in case no source works.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Font format hint demo</title>
<style>
@font-face {
font-family: "CktisFormatDemo";
/* Keep the WOFF2 URL available if the next declaration is invalid. */
src: url("data:font/woff2,second-invalid-payload") format("woff2");
src: url("data:font/woff2,first-invalid-payload") format("cktis-unknown-format"),
url("data:font/woff2,second-invalid-payload") format("woff2");
}
.demo {
font-family: "CktisFormatDemo", monospace;
}
</style>
<body>
<div class="demo">Format demo</div>
<div id="result" role="status"></div>
<script type="module">
const demo = document.querySelector('.demo');
const result = document.querySelector('#result');
const src = document.styleSheets[0].cssRules[0].style.getPropertyValue('src');
const loadFailed = await document.fonts.load('16px "CktisFormatDemo"', demo.textContent)
.then(() => false, () => true);
await document.fonts.ready;
const families = getComputedStyle(demo).fontFamily.split(',').map(name => name.trim());
const fallbackIsMonospace = families.length === 2 &&
families[0].replace(/["']/g, '') === 'CktisFormatDemo' &&
families[1] === 'monospace';
result.textContent = src.includes('second-invalid-payload') && loadFailed && fallbackIsMonospace
? 'CktisFormatDemo failed to load; using monospace.'
: 'Unexpected result: check the src descriptor, font load, and fallback family.';
</script>
</body>
</html>Result
Format demo CktisFormatDemo failed to load; using monospace.
Both `data:` URLs deliberately contain invalid data. The earlier `src` declaration keeps the WOFF2 URL available if the browser rejects the later list during parsing. `CktisFormatDemo` fails to load, and the text in `.demo` renders using `monospace`. This result does not show which sources the browser tried to fetch.
Related lessons
- CSS · Lesson 14 · Advanced · Version: 2
- CSS · Lesson 15 · Advanced · Version: 2