Home

How to Use Embedded Fonts for Your HTML5 and CSS3 Based Web Pages

|
|  Updated:  
2016-03-26 13:16:52
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

Although an HTML5 web developer can suggest any font for a web page, the font files are traditionally a client-level asset. If the client doesn't have the font installed, she won't see it. Fortunately, CSS3 supports a sensible solution for providing downloadable fonts.

image0.jpg

The style does not work like most CSS elements. It doesn't apply markup to some part of the page. Instead, it defines a new CSS value that can be used in other markup. Specifically, it allows you to place a font file on your server and define a font family using that font.

  @font-face {
  font-family: "Miama";
  src: url("Miama.otf");
  }

The font-family attribute indicates the name you will be giving this font in the rest of your CSS code. Typically it is similar to the font file name. The font-family attribute is the URL of the actual font file as it is found on the server. After a font-face has been defined, it can be used in an ordinary attribute in the rest of your CSS code:

  h1 {
  font-family: Miama;
  }

Here's the code for the custom font example:

<!DOCTYPE html>
 <head>
 <title>EmbeddedFont</title>
 <style type = "text/css">
  @font-face {
  font-family: "Miama";
  src: url("Miama.otf");
  }
  @font-face {
  font-family: "spray";
  src: url("ideoma_SPRAY.otf");
  }
  h1 {
  font-family: Miama;
  font-size: 300%;
  }
  h2 {
  font-family: spray;
  }
 </style>
 </head>
 <body>
 <h1>Embedded Font Demo</h1>
 <h2 id="tab1" >Here's another custom font</h2>
 </body>
</html>

Although all modern browsers support the @font-face feature, the actual file types supported vary from browser to browser. Here are the primary font types:

  • TTF: The standard TrueType format is well-supported, but not by all browsers. Many open-source fonts are available in this format.

  • OTF: This is similar to TTF, but is a truly open standard, so it is preferred by those who are interested in open standards. It is supported by most browsers except IE.

  • WOFF: WOFF is a proposed standard format currently supported by Firefox. Microsoft has hinted at supporting this format in IE.

  • EOT: This is Microsoft's proprietary embedded font format. It only works in IE, but to be fair, Microsoft has had embedded font support for many years.

You can use a font conversion tool like onlinefontconverter.com/ to convert to whatever font format you prefer.

It's possible to supply multiple src attributes. This way, you can include both an EOT and OTF version of a font so that it will work on a wide variety of browsers.

When you use this technique, you need to have a copy of the font file locally. It should be in the same directory as your web page. When you begin hosting on a web server, you'll want to move your font file to the server along with all the other resources your web page needs. Just because you can include a font doesn't mean you should. Think carefully about readability.

Also, be respectful of intellectual property. Fortunately there are many excellent free open-source fonts available. Begin by looking at Open Font Library. Google Fonts is another great resource for free fonts. With the Google Font tool, you can select a font embedded on Google's servers, and you can copy code that makes the font available without downloading.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].