Beware the Font Name
Tags: html, css, webdev
Published: 2022-06-03 13:58:01

---
title: Beware the Font Name
published: true
description: The other day at work, we were reviewing some CSS code when we stumped upon something interesting. It was related to how we imported the new font files that we got from an agency.
tags: html,css,webdev
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx6t9qbs13nugbepsmvb.jpg
canonical_url: https://alvaromontoro.com/blog/68008/beware-the-font-name
---

The other day at work, we were reviewing some CSS code when we stumped upon something interesting. It was related to how we imported the new font files that we got from an agency.

The code looked something like this:

```scss
@font-face {
  font-family: myFont;
  src: url(myFont.eot);
  src: url(myFont.woff) format("woff"), 
       url(myFont.ttf) format("truetype");
}

@font-face {
  font-family: myFont-bold;
  src: url(myFont-Bold.otf);
}

@font-face {
  font-family: myFont-bolditalic;
  src: url(myFont-BoldItalic.otf);
}

@font-face {
  font-family: myFont-italic;
  src: url(myFont-Italic.otf);
}

@font-face {
  font-family: myFont-medium;
  src: url(myFont-Medium.otf);
}

@font-face {
  font-family: myFont-mediumitalic;
  src: url(myFont-MediumItalic.otf);
}
```

---


It was a puzzling moment that led to a more _interesting_ conversation:

> DEV 1:
> _Why are you doing it like that?_
>
> DEV 2:
> _That's how the agency provided the fonts._
> 
> DEV 1:
> _But that makes the code more difficult to maintain._
> 
> DEV 2:
> _It's not that much._
> 
> DEV 1:
> _Also, you may not be using the font you think you are using. Why not have the same name for all the fonts?_
> 
> DEV 2:
> _What do you mean by "the same name"?_
> 
> DEV 1:
> _Yes, why don't you do something like this:_
> ```scss
@font-face {
  font-family: myFont;
  src: url(myFont.eot);
  src: url(myFont.woff) format("woff"), 
       url(myFont.ttf) format("truetype");
}
> 
@font-face {
  font-family: myFont;
  src: url(myFont-Bold.otf);
  font-weight: bold;
}
> 
@font-face {
  font-family: myFont;
  src: url(myFont-BoldItalic.otf);
  font-style: italic;
  font-weight: bold;
}
> 
@font-face {
  font-family: myFont;
  src: url(myFont-Italic.otf);
  font-style: italic;
}
> 
@font-face {
  font-family: myFont;
  src: url(myFont-Medium.otf);
  font-weight: 500;
}
> 
@font-face {
  font-family: myFont;
  src: url(myFont-MediumItalic.otf);
  font-style: italic;
  font-weight: 500;
}
```
>
> <br>DEV 2:
_Wait, you cannot do that! ...Can you?!_

I have to admit, at this point, I doubted myself and had to pause to verify that the suggested alternative was, in fact, correct.

## The test

But even after the explanation, the other developer didn't seem too convinced, so we created a simple example:

```html
<section>
  <h1>This is a title</title>
  <p>
    And this is a paragraph with <b>bold text</b> using
    the <strong>b and strong tags</strong>.
  </p>
</section>
```

The CSS looked like the first import, plus these rules: 

```css
body {
  font-family: myFont, sans-serif;
}

h1, h2, h3, h4 {
  font-family: myFont-bold, sans-serif;
}
```

The text had the new font, and the titles, `<b>`, and `<strong>`, looked bold. **Everything seemed correct... but it really wasn't**. And it was easier to see after we replaced the bold font with a new font that looked completely different, like a handwriting font:

```scss
@font-face {
  font-family: myFont-bold;
  src: url(Handwriting-bold.otf);
}
```

After that, we reloaded the page, and... the headings looked bold and had the new font, and the `<b>` and `<strong>` elements looked bold, too... but they have the old font!

In reality, the browser had synthetically emboldened the regular font to approximate how it should look. But that was not the bold font. An example on the [W3C font module definition](https://drafts.csswg.org/css-fonts/#example-f67d9f89) explains this effect and process in detail.

Now, it was apparent where the problem was and what we needed to do. **If we kept the different names for all the fonts, we would need to redefine the font for every single element and situation**. Otherwise, we would just be using the regular font (modified by the browser to look correctly) instead of the custom fonts we had.

## Conclusion

Our team had paid for an agency to create fancy fonts, and then we were not really using them because we didn't incorporate the fonts properly in CSS.

We were specifying the font for some particular elements (e.g., headings) but not for all elements (e.g., missing `<b>` and `<strong>`), and that made the browser generate _fake_ bold/italics versions of the font, instead of the actual bold/italics fonts that we were importing (and paid for!)

We should **let the browser handle the font complexity** instead of trying to go around it by using different names. Having the same name (specifying the font-weight and style) makes things smoother and easier to maintain in the long run.

## Bibliography

- [CSS Tricks: What’s the deal with declaring font properties on @font-face?](https://css-tricks.com/whats-deal-declaring-font-properties-font-face/)
- [Hacksoft: Using multiple font files the right way](https://www.hacksoft.io/blog/using-multiple-font-files-the-right-way)
- [W3C: CSS Fonts Module Level 4 (editor's draft)](https://drafts.csswg.org/css-fonts/#example-f67d9f89)

Cover [photo by Brett Jordan](https://unsplash.com/photos/O0OCxZf0fTk) on Unsplash.