How to embed fonts in HTML?

125,829

Solution 1

Things have changed since this question was originally asked and answered. There's been a large amount of work done on getting cross-browser font embedding for body text to work using @font-face embedding.

Paul Irish put together Bulletproof @font-face syntax combining attempts from multiple other people. If you actually go through the entire article (not just the top) it allows a single @font-face statement to cover IE, Firefox, Safari, Opera, Chrome and possibly others. Basically this can feed out OTF, EOT, SVG and WOFF in ways that don't break anything.

Snipped from his article:

@font-face {
  font-family: 'Graublau Web';
  src: url('GraublauWeb.eot');
  src: local('Graublau Web Regular'), local('Graublau Web'),
    url("GraublauWeb.woff") format("woff"),
    url("GraublauWeb.otf") format("opentype"),
    url("GraublauWeb.svg#grablau") format("svg");
}

Working from that base, Font Squirrel put together a variety of useful tools including the @font-face Generator which allows you to upload a TTF or OTF file and get auto-converted font files for the other types, along with pre-built CSS and a demo HTML page. Font Squirrel also has Hundreds of @font-face kits.

Soma Design also put together the FontFriend Bookmarklet, which redefines fonts on a page on the fly so you can try things out. It includes drag-and-drop @font-face support in FireFox 3.6+.

More recently, Google has started to provide the Google Web Fonts, an assortment of fonts available under an Open Source license and served from Google's servers.

License Restrictions

Finally, WebFonts.info has put together a nice wiki'd list of Fonts available for @font-face embedding based on licenses. It doesn't claim to be an exhaustive list, but fonts on it should be available (possibly with conditions such as an attribution in the CSS file) for embedding/linking. It's important to read the licenses, because there are some limitations that aren't pushed forward obviously on the font downloads.

Solution 2

Try Facetype.js, you convert your .TTF font into a Javascript file. Full SEO compatible, supports FF, IE6 and Safari and degrades gracefully on other browsers.

Solution 3

No, there isn't a decent solution for body type, unless you're willing to cater only to those with bleeding-edge browsers.

Microsoft has WEFT, their own proprietary font-embedding technology, but I haven't heard it talked about in years, and I know no one who uses it.

I get by with sIFR for display type (headlines, titles of blog posts, etc.) and using one of the less-worn-out web-safe fonts for body type (like Trebuchet MS). If you're bored with all the web-safe fonts, you're probably defining the term too narrowly — look at this matrix of stock fonts that ship with major OSes and chances are you'll be able to find a font cascade that will catch nearly all web users.

For instance: font-family: "Lucida Grande", "Verdana", sans-serif is a common font cascade; OS X comes with Lucida Grande, but those with Windows will get Verdana, a web-safe font with letters of similar size and shape to Lucida Grande. Linux users will also get Verdana if they've installed the web-safe fonts package that exists in most distros' package managers, or else they'll fall back to an ordinary sans-serif.

Solution 4

And it's unlikely too -- EOT is a fairly restrictive format that is supported only by IE. Both Safari 3.1 and Firefox 3.1 (well the current alpha) and possibly Opera 9.6 support true type font (ttf) embedding, and at least Safari supports SVG fonts through the same mechanism. A list apart had a good discussion about this a while back.

Solution 5

Check out Typekit, a commercial option (they have a free package available too).

It uses different techniques depending on which browser is being used (@font-face vs. EOT format), and they take care of all the font licensing issues for you also. It supports everything down to IE6.

Here's some more info about how Typekit works:

Share:
125,829

Related videos on Youtube

Dan Rosenstark
Author by

Dan Rosenstark

When I was born, I knew nothing about programming. Projects MIDI Designer Pro for iPad, iPhone, iPod touch—dream | create | play your Perfect MIDI Controller on iOS MJDJ—Desktop Java application for MIDI Morphing (transforming) Handsonic Editor—Powerful and popular editor for the Roland Handsonic HPD-15 The KBase—A multi-hierarchical text editor (.Net standalone and Web versions) Technical rambles (blog) Contact Contact me via MIDIdesigner.com

Updated on February 15, 2021

Comments

  • Dan Rosenstark
    Dan Rosenstark about 3 years

    I'm trying to figure out a decent solution (especially from the SEO side) for embedding fonts in web pages. So far I have seen the W3C solution, which doesn't even work on Firefox, and this pretty cool solution. The second solution is for titles only. Is there a solution available for full text? I'm tired of the standard fonts for web pages.

    Thanks!

    • hakre
      hakre almost 11 years
    • user2284570
      user2284570 over 9 years
      possible duplicate of Fonts on the Web
    • Madara's Ghost
      Madara's Ghost over 9 years
      @user2284570 This question has a better answer. I've closed the other one as a duplicate of this.
    • 1.21 gigawatts
      1.21 gigawatts almost 4 years
      Any updated answers on this question? All of the answers are more than half a decade old. Also, I couldn't find a reference on MDN if anyone knows.
    • Dan Rosenstark
      Dan Rosenstark almost 4 years
      Please provide an updated answer and help the World. I haven’t done HTML since 2011 myself
  • savetheclocktower
    savetheclocktower over 15 years
    Let me also stand on the soapbox for a minute: though I wish Windows had more "good" fonts (OS X has the greatest hits of typography history: Futura, Helvetica, Gill Sans, etc.), it's a good thing that we have to restrain ourselves when picking fonts. Freedom can be used for evil, too.
  • Dan Rosenstark
    Dan Rosenstark over 15 years
    No comment on your comment, but thanks for the answer. That's great. Is there a list of cascades somewhere? Thanks again.
  • savetheclocktower
    savetheclocktower over 15 years
    Here's one list: (ampsoft.net/webdesign-l/WindowsMacFonts.html). It's conservative, only listing fonts that are identical (or nearly so) across platforms. You might have to do a comparison to see which "safe" font is most like your "risky" font.
  • savetheclocktower
    savetheclocktower over 15 years
    (Oh, also: always specify a general fallback — sans-serif, serif, or monospace. That way, it'll at least be in the ballpark, should the user have none of your requested fonts.)
  • Dan Rosenstark
    Dan Rosenstark over 15 years
    Excellent. Thanks again. Although now that I think about it I can just steal the cascades from sites I like.
  • olliej
    olliej over 15 years
    If by bleeding edge you mean Safari 3.1 -- which has been out for 6 or 7 months -- as well as Opera 9.2 (which i believe is when Opera added ttf support), then yes, bleeding edge is an appropriate description, but most people would not consider something to be bleeding edge many months after release
  • Gene
    Gene over 15 years
    Most users that come to the sites I work on still use IE6 (those bastards) So from that point of view I consider the latest major versions to be bleeding edge.
  • Pekka
    Pekka over 14 years
    I think it is worthy to emphasize that you are not automatically allowed to take a font and @font-face-ize it even if you have bought a license of that font legally and for a lot of money. You need to check the font's license for whether that kind of electronic re-distribution is allowed or not. Infringements are so easy to detect and trawl for that there may easily be trouble otherwise, especially if you are a company situated in the right jurisdiction.
  • fencepost
    fencepost over 14 years
    FontSquirrel makes a clear point of this both on its pre-built font kits and in its generator (which requires you to verify that the fonts are actually licensed for such use). There are other conversion tools out there, but I don't know which ones make a point of discussing licensing.
  • MordechayS
    MordechayS over 14 years
    For those who wonder how it works, it uses the canvas tag (html 5) or VML.
  • Dan Rosenstark
    Dan Rosenstark over 14 years
    Fascinating stuff. Out of curiosity, how did you get to this post? There was more than one answer on the same day, and several comments on your answer. +1...
  • fencepost
    fencepost over 14 years
    I don't remember how I ended up on it - search for HTML font perhaps, but I responded because I was working on just this for a client just before Christmas so the information was fresh. I assume Pekka saw it as new activity on an old post; I don't know if the system flags attempts to post multiple links (I know it blocked initially) but I figured it might be a case of "check this to be sure it's not someone spamming".
  • askon
    askon almost 14 years
    I would have to agree with this solution it looks extremely simple and seems to be working on every browser I throw at it.
  • Dan Rosenstark
    Dan Rosenstark about 12 years
    @Pekka "infringements are so easy to detect and trawl for"... can you englighten me a bit, please... how would this detection and trawling be done, and under what circumstances would it happen?
  • fencepost
    fencepost about 12 years
    It wouldn't be trivial to look at a site or page and determine whether it was infringing, but if you're a font designer I suspect you could search for the standard filename or font face name of your font and locate usages.
  • Dan Rosenstark
    Dan Rosenstark over 11 years
    @fencepost so they'd be relying on the name, filenames and other metadata to detect infringements? I realize this is all hypothetical.
  • fencepost
    fencepost over 11 years
    That's my suspicion for automated searches - assuming that none of the search engines are actually indexing the actual copyright strings within the files themselves, it's probably filename and font name based. Of course, font-oriented people might look at the page and say "Hey, I know that font!" Since it's not just the filename, modifying the font file to change the names then using your tweaked file seems like stealing for the sake of stealing - if you're putting in that much effort, find a comparable free font elsewhere. It's like water-cooling Celerons, it's not for performance.
  • user2284570
    user2284570 over 9 years
    @fencepost : Now I understand why mediawiki allow full style attribute but no style tags :-).
  • aleclarson
    aleclarson over 6 years
    This link is dead.