Fonts and shadow DOM
Learn why @font-face in shadow DOM is ignored, how web components load fonts from the document, and how to use icon fonts and font fallbacks.
- 01Explain where a font must be declaredSay why an
@font-facerule inside a shadow root does nothing in Chrome and Firefox, and what Safari does with it. - 02Load a font for a componentDeclare it in the page, adopt it in the document, or add one
FontFacefrom the component, once. - 03Use icon fonts and fallbacksPut an icon font's
@font-facein the page and its classes in the component, and give every font a fallback stack.
Why is @font-face ignored in shadow DOM?
Leaf and Kettle has a new font for its menu. It's a handwriting font called Kettle Hand, like the chalk on the shop's board. Ravi looks after the shop's design. He wants every tea card to bring the font with it. So he links the font's stylesheet inside the card's own template.
He opens the menu. The tea names are in a plain serif font. The stylesheet loaded. Why didn't the font?
In Chrome and Firefox, an @font-face rule inside a shadow root does nothing. It never joins the page's list of fonts, so the text falls back to another font. Declare the font in the document instead. Then any shadow root can use it by name.
Lesson 6.4 met this problem first. A web font needs two parts. An @font-face rule gives the font a name and says where its file is. A font-family property uses that name. Here is Ravi's card. Its template becomes the card's shadow root:
<!-- The page itself loads no fonts. --><template id="tea-card"> <link rel="stylesheet" href="kettle-hand.css"> <style>h3 { font-family: "Kettle Hand", serif; }</style> <h3>Green tea</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> console.log("Fonts the page knows:", document.fonts.size);</script>- Line 1 is a comment. The page itself loads no fonts.
- Lines 2 to 6 are the card's template. Line 3 links
kettle-hand.css, the file with the@font-facerule. - Line 4 asks for Kettle Hand for the card's heading, and serif if Kettle Hand is missing.
- In the
tea-card.jstab, line 4 finds the template. Line 5 copies it into the card's shadow root. - Line 11 prints how many fonts are in
document.fonts, the page's font list.
It prints Fonts the page knows: 0. The heading shows the browser's serif font. Kettle Hand never joined the list, so the browser never used it.
Open the kettle-hand.css tab to see the rule. On a real site, it looks like this:
@font-face {
font-family: "Kettle Hand";
src: url("/fonts/kettle-hand.woff2") format("woff2");
}- Line 2 gives the font its name, Kettle Hand. CSS uses this name, not the file's name.
- Line 3 says where the file is.
woff2is the usual format for web fonts.
In the tab, src holds the whole font file as a data: URL instead. A data: URL carries a file inside the address itself. So the example runs without a server. Every live example in this lesson carries its font this way.
@font-face rules to document.fonts, the page's font list. The rule in the card's shadow root never gets there, so the heading falls back to serif.Leaf and Kettle orders everything through its front desk. The desk keeps one list of suppliers. One day, the kitchen pins a new supplier's card inside its own cupboard. The desk never sees it, so nobody places the order. The kitchen makes do with what it has.
- In real life: The front desk's list of suppliers
- In JavaScript:
document.fonts, the page's font list - In real life: A supplier on the desk's list
- In JavaScript: An
@font-facerule in the document - In real life: A supplier's card pinned in the kitchen
- In JavaScript: An
@font-facerule inside a shadow root - In real life: Making do with what's in the kitchen
- In JavaScript: The heading falling back to serif
Declare fonts in the document. In Chrome and Firefox, an @font-face rule inside a shadow root is ignored. A font the document declares works inside every shadow root, by name or by inheritance.
What does each browser do with @font-face in shadow DOM?
Ravi's idea wasn't wrong. The CSS spec says it should work. The CSS Shadow spec calls a font's name a tree-scoped name. That means the name belongs to the tree whose stylesheet declares it: the document, or one shadow root.
When text asks for a font by name, the spec says the browser should look in steps:
- First, in the text's own tree. For Ravi's heading, that's the card's shadow root.
- If the name isn't there, in the tree around it: the host's tree.
- And so on, up to the document.
So by the spec, Ravi's font would work inside his card, and the page couldn't use it. That's good encapsulation. But browsers don't do it.
Why Chrome skips the rule
Chrome's own code shows why. Here are the first lines of the function that adds a stylesheet's @font-face rules to the font list, copied from Chromium 158:
void ScopedStyleResolver::AddFontFaceRules(const RuleSet& rule_set) {
// TODO(crbug.com/336876): We don't add @font-face rules of scoped style
// sheets for the moment.
if (!GetTreeScope().RootNode().IsDocumentNode()) {
return;
}In plain words, line by line:
- Line 1 starts the function. It gets the rules from one stylesheet.
- Lines 2 and 3 are a note to Chrome's developers: "We don't add
@font-facerules of scoped style sheets for the moment." A scoped style sheet here is one inside a shadow root. - Line 4 asks: is this sheet's tree something other than the document?
- If so, line 5 stops. No font is added.
So Chrome still reads the rule. The shadow root's sheet lists it in cssRules, as a CSSFontFaceRule. But the rule never reaches the font list, and the font file is never fetched.
Firefox and Safari
Firefox ignores the rule too. Safari does something else. Chrome's article on CSS names reports that Safari moves the rule up to the document. WebKit, Safari's engine, shows it: the card's heading gets the font, and so does text outside the card. Read each row across:
| Who | @font-face inside a shadow root | @font-face in the document |
|---|---|---|
| The CSS Shadow spec | It works for the text that shadow root styles. The page can't use it. | Every shadow root below can use it. |
| Chrome and Edge | Ignored. The font never loads. | Works inside every shadow root. |
| Firefox | Ignored. The text falls back to another font. | Works inside every shadow root. |
| Safari | Treated as the page's own rule. It works inside, and the page can use it too. | Works inside every shadow root. |
Only the last column is the same everywhere. Why it matters: a font declared in the document works in every browser, so that's where your @font-face rules go.
How do you load a custom font for a web component?
Put the @font-face rule in the document. Then use the font's name inside the component. Try both places in the playground. The heading's font tells you which one works:
<!-- The page itself loads no fonts. --><template id="tea-card"> <link rel="stylesheet" href="kettle-hand.css"> <style>h3 { font-family: "Kettle Hand", serif; }</style> <h3>Green tea</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> console.log("Fonts the page knows:", document.fonts.size);</script>It prints Fonts the page knows: 0. The @font-face rule sits in the card's shadow root. Chrome never adds it to the page's font list, so the heading falls back to serif.
- With In the shadow root, line 3 links the font inside the card's template. It prints
Fonts the page knows: 0, and the heading is serif. - With In the page, line 1 links it from the page instead. It prints
Fonts the page knows: 1, and the heading is in Kettle Hand.
Line 4 didn't change. The heading asks for Kettle Hand by name. The lookup from the last section climbs out of the shadow root and finds the font in the document. Every browser does that part.
The page can also set the font itself. font-family is an inherited property: an element takes its parent's value unless a rule sets its own. Inherited values flow into a shadow tree, as lesson 11.1 showed. So body { font-family: "Kettle Hand", serif; } reaches the card's heading too, if the card sets no font of its own. Exercise 2 tries it.
Checking which font drew the text
Your eyes can be fooled by two similar fonts. DevTools can tell you for sure. Right-click the heading in the result and choose Inspect. In the Elements panel, open the Computed tab and scroll to the end. The Rendered fonts part lists each font that drew the text:
- its name;
- where it came from: "Network resource" for a web font, "Local file" for a font on your computer;
- how many glyphs it drew. A glyph is one drawn letter or sign.
With In the page, it lists Kettle Hand, a network resource, with 9 glyphs: the 9 characters of Green tea. With In the shadow root, it lists your computer's serif font, a local file. Chrome's DevTools team added this part so you can check that a web font is really in use.
On a real site, where src names a file, the Network panel agrees. Pick Font in its filter bar. With the stylesheet in the page, Chrome requests the font file. With the stylesheet in the shadow root, it never asks for it.
What about adopted style sheets?
Lesson 11.5 shared one constructed sheet through adoptedStyleSheets. Does an @font-face rule in an adopted sheet behave any differently? This page adopts one sheet in two places:
<tea-card></tea-card> <script type="module"> import { kettleHand } from "./kettle-hand.js"; import "./tea-card.js"; const fonts = new CSSStyleSheet(); fonts.replaceSync(`@font-face { font-family: "Kettle Hand"; src: url(${kettleHand}); }`); const root = document.querySelector("tea-card").shadowRoot; root.adoptedStyleSheets = [fonts]; console.log("Adopted by the shadow root:", document.fonts.size); document.adoptedStyleSheets = [fonts]; console.log("Adopted by the document:", document.fonts.size);</script>- Line 4 imports
kettleHand, the font file as a data: URL, from thekettle-hand.jstab. Line 5 loads the card. - Line 7 makes an empty sheet. Line 8 fills it with one
@font-facerule.${kettleHand}puts the data: URL intosrc. - Line 11 adopts the sheet in the card's shadow root. Line 12 prints
Adopted by the shadow root: 0. - Line 14 adopts the same sheet in the document. Line 15 prints
Adopted by the document: 1.
The heading ends up in Kettle Hand. Same sheet, same rule. Only the document's copy counted. A CSS module script from lesson 11.6 gives you a sheet too. Its fonts work the same way: only when the document adopts it.
Here are the places an @font-face rule can go. Read each row across:
| Where the @font-face rule goes | Does the card's text get the font in Chrome? |
|---|---|
A <link> or <style> in the page | Yes. |
A sheet in document.adoptedStyleSheets | Yes. |
document.fonts.add(new FontFace(…)) | Yes. |
A <link> or <style> in the shadow root | No. Chrome ignores the rule. |
A sheet in the shadow root's adoptedStyleSheets | No. Chrome ignores the rule. |
Can a web component load its own font?
Yes, if it puts the font in the document. Some components can't count on the page. Think of a widget that other sites paste in. It can still add its font to the document, from its own file.
The CSS Font Loading API does this in one line. A FontFace object is a font face made in JavaScript. It takes a name and a source, like an @font-face rule. document.fonts.add() puts it in the page's font list. MDN notes that a face must be in that list before text can use it.
Where the line goes matters. Switch between the two choices, and watch the count:
<tea-card>Green tea</tea-card><tea-card>Oolong</tea-card><tea-card>Black tea</tea-card> <script type="module"> import "./tea-card.js"; console.log("Fonts the page knows:", document.fonts.size);</script>It prints Fonts the page knows: 1. A module runs once per page. So line 3 adds one font face, and all three cards use it.
- In the
tea-card.jstab, line 1 imports the font file. With the first choice, line 3 makes aFontFacenamed Kettle Hand and adds it todocument.fonts. - On the page, lines 1 to 3 are three cards. Line 6 loads the card file. Line 7 prints the count:
Fonts the page knows: 1. - With In the constructor, the same line moves to line 6 of the tab. Now it prints
Fonts the page knows: 3.
All three headings look the same either way. But the page now holds three faces for one font, and every new card adds another. A module runs once per page, however many cards use it. So the top of the module is the place for work that must happen once.
A family of five comes to Leaf and Kettle for tea. One of them writes the family's name in the booking book at the door. If each of the five wrote it again at the table, the book would hold the same name five times, and the staff would set too many tables.
- In real life: The booking book
- In JavaScript:
document.fonts - In real life: One name, written at the door
- In JavaScript: One
FontFace, added at the top of the module - In real life: Each guest writing it again
- In JavaScript: Each card's constructor adding a face
- In real life: The same name five times
- In JavaScript: Three faces for one font
Adding a stylesheet to the head, once
A component can also add the font's stylesheet to the document's <head>. Give the element an id, and check for it first. Then a second card, or a second copy of the library, won't add it again:
if (!document.getElementById("kettle-hand-font")) {
const link = document.createElement("link");
link.id = "kettle-hand-font";
link.rel = "stylesheet";
link.href = new URL("./kettle-hand.css", import.meta.url);
document.head.append(link);
}- Line 1 looks for the link by its id. Everything inside runs only if it's missing.
- Lines 2 to 4 make the link and give it the id.
- Line 5 builds the file's address from the module's own, with
import.meta.url, as in lesson 11.6. - Line 6 puts the link in the head, so the rule lands in the document.
Exercise 4 fixes a card that forgets the check.
How do you use an icon font in shadow DOM?
An icon font is a font whose letters are pictures. Each picture sits at a private-use code point: a character number whose meaning Unicode leaves to whoever uses it. A class picks the picture. For example, .icon-cup::before { content: "\e001"; } puts character E001 before the element's text. The icon font draws that character as a cup.
So an icon font has two halves, and they need different homes:
- The
@font-facerule goes in the document. Inside a shadow root, Chrome ignores it, as you saw. - The class rules go inside the component. A page's selectors don't match elements in a shadow root, as lesson 11.1 showed.
The shop's icon font is Tea Icons. Its file, tea-icons.css, has both halves. Try the three choices:
<link rel="stylesheet" href="tea-icons.css"><template id="tea-card"> <!-- No icon classes in the shadow root. --> <h3><i class="icon icon-cup"></i> Green tea</h3></template><h2><i class="icon icon-leaf"></i> Leaf and Kettle</h2><tea-card></tea-card> <script src="tea-card.js"></script><script> const cup = document.querySelector("tea-card").shadowRoot.querySelector(".icon"); console.log("Fonts the page knows:", document.fonts.size); console.log("The cup has content:", getComputedStyle(cup, "::before").content !== "none");</script>It prints Fonts the page knows: 1 and The cup has content: false. The leaf shows. The card's cup is empty: the page's .icon-cup rule can't match an element inside the shadow root.
- Line 1 links
tea-icons.cssfrom the page, or holds a comment. Line 3 puts the class rules in the shadow root, or holds a comment. - Line 4 is the card's cup. Line 6 is a leaf in the page.
- Line 11 finds the cup. Line 12 prints the font count. Line 13 prints whether the cup's
::beforegot any content.
Here is what each choice shows:
- Font and classes in the page: the leaf shows, but the cup doesn't. It prints
The cup has content: false. The page's.icon-cuprule can't reach the card. - Classes in the shadow root only:
The cup has content: true, butFonts the page knows: 0. Tea Icons isn't on the page, so the browser draws E001 with another font, and no cup shows. - Font in the page, classes in the shadow root: both icons show.
.icon-cup rule can't match inside, so the card keeps its own copy of the class rule.Leaf and Kettle keeps one box of rubber stamps at the front desk: a cup, a leaf, a timer. Anyone may borrow it. Each room also needs a chart that says which stamp is which. The kitchen door stays shut, so the kitchen can't read the hallway's chart. It pins up its own.
- In real life: The box of stamps at the front desk
- In JavaScript: The icon font, declared in the page
- In real life: The chart that says which stamp is the cup
- In JavaScript:
.icon-cup::before { content: "\e001"; } - In real life: A chart pinned inside the kitchen
- In JavaScript: Class rules inside the shadow root
- In real life: The chart in the hallway
- In JavaScript: The page's class rules, which don't match inside
Many icon fonts ship both halves in one CSS file. Font Awesome's all.css does: its @font-face rules and its icon classes sit side by side. Link that file in the page, for the font. Link it in the shadow root too, for the classes. Chrome ignores the second copy's @font-face and uses the page's. Exercise 5 adds a second icon to the card.
Many component libraries draw icons with SVG instead, which needs no font at all. The web performance lesson on SVG and icons compares the two.
How do font fallbacks work in a web component?
A font may not arrive. The network may be slow, the file may be missing, or the page may never declare it. So every font-family should name a font stack: a list of families, best first. It ends with a generic family, like serif or sans-serif. A generic family always maps to some font the browser has.
The stack also helps when a font is missing a letter:
<link rel="stylesheet" href="kettle-hand.css"><template id="tea-card"> <style>h3 { font-family: "Kettle Hand", Georgia, serif; }</style> <h3><slot></slot></h3></template><tea-card>Green tea</tea-card><tea-card>Thé vert</tea-card> <script src="tea-card.js"></script>- Line 1 loads Kettle Hand from the page.
- Line 3 is the stack: Kettle Hand, then Georgia, then the browser's serif.
- Line 4 shows each card's text in its heading, through a slot.
- Lines 6 and 7 are two cards. The second is the French menu's green tea, Thé vert.
Green tea is all Kettle Hand. In Thé vert, the é looks different. Kettle Hand has no é, so the browser drew it with the next font in the stack. Inspect the second card, and Rendered fonts lists two fonts: Kettle Hand with 7 glyphs, and another font with 1.
Leaf and Kettle's scone recipe says: clotted cream, or butter if there's none, or any spread in the fridge. The cook doesn't give up when one thing runs out. She goes down the list for each part of the plate.
- In real life: The recipe's list, best first
- In JavaScript: The font stack
- In real life: Clotted cream
- In JavaScript: Kettle Hand
- In real life: Butter
- In JavaScript: Georgia
- In real life: Any spread in the fridge
- In JavaScript:
serif, the generic family - In real life: Checking the list for each part
- In JavaScript: Choosing a font for each character
If the font file never arrives at all, every letter moves down the stack, to Georgia or to serif. That's why a component should name a full stack in its own CSS. It can't be sure the page loaded its font.
Shaping the wait and the swap
Three descriptors in @font-face decide what readers see while a font loads. They go in the document, with the rule:
@font-face {
font-family: "Kettle Hand";
src: url("/fonts/kettle-hand.woff2") format("woff2");
font-display: swap;
}
@font-face {
font-family: "Kettle Hand Fallback";
src: local("Georgia");
size-adjust: 90%;
}
tea-card { --tea-font-display: "Kettle Hand", "Kettle Hand Fallback", serif; }- Line 4,
font-display: swap, draws the text in a fallback font at once. When the file arrives, the browser swaps it in. MDN lists the other values.blockhides the text for a short time first. - Lines 7 to 11 make a second face, Kettle Hand Fallback, from the Georgia on the reader's computer. Line 10,
size-adjust: 90%, draws its letters at 90% of their size. MDN says it scales every glyph and measurement of the font. - Line 13 puts the fallback face between Kettle Hand and serif.
Why bother? When the web font swaps in, text that changes size pushes the page around. A fallback sized like the web font moves less. ascent-override goes further: it sets how far the font reaches above the baseline, the line letters sit on. The 90% here is only an example. The right numbers come from comparing the two fonts. The web performance lessons on how web fonts load and fallback fonts without layout shifts go deeper.
Giving every tea card the shop's display font
Now the kit. Its tea-card has a new hook for a display font: a font for names and headings. The heading reads its font from a custom property, --tea-font-display. A custom property is a CSS variable, and custom properties cross the shadow boundary, as lesson 2.3 showed. The page loads the font and fills in the property:
<link rel="stylesheet" href="kettle-hand.css"><style> tea-card { --tea-font-display: "Kettle Hand", Georgia, serif; }</style><tea-card name="Green tea" price="4"></tea-card><tea-card name="Oolong" price="5"></tea-card> <script type="module"> import "./tea-kit/index.js"; const heading = document.querySelector("tea-card").shadowRoot.querySelector("h3"); console.log(getComputedStyle(heading).fontFamily);</script>- In the
tea-kit/tea-card.jstab, line 6 is the heading's rule. It ends withfont-family: var(--tea-font-display, Georgia, serif). That means: use the property, or Georgia and serif if it isn't set. - On the page, line 1 loads Kettle Hand. The card itself declares no font.
- Line 3 sets the property on every card, with a full stack.
- Line 9 loads the kit. Lines 10 and 11 print the heading's font:
"Kettle Hand", Georgia, serif.
Both tea names are in Kettle Hand. Press Edit & run and delete line 3. Now it prints Georgia, serif, the card's own fallback.
Why a custom property, and not font-family on tea-card? The heading has its own font-family rule. A rule that matches an element beats a value inherited from its parent. The card reads the custom property on purpose, so the page gets a say. Exercise 6 shows the difference. Lesson 12.1 turns hooks like this into a styling API.
Common mix-ups
- "The spec says font names are tree-scoped, so
@font-faceworks inside a shadow root." No. The spec says it should. Chrome and Firefox ignore it, and Safari treats it as the page's. - "Custom fonts don't work in web components." No. A font the document declares works inside every shadow root, by name or by inheritance.
- "An adopted sheet is different, so
@font-faceworks there." No. In a shadow root'sadoptedStyleSheets, Chrome ignores it too. Adopted by the document, it works. - "The page's icon classes reach inside the component." No. Page selectors don't match inside a shadow root. Keep the
@font-facein the page, and the class rules in the component. - "Adding the font in the constructor is fine." No. Every card adds another
FontFace. Add it once, at the top of the module, or check first. - "
document.fonts.check()tells you the font is in use." No. MDN notes it returnstrueeven for a font that isn't there at all. Use DevTools' Rendered fonts. - "Setting
font-familyon a component always changes its text." No. A rule inside the component that setsfont-familywins. Use the component's custom property.
Practice exercises
Answer first, then use the hints. The pages run live, so you can check yourself.
Line 1 links the font from the page, and line 3 links it inside the card's template. In Chrome, what does the console print?
<link rel="stylesheet" href="kettle-hand.css"><template id="tea-card"> <link rel="stylesheet" href="kettle-hand.css"> <style>h3 { font-family: "Kettle Hand", serif; }</style> <h3>Green tea</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> console.log("Fonts the page knows:", document.fonts.size);</script>Fonts the page knows: 1. Chrome adds the page's @font-face rule and skips the one in the shadow root, so the list holds one font.
The page sets a font on body. The card's heading has no font rule. What does line 11 print?
<link rel="stylesheet" href="kettle-hand.css"><style>body { font-family: "Kettle Hand", serif; }</style><template id="tea-card"> <h3>Green tea</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> const heading = document.querySelector("tea-card").shadowRoot.querySelector("h3"); console.log(getComputedStyle(heading).fontFamily);</script>"Kettle Hand", serif. The heading sets no font, so it inherits font-family from the card, which inherits it from body. The name then finds the font the page declared.
The card adopts a font sheet, but its heading stays serif, and the page prints Fonts the page knows: 0. Fix tea-card.js. What does your fixed page print?
<tea-card></tea-card> <script type="module"> import "./tea-card.js"; console.log("Fonts the page knows:", document.fonts.size);</script><tea-card></tea-card> <script type="module"> import "./tea-card.js"; console.log("Fonts the page knows:", document.fonts.size);</script>In the shadow root, Chrome ignores the sheet's @font-face rule. Adopted once by the document, the rule joins the font list, and the page prints Fonts the page knows: 1.
Each card adds a <style> with the font to the document's head, and the page prints Font styles in the head: 3. Make the cards add it only once. What does your fixed page print?
<tea-card>Green tea</tea-card><tea-card>Oolong</tea-card><tea-card>Black tea</tea-card> <script type="module"> import "./tea-card.js"; console.log("Font styles in the head:", document.querySelectorAll("#kettle-hand-font").length);</script><tea-card>Green tea</tea-card><tea-card>Oolong</tea-card><tea-card>Black tea</tea-card> <script type="module"> import "./tea-card.js"; console.log("Font styles in the head:", document.querySelectorAll("#kettle-hand-font").length);</script>The first card adds the <style> with its id. The check finds it for the other two, so they add nothing, and the page prints Font styles in the head: 1.
The card shows a cup, but no timer, and the page prints Icons with content: 1. Fix the card's template. What does your fixed page print?
<link rel="stylesheet" href="tea-icons.css"><template id="tea-card"> <style>.icon { font-family: "Tea Icons"; } .icon-cup::before { content: "\e001"; }</style> <h3><i class="icon icon-cup"></i> Green tea <i class="icon icon-timer"></i> 3 min</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> const icons = document.querySelector("tea-card").shadowRoot.querySelectorAll(".icon"); const drawn = [...icons].filter((icon) => getComputedStyle(icon, "::before").content !== "none"); console.log("Icons with content:", drawn.length);</script><link rel="stylesheet" href="tea-icons.css"><template id="tea-card"> <style>.icon { font-family: "Tea Icons"; } .icon-cup::before { content: "\e001"; } .icon-timer::before { content: "\e003"; }</style> <h3><i class="icon icon-cup"></i> Green tea <i class="icon icon-timer"></i> 3 min</h3></template><tea-card></tea-card> <script src="tea-card.js"></script><script> const icons = document.querySelector("tea-card").shadowRoot.querySelectorAll(".icon"); const drawn = [...icons].filter((icon) => getComputedStyle(icon, "::before").content !== "none"); console.log("Icons with content:", drawn.length);</script>The page's class rules don't match inside the shadow root, so the timer had no content. With its own rule in the card, both icons have content, and the page prints Icons with content: 2. The font still comes from the page.
The page loads Kettle Hand and sets it on tea-card, but the name stays in Georgia, and the page prints Georgia, serif. Fix line 3. What does your fixed page print?
<link rel="stylesheet" href="kettle-hand.css"><style> tea-card { font-family: "Kettle Hand", serif; }</style><tea-card name="Green tea" price="4"></tea-card> <script type="module"> import "./tea-kit/index.js"; const heading = document.querySelector("tea-card").shadowRoot.querySelector("h3"); console.log(getComputedStyle(heading).fontFamily);</script><link rel="stylesheet" href="kettle-hand.css"><style> tea-card { --tea-font-display: "Kettle Hand", serif; }</style><tea-card name="Green tea" price="4"></tea-card> <script type="module"> import "./tea-kit/index.js"; const heading = document.querySelector("tea-card").shadowRoot.querySelector("h3"); console.log(getComputedStyle(heading).fontFamily);</script>The heading's own font-family rule beats the value it would inherit, so the page's rule never reached it. The custom property is what the rule reads, so setting it prints "Kettle Hand", serif.
Check your understanding
For each question, ask: is this rule in the document, or in a shadow root?
Question 1 of 7In Chrome, where must an
@font-facerule live so a card's shadow root can use the font?Choose an answer to see the explanation.
Question 2 of 7What does this page print in Chrome?
Read the code, then predict<tea-card></tea-card> <script type="module"> import { kettleHand } from "./kettle-hand.js"; const root = document.querySelector("tea-card").attachShadow({ mode: "open" }); root.innerHTML = `<style>@font-face { font-family: "Kettle Hand"; src: url(${kettleHand}); }</style>`; console.log(document.fonts.size); </script>Choose an answer to see the explanation.
Question 3 of 7What does Safari do with an
@font-facerule inside a shadow root?Choose an answer to see the explanation.
Question 4 of 7A page links an icon font's CSS file, with its
@font-facerule and an.icon-cup::beforerule. Why is the cup inside the card empty?Choose an answer to see the explanation.
Question 5 of 7Three cards each run
document.fonts.add(new FontFace(...))in their constructor. How many faces does the page hold?Choose an answer to see the explanation.
Question 6 of 7A heading has
font-family: "Kettle Hand", Georgia, serif, and its text has an é that Kettle Hand doesn't have. What happens?Choose an answer to see the explanation.
Question 7 of 7What does this FontFace page print?
Read the code, then predict<tea-card></tea-card> <script type="module"> import { kettleHand } from "./kettle-hand.js"; const face = new FontFace("Kettle Hand", `url(${kettleHand})`); document.fonts.add(face); await face.load(); console.log(face.status); </script>Choose an answer to see the explanation.
Key takeaways
- In Chrome and Firefox, an
@font-facerule inside a shadow root is ignored, whether it's in a<style>, a<link>, or an adopted sheet. Safari treats it as the page's. - The CSS Shadow spec says it should work inside. Chromium's code stops early for any tree that isn't the document.
- Declare fonts in the document. A shadow root finds them by name, and
font-familyinherits into it. - A component can add its own font with
document.fonts.add(new FontFace(…)). Do it once, at the top of the module, or check first. - An icon font's
@font-facegoes in the page. Its class rules go inside the component. - Name a full font stack. The browser picks a font for each character, and
font-displayandsize-adjustshape the swap. - DevTools' Rendered fonts shows which font really drew the text.
Remember the one-liner.
The document declares the font; the component names it, with a fallback.
Sources
The facts on this page come from these standards, docs, and source files:
- CSS Shadow Module Level 1: Name-Defining Constructs and Inheritance, CSSWG Editor's Draft.
- scoped_style_resolver.cc, AddFontFaceRules (Chromium 158.0.8127.18), Chromium.
- Author-defined CSS names and the shadow DOM: In specification and in practice, Noam Rosenthal, Chrome for Developers.
- @font-face, MDN.
- CSS Font Loading API: adding a font to a document or worker, MDN.
- FontFaceSet: check() method, MDN.
- Private-Use Characters, Noncharacters, and Sentinels FAQ, Unicode.
- css/all.css (Font Awesome Free 7.3.1), Font Awesome.
- DevTools answers: What font is that?, Paul Irish, Chrome for Developers.
- PlatformFontsWidget.ts, the Rendered fonts section, Chrome DevTools.
- font-display, MDN.
- size-adjust, MDN.
- ascent-override, MDN.
Coming next: Custom properties as a styling API. It turns hooks like --tea-font-display into a documented API.