How to import a font file using JavaScript?

CSS has an @font-face rule that allows us to “use” custom fonts.

Does this feature have JavaScript?

In particular, is it possible to “use” custom fonts without using the CSS @font-face rule?

+4
source share
4 answers

It is not possible to force the browser to download a font from the network, except for the @font-face rule. Theoretically, you could create an @font-face rule from JavaScript without having to use document.write (or document.createElement("style") , etc.) if you used the CSS Object Model instead; but I would not expect this to be implemented in any browser at the moment.

+3
source

if you know CSS, you can use document.write to dynamically add a stylesheet ... I assume this will be counted using CSS, but it will work

+4
source

No. No. Javascript can manipulate the DOM, but it does not care about formatting.

+1
source

Yes, you can:

 document.body.style.fontFamily = '...'; 

here is the fiddle: http://jsfiddle.net/maniator/QMZ8N/

it only adds a predefined font to the element.

This means that it does NOT add font style to the page. you may need to use CSS for this

0
source

Source: https://habr.com/ru/post/1347168/


All Articles