Manage Web Fonts using CSS3

By Rob Gravelle

https://www.htmlgoodies.com/beyond/css/manage-web-fonts-using-css3.html (Back to article)

<img id="__mce_tmp" /><span id="mce_marker" data-mce-type="bookmark"></span><span id="__caret">_</span>Manage Web Fonts using CSS3

In the early days of the World-Wide-Web, developers had to content themselves with "web-safe" colors and fonts. Using the CSS font-family property, one had to supply several font names as a "fallback" hierarchy, in order to ensure maximum compatibility between browsers and the users' operating systems. That way, if the browser did not support the first font, it would try the next font in the list. Therefore, the optimal strategy was to start with the font you wanted, and end with a generic family, so that the browser could pick a similar font in the generic family, should no other font be available. Today, Web fonts allow Web designers to use any font they wish, whether or not they are installed on the user's computer. In today's article, we'll learn how to employ Web fonts in your web pages.

Finding a Font File

There are font files all over the Web. Just Google "Web fonts" and you'll see! One site in particular called fontsquirrel.com has amassed a large number of free Web fonts that you can download. The only challenge is that they don't host the files, so you have to follow the link to the originating site. From there, there may be a bit of work involved in locating the free version of your desired font. I picked the "Intro Rust" font, which eventually led me to this page. In order to download the font, I had to add it to my cart, and select the free webfont option. To check out, I had to create an account, before getting my download button.


Choosing a File Format

There are several types of font formats to choose from, each with their own pros and cons:

Unfortunately, not all font formats are supported by every browser:

Font formatInternet ExplorerChromeFirefoxSafariOpera
TTF/OTF 9.0* 4.0 3.5 3.1 10.0
WOFF 9.0 5.0 3.6 5.1 11.1
WOFF2 Not supported 36.0 35.0* Not supported 26.0
SVG Not supported 4.0 Not supported 3.2 9.0
EOT 6.0 Not supported Not supported Not supported Not supported

Using the Custom Font in Your Web Pages

Being part of a collection, the Intro Rust font that I wanted to use was contained in an archive file along with many other files. Inside the "Webfonts\introrustg_base2line_macroman" folder, there was an HTML demo file named "introrustg-base2line-demo.html". Opening it in a browser displayed a page with four tabs: "SPECIMEN", "SAMPLE LAYOUT", "Glyphs & Languages", and "Installing Webfonts". The latter contains the same information as the How_to_use_webfonts.html file located in the Reference folder. The fastest way to set up the fonts is to upload all of the files contained in the "Webfonts\introrustg_base2line_macroman" folder to your CSS directory on your web server. Then reference the stylesheet.css file within your page using a LINK tag:

<link rel="stylesheet" href="stylesheet.css" type="text/css" charset="utf-8" />

The instructions also show the contents of the stylesheet.css file. It utilizes "Bulletproof @Font-Face Syntax" that aims to deal with Internet Explorer issues as well as deliver consistent results across Safari, Firefox, Chrome 8, iOS, Android, and Opera.

        font-family: 'MyWebFont';
        src: url('WebFont.eot');
        src: url('WebFont.eot?#iefix') format('embedded-opentype'),
             url('WebFont.woff') format('woff'),
             url('WebFont.ttf') format('truetype'),
             url('WebFont.svg#webfont') format('svg');

Thanks to the above CSS, the font file(s) will be automatically downloaded by the browser when needed.

To apply the font to page elements, use the font-family CSS attribute as you normally would, and ascribe the font-family name declared in the CSS file. Unless you change it, the default value is "MyWebFont". It's still suggested that you include backup values such as a standard web-safe font and generic family, even though they will only come into play if there is a problem downloading the font file(s) or the browser is a weird one!

p { font-family: 'MyWebFont', Arial, sans-serif; }


There are other ways to acquire custom fonts besides visiting specialized font sites. Remember that the browser downloads the files as required, so spending money on a really fancy font hardly seems worth it, unless you're OK with distributing the files to your visitors free of charge. In an up-coming article, I'll reveal how to snag files from other websites. Afterall, what is the Web if not a wild animal that canibalizes itself!

Rob Gravelle

Rob Gravelle resides in Ottawa, Canada, and is the founder of GravelleWebDesign.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses.

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His band, Ivory Knight, was rated as one of Canada's top hard rock and metal groups by Brave Words magazine (issue #92) and reached the #1 spot in the National Heavy Metal charts on Reverb Nation.