completefrontend
Open lab My progress
THE WEB COMPONENTS FIELD GUIDE

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.

By the end, you can
  • 01
    Explain where a font must be declaredSay why an @font-face rule inside a shadow root does nothing in Chrome and Firefox, and what Safari does with it.
  • 02
    Load a font for a componentDeclare it in the page, adopt it in the document, or add one FontFace from the component, once.
  • 03
    Use icon fonts and fallbacksPut an icon font's @font-face in 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:

A card that links its font inside its own templatePop out in the code editor (opens in a new tab)HTML
<!-- 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>
ResultLoading…
  • 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-face rule.
  • Line 4 asks for Kettle Hand for the card's heading, and serif if Kettle Hand is missing.
  • In the tea-card.js tab, 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:

The @font-face rule as a real site writes itCSS
@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. woff2 is 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.

Two halves. On the left, linked in the shadow root: a tea-card window holds a card, @font-face Kettle Hand. Below it, labelled never added, a crate named document.fonts holds a faded, crossed-out Kettle Hand card. Under it: Green tea in serif. On the right, linked in the page: a page window holds the same @font-face card. An arrow labelled added goes down to the document.fonts crate, which holds a Kettle Hand card. Under it: Green tea in Kettle Hand.Linked in the shadow roottea-card@font-face Kettle Handnever addedKettle Handdocument.fontsGreen tea in serifLinked in the pagepage@font-face Kettle HandaddedKettle Handdocument.fontsGreen tea in Kettle Hand
Chrome only adds the document's @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.
Real-life analogyThe front desk places the orders

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-face rule in the document
In real life: A supplier's card pinned in the kitchen
In JavaScript: An @font-face rule inside a shadow root
In real life: Making do with what's in the kitchen
In JavaScript: The heading falling back to serif
The main idea

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:

  1. First, in the text's own tree. For Ravi's heading, that's the card's shadow root.
  2. If the name isn't there, in the tree around it: the host's tree.
  3. 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:

AddFontFaceRules, the first lines, from Chromium's scoped_style_resolver.ccText
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:

  1. Line 1 starts the function. It gets the rules from one stylesheet.
  2. Lines 2 and 3 are a note to Chrome's developers: "We don't add @font-face rules of scoped style sheets for the moment." A scoped style sheet here is one inside a shadow root.
  3. Line 4 asks: is this sheet's tree something other than the document?
  4. 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:

@font-face inside a shadow root, and in the document
Who@font-face inside a shadow root@font-face in the document
The CSS Shadow specIt works for the text that shadow root styles. The page can't use it.Every shadow root below can use it.
Chrome and EdgeIgnored. The font never loads.Works inside every shadow root.
FirefoxIgnored. The text falls back to another font.Works inside every shadow root.
SafariTreated 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:

Playground: where the font's stylesheet goes
The same font link, in the page or in the shadow rootPop out in the code editor (opens in a new tab)HTML
<!-- 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>
ResultLoading…
Try it yourself
Linked inside the card's template

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.

Only lines 1 and 3 change: the link moves between the page and the card's template. Watch the heading's font.
  • 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:

One font sheet, adopted by the shadow root and then by the documentPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • Line 4 imports kettleHand, the font file as a data: URL, from the kettle-hand.js tab. Line 5 loads the card.
  • Line 7 makes an empty sheet. Line 8 fills it with one @font-face rule. ${kettleHand} puts the data: URL into src.
  • 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 can go, and whether it works in Chrome
Where the @font-face rule goesDoes the card's text get the font in Chrome?
A <link> or <style> in the pageYes.
A sheet in document.adoptedStyleSheetsYes.
document.fonts.add(new FontFace(…))Yes.
A <link> or <style> in the shadow rootNo. Chrome ignores the rule.
A sheet in the shadow root's adoptedStyleSheetsNo. 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:

Playground: where the component adds its font
Three cards whose file adds the font to the documentPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
Try it yourself
Line 3 runs once

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.

The page stays the same. Each choice moves the font line in the tea-card.js tab. Open that tab to see it.
  • In the tea-card.js tab, line 1 imports the font file. With the first choice, line 3 makes a FontFace named Kettle Hand and adds it to document.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.

Two halves. On the left, at the top of the module: a tea-card.js card with an arrow labelled runs once down to a crate named document.fonts, which holds one FontFace card. Under it: 1 face for 3 cards. On the right, in the constructor: three cards, Green tea, Oolong, and Black tea, each with an arrow down to a document.fonts crate that holds three FontFace cards. Under it: 3 faces, one per card.At the top of the moduletea-card.jsruns onceFontFacedocument.fonts1 face for 3 cardsIn the constructorGreen teaOolongBlack teaFontFaceFontFaceFontFacedocument.fonts3 faces, one per card
A module runs once per page, so line 3 adds one face that every card uses. A constructor runs once per card, so the same line there adds a face for each card.
Real-life analogyWriting the booking once, at the door

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:

Add the font's stylesheet to the head, only if it isn't thereJavaScript
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-face rule 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:

Playground: an icon font and its classes
A leaf icon in the page and a cup icon in the cardPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
Try it yourself
The page's classes stop at the shadow root

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.

Only lines 1 and 3 change. Line 1 links the icon font's stylesheet in the page; line 3 puts the class rules in the shadow root.
  • Line 1 links tea-icons.css from 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 ::before got 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-cup rule can't reach the card.
  • Classes in the shadow root only: The cup has content: true, but Fonts 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.
A page window. On its left, two cards: @font-face Tea Icons, and a faded, crossed-out .icon-cup::before, the page's class rule. On the right, a tea-card window, the card's shadow root, holds its own .icon-cup::before card, with the note the cup shows. An arrow labelled the font, by name goes from the @font-face card into the tea-card window. An arrow from the page's class rule stops short of the window, labelled stops at the shadow root.page@font-face Tea Icons.icon-cup::beforetea-card.icon-cup::beforethe cup showsthe font, by namestops at the shadow root
The page declares Tea Icons, and the shadow root finds the font by name. The page's .icon-cup rule can't match inside, so the card keeps its own copy of the class rule.
Real-life analogyA box of stamps and a chart in each room

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:

A font stack, and a tea name with a letter Kettle Hand doesn't havePop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • 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.

The stack font-family: "Kettle Hand", Georgia, serif as three cards in a row, Kettle Hand, Georgia, and serif, joined by arrows. Under the first arrow: no é. Under the second: no Georgia. Below, Thé vert drawn letter by letter: a card under Kettle Hand holds T, h, v, e, r, t, and a card under Georgia holds é. Under serif: only if Georgia is missing.font-family: "Kettle Hand", Georgia, serif;Kettle HandGeorgiaserifno éno GeorgiaThé vert, letter by letter:T, h, v, e, r, téonly if Georgia is missing
The browser picks a font for each character. Kettle Hand draws every letter of Thé vert except é, which it doesn't have, so Georgia draws that one. The browser's serif is the last stop.
Real-life analogyA recipe that lists substitutes

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:

A web font that swaps in, and a fallback sized to match itCSS
@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. block hides 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:

The kit's cards with the shop's display fontPop out in the code editor (opens in a new tab)HTML
<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>
ResultLoading…
  • In the tea-kit/tea-card.js tab, line 6 is the heading's rule. It ends with font-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-face works 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-face works there." No. In a shadow root's adoptedStyleSheets, 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-face in 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 returns true even for a font that isn't there at all. Use DevTools' Rendered fonts.
  • "Setting font-family on a component always changes its text." No. A rule inside the component that sets font-family wins. Use the component's custom property.

Practice exercises

Answer first, then use the hints. The pages run live, so you can check yourself.

Exercise 1 · Warm-upThe font in both places

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>
ResultLoading…

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upA font from the page's body

    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>
    ResultLoading…

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeThe adopted font that never loads

      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>
      ResultLoading…

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeOne font style, however many cards

        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>
        ResultLoading…

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeA second icon in the card

          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>
          ResultLoading…

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeThe display font that doesn't reach the name

            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>
            ResultLoading…

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask: is this rule in the document, or in a shadow root?

              The fonts and shadow DOM quiz · 7 questionsScore: first tries count
              1. Question 1 of 7In Chrome, where must an @font-face rule live so a card's shadow root can use the font?

                Choose an answer to see the explanation.

              2. Question 2 of 7What does this page print in Chrome?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <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.

              3. Question 3 of 7What does Safari do with an @font-face rule inside a shadow root?

                Choose an answer to see the explanation.

              4. Question 4 of 7A page links an icon font's CSS file, with its @font-face rule and an .icon-cup::before rule. Why is the cup inside the card empty?

                Choose an answer to see the explanation.

              5. 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.

              6. 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.

              7. Question 7 of 7What does this FontFace page print?

                Read the code, then predictPop out in the code editor (opens in a new tab)HTML
                <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-face rule 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-family inherits 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-face goes 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-display and size-adjust shape 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:

              Coming next: Custom properties as a styling API. It turns hooks like --tea-font-display into a documented API.

              CompleteFrontend Clear concepts. Working examples.