This has been bugging me for years and I can’t believe it’s still happening.
Being able to use custom fonts has been a huge boon to web design. And font hosting services like Typekit (sorry, I will never call it Adobe Fonts) and Google Fonts make using custom fonts easy.
But sometimes you still buy a font license that involves hosting the font files directly on your own server, and that’s where things get absolutely maddening because, for some reason, someone early on grossly misinterpreted how to use @font-face
and that error has been perpetuated by countless unthinking others.
(Yes, I’m being harsh. But this is really not that complicated. And getting it right makes writing your CSS and HTML so much easier.)
Here’s an example of some font-specifying CSS you might receive from a font vendor:
@font-face {
font-family: 'Font-Name-Regular';
src: url('Font-Name/Font-Name-Regular.woff2') format('woff2'),
url('Font-Name/Font-Name-Regular.woff') format('woff');
font-weight: normal;
}
.Font-Name-Regular {
font-family: 'Font-Name-Regular';
}@font-face {
font-family: 'Font-Name-Regular';
src: url('Font-Name/Font-Name-Regular-Italic.woff2') format('woff2'),
url('Font-Name/Font-Name-Regular-Italic.woff') format('woff');
font-weight: normal;
font-style: italic;
}
.Font-Name-Regular-Italic {
font-family: 'Font-Name-Regular';
font-style: italic;
}@font-face {
font-family: 'Font-Name-Bold';
src: url('Font-Name/Font-Name-Bold.woff2') format('woff2'),
url('Font-Name/Font-Name-Bold.woff') format('woff');
font-weight: normal;
}
.Font-Name-Bold {
font-family: 'Font-Name-Bold';
}@font-face {
font-family: 'Font-Name-Bold';
src: url('Font-Name/Font-Name-Bold-Italic.woff2') format('woff2'),
url('Font-Name/Font-Name-Bold-Italic.woff') format('woff');
font-weight: normal;
font-style: italic;
}
.Font-Name-Bold-Italic {
font-family: 'Font-Name-Bold';
font-style: italic;
}
This is, in fact, the exact code I just received yesterday from a font vendor when I purchased a license, with the actual font name removed to protect the guilty innocent.
What’s so bad about this, you might ask? Aside from the conventions I dislike of indenting the closing }
and using 4 spaces instead of tabs, there are two glaring problems with this:
- Because the
font-family
name defined for each weight and style is different, when you go to use this font, you need to specify thefont-family
every time you want to use bold or italics in your HTML, or at least use the custom CSS classes defined here. No! No no no! You should not have to apply a class to get bold or italics to render properly. The<strong>
and<em>
tags should do that on their own! - Don’t f***ing define a bold font with
font-weight: normal;
! If you don’t realize from this, alone, that something is wrong with your approach, stop coding right now.
So, how should this be done, you ask?
Well, it’s simple. Each @font-face
declaration has four properties. One is src:
which tells the browser where to find the correct font file(s) for this face. The other three properties work together to define the context in which this particular src
should be used: any time this combination of font-family
, font-weight
and font-style
come together.
You can use the same font-family
in different @font-face
declarations as long as font-weight
and font-style
are different. In fact, you’re supposed to! That’s the way it’s designed to work!!!
When you do this properly, you don’t need any custom CSS classes. Try this on for size:
@font-face {
font-family: 'Font-Name';
src: url('Font-Name/Font-Name-Regular.woff2') format('woff2'),
url('Font-Name/Font-Name-Regular.woff') format('woff');
font-weight: normal;
}@font-face {
font-family: 'Font-Name';
src: url('Font-Name/Font-Name-Regular-Italic.woff2') format('woff2'),
url('Font-Name/Font-Name-Regular-Italic.woff') format('woff');
font-weight: normal;
font-style: italic;
}@font-face {
font-family: 'Font-Name';
src: url('Font-Name/Font-Name-Bold.woff2') format('woff2'),
url('Font-Name/Font-Name-Bold.woff') format('woff');
font-weight: bold;
}@font-face {
font-family: 'Font-Name';
src: url('Font-Name/Font-Name-Bold-Italic.woff2') format('woff2'),
url('Font-Name/Font-Name-Bold-Italic.woff') format('woff');
font-weight: bold;
font-style: italic;
}
Aside from the fact that this eliminates 1/3 of the lines of code, it also will make your HTML much cleaner and more properly separates content from styling.
Here’s an example of some HTML you might have to write using the first approach:
<p class="Font-Name-Regular">This is some regular text, which also
includes a bit of <em class="Font-Name-Regular-Italic">italics</em>
and even a dash of <strong class="Font-Name-Bold">bold</strong>.</p>
Now, granted, my version does require you to define the font-family
for your <p>
tags in your CSS file. But guess what… you’re supposed to do that! Put this in your CSS:
p { font-family: 'Font-Name'; }
With that in place, the proper HTML for the same appearance becomes this:
<p>This is some regular text, which also
includes a bit of <em>italics</em>
and even a dash of <strong>bold</strong>.</p>
So, again… when thinking about @font-face
, just remember these two simple things:
- All
@font-face
declarations for the same font family should have the samefont-family
. (Seems kind of obvious when I put it that way, doesn’t it?) - The value for
font-weight
should be the actual weight of the font. Only the “regular” weight should havefont-weight: normal;
orfont-weight: 400;
. If you’re usingfont-weight: normal;
on a bold font, you’ve done something wrong.
This change makes for cleaner code, easier maintenance, and proper separation of content from design.
Addendum
Shortly after I posted this, I went back and looked at the unnamed font vendor’s sample page, because I knew it referenced “the @font-face standard since 2017”. I could not believe that this approach was actually a “standard,” so I tracked down the source, an article Bram Stein published on A List Apart in 2017 called Using Webfonts.
Guess what… Bram Stein’s examples do it the right way!
I do know one place where I’ve consistently seen this wrong way I’m railing against… it’s the code generated on FontSquirrel (no link, on principle) whenever you download a font. Other “web font generator” sites like FontSquirrel probably do it to. They’re all wrong… but Bram Stein isn’t. Don’t drag him down with this bad code!