DEV Community

Rocktim Saikia
Rocktim Saikia

Posted on

How to load a google font in a web component ?

How to load a google font in an encapsulated shadow dom. I am learning web components and couldn't find any way to add a google font URL to the shadow dom. quick help please?

Top comments (4)

aminnairi profile image

Very good question. I never really managed to do it in an encapsulated way. I always ended up adding the link tag in both my component and the client's code (where the component is used). But that totally kills the portability of the component. I'm writing this comment to get a followup on this one.

If you manage to find a way on your own, don't forget to share it with us!

rocktimsaikia profile image
Rocktim Saikia

sure. I am reading every single web component article on mdn to find something related to this issue. Currently injecting the link to the client side is the only option I see.

kodertian profile image

Ex.: Assuming we have in the index.html head a link tag to the google font, eg:

In an external index.css file we have declared something like:
:root {
--jos: 'josefin slab';
} .

Then in the web component's style we could do:
:host {
font-family: var(--jos)

But I believe you don't want the google font declared in the outside index.html file, so in the WC style we could do something like:

@import url(";

:host {
font-family: "Josefin Slab";

That would work.

remjx profile image
Mark Jackson • Edited

This isn't working for me, I can only get the font to load if I put the font <link> in the html <head>