I'm embedding SVGs in my page with the <object>
tag, and they're supposed to utilize Google Fonts (e.g. Roboto). However, the SVGs aren't picking these fonts up and instead default to system fonts.
What am I doing wrong? Does every SVG require that the font itself be embedded in <style>
?
Example code:
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
</head>
<body>
<object width="250" height="200" type="image/svg+xml" data="img/popup_image.svg"></object>
</body>
SVG snippet:
<text font-size="14" fill="#333" font-family="Roboto">Words go here</text>
The browser treats SVG text as regular HTML text. In other words, any text elements in your SVG must be styled like normal HTML elements (like a
<span>
, for example). You need to embed your font in your SVG in the form of a CSS@import
. Look through the XML of your SVG for the<defs>
section. Then, add this code to it:Next, update your
<text>
element to be like this:If you want more information about this, you might try this website: http://nimbupani.com/about-fonts-in-svg.html. It has some pretty good information on fonts in embedded SVGs. A working example of this can be found here: https://github.com/marians/test-webfonts-in-svg.
Some suggestions if you want to make your logo portable and available offline you should consume and embed the smallest version of your font possible.
First, optimize the font size, by only pulling in the stroke weights and characters you actually need.
So instead of this:
Condense the options and also add the
?text=abc
param to only grab a subset of glyphs like this:Inside your svg, you can include a
@font-face
declaration in a stylesheet in the<defs>
tag like this:Grab the file by navigating to the google fonts stylesheet and downloading the actual .woff2 file. You can convert it to a b64 string using base64-encoder or read more at Converting and rendering web fonts to base64. Take that string and include it in your file. Just for clarity, you could also specify the unicode range on the font-face to make explicit that you're only pulling in some characters
See Also: