40

I created a custom Facebook icon using Inkscape and saved it as an svg. It uses the same font as the title of my page, which uses the font "ubuntutitling-bold-webfont", which I have referenced as a .woff in a CSS file. The title functions, but the svg does not - it displays the "f" in some sort default font. The font file is located at ../fonts/ubuntutitling-bold-webfont.woff in relativity to the CSS and image file, and at fonts/ubuntutitling-bold-webfont.woff in relativity to the web page.

I fount How do I embed Google Web Fonts into an SVG?, but I am a bit confused on how to apply the code to the SVG, if I even need to. Can the SVG use the .woff font referenced in the CSS file? Was the way I manually changed the font inside the SVG file correct?

Here is the code for the SVG:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="80"
   height="80"
   id="svg2"
   version="1.1"
   inkscape:version="0.48.3.1 r9886"
   sodipodi:docname="New document 1">
  <defs
     id="defs4" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="1"
     inkscape:cx="26.573808"
     inkscape:cy="42.478414"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0"
     inkscape:window-width="1619"
     inkscape:window-height="611"
     inkscape:window-x="165"
     inkscape:window-y="301"
     inkscape:window-maximized="0" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(0.75006053,-981.36219)">
    <rect
       style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:10;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
       id="rect2987"
       width="70"
       height="70"
       x="4.2499394"
       y="986.36218"
       rx="10"
       ry="10" />
    <text
       xml:space="preserve"
       style="font-size:109.44319916px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#ffffff;fill-opacity:1;stroke:#fffffe;stroke-width:2.73607969;stroke-opacity:1;font-family:ubuntutitling-bold-webfont';-inkscape-font-specification:ubuntutitling-bold-webfont'"
       x="25.989071"
       y="1096.0118"
       id="text3757"
       sodipodi:linespacing="125%"
       transform="scale(1.0381556,0.96324674)"><tspan
         sodipodi:role="line"
         id="tspan3759"
         x="25.989071"
         y="1096.0118"
         style="stroke-width:2.73607969">f</tspan></text>
  </g>
</svg>

Thank you in advance.

1
  • How do you display the SVG? Do you load it as an <img>, do you embed it as an object or do you display the SVG inline?
    – Ideogram
    Oct 17, 2015 at 11:43

4 Answers 4

24

You need to put CSS in the defs section. Something like:

<defs>
  <style type="text/css">
    @font-face {
      font-family: Delicious;
      src: url('../fonts/font.woff');
    }
  </style>
</defs>
7
7

To load the Google or any other external fornts in SVG file, we have to add the style tag in defs tag as follows:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="246" height="272" xml:space="preserve"><desc>Your description</desc>

<defs>
<style type="text/css">@import url('http://fonts.googleapis.com/css?family=Lobster|Fontdiner+Swanky|Crafty+Girls|Pacifico|Satisfy|Gloria+Hallelujah|Bangers|Audiowide|Sacramento');</style>
</defs>
2
1

Edit: I reread your question... it seems like your best option might be to convert the text in your icon to an outline, so it doesn't need the font at all. (You may have to check your font license to see if this is ok.)

I assumed, incorrectly, that you had a vector icon and you were looking for the best way to display it in your website.


I think you have a few options.

Use an SVG image

You could just use the SVG as an image itself. You'll lose support for older versions of IE, but you could swap in a PNG or GIF for IE8 and earlier. Or not bother about IE8 and older support (may or may not be an option, depending on your target audience).

Use Font Squirrel's generator

Font Squirrel have an @font-face generator that does all the heavy lifting for you, if you already have the font created.

Font Squirrel @font-face generator

Use a PNG image

Why are you using SVG? Is it for better zoom and high DPI support? If so, there's ways to do that with CSS and PNGs. It really depends on your intended use.


I'm not a fan of embedding icons in fonts. You lose pixel level control. There is a few situations where doing so is a good choice, but often it's way more effort that it's worth and yields worse results.

-1
<defs>
 <style type="text/css">
 <![CDATA[
  @font-face {
    font-family: 'Fredoka One';
    font-style: normal;
    font-weight: 400;
    src: local('Fredoka One'), local('FredokaOne-Regular'), 
    url(https://fonts.gstatic.com/s/fredokaone/v7/k3kUo8kEI- 
    tA1RRcTZGmTlHGCac.woff2) format('woff2');
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, 
    U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, 
    U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
  }
  ]]>
 </style>
</defs>

That's an example for the following font: https://fonts.googleapis.com/css?family=Fredoka%20One Open visit the font page and copy everything into "defs".

1
  • 2
    Welcome to GDSE! The link leads nowhere for me, could you please edit your answer to add the relevant information?
    – curious
    Sep 15, 2019 at 3:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.