If you are familiar with letterpress printing, you may have seen drawers containing individual metal sorts, which collectively make up sets of fonts. In that context, a font was the complete set of metal types in a specific style that were used to typeset entire pages of text.
In the digital world, instead of metal sorts, we have digital files. And all digital files are really just long lists of numbers stored as binary on a storage device, and file formats are what allows us to read and understand the data these numbers represent.
Digital fonts are just another type of file format. For both analogue and digital fonts, you can think of them as collections of glyphs. Now, what is a glyph?
A glyph is the specific shape of a letter or character in a particular font. In the digital world, everything is data. So glyphs can be described as an array of pixels, or collections of vector images or even paths of Bézier curves and straight lines.
Different font formats store information about the font, like their glyphs, encodings, metadata about the font and so on, differently based on how their specifications were written.
Web fonts
When the World Wide Web was first introduced back in 1989, options for displaying text in the browser where limited to system fonts, which are fonts that come with the operating system installed on your computer.
It was not until 1996, when the Web Fonts Working Group of the W3C started working on Web Fonts as a feature, which would allow folks to use fonts of their choice on their websites rather than just be limited to system fonts.
Today, we are able to specify the fonts we want our content to be rendered in by telling the browser exactly where to find the font files. These font files do not necesarily have to be on the user’s computer, and can be hosted somewhere on the internet. Your user’s browser will then also download the font file together with the rest of the content on your web page.
Using Google fonts
Google Fonts is a good option for folks starting out with web development and want to use a custom font without too much hassle. Once you pick out fonts you like, you can select which styles of that font you want. Google Fonts will generate the code you need under the Selected families side panel on the right.
There are 2 options available, <link> and @import. The difference between the 2 is that the <link> option must be added to the <head> of your HTML document, while the @import option can be added to any CSS file (without the <style> tag).
For example, if you want to use the font, Crimson Pro, in the 200, italic style, you can use the <link> option like this:
<link rel="preconnect" href="https://fonts.googleapis.com" /><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /><link href="https://fonts.googleapis.com/css2?family=Crimson+Pro:ital,wght@1,200&display=swap" rel="stylesheet"/>And in your CSS file, you would use the font-family property on elements like this:
h1 { font-family: 'Crimson Pro', serif;}
blockquote { font-family: 'Crimson Pro', serif;}For the @import option, instead of copying the code from Google Fonts directly with the <style> tag included, you can just use the @import at the top of your CSS file like this:
@import url('https://fonts.googleapis.com/css2?family=Crimson+Pro:ital,wght@1,200&display=swap');And use the font-family property like this in the same way as the first option. If you’re interested in what Google Fonts is actually doing (or if you’d like to host your own fonts), read on. Otherwise you can skip to the next section.
The @font-face rule
If you are following this course in order, you might remember something called at-rules back in CSS Syntax and Terminology. Most of the CSS we have gone through so far involve targeting specific elements on the page. At-rules are more like an FYI to the browser.
The @font-face rule tells the browser information about the font you want to use, and the circumstances when the font should be used. It looks something like this:
@font-face { font-family: 'Bellato'; src: url('./fonts/Bellota-Regular.woff2') format('woff2'), url('./fonts/Bellota-Regular.woff') format('woff'); font-style: normal; font-weight: 400;}These 4 font descriptors are most commonly used in @font-face rules. The font-family descriptor is like a label for what name you want the font to be. Usually, you would use the name of the font, but in theory, you can name it anything you like, and refer to that in your CSS rules like so:
@font-face { font-family: 'Dinosaur'; src: url('./fonts/Bellota-Regular.woff2') format('woff2'), url('./fonts/Bellota-Regular.woff') format('woff'); font-style: normal; font-weight: 400;}
h3 { font-family: 'Dinosaur', sans-serif;}The browser will still be loading the file Bellota-Regular.woff2 or Bellota-Regular.woff since that is specified with the src descriptor, which tells the browser where to find the font file.
If the font file is in the same project as the rest of your code, you can use a relative file path like the example above. But if your fonts are hosted somewhere else, you would use the absolute URL instead.
The font-style and font-weight are optional descriptors are used to specify the style and weight of the font. The font-style defaults to normal, while font-weight defaults to 400. Ideally, you would want to have the italic and heavier weight styles of the typeface you picked as well.
Rather than giving each font file a different name, you can use the same font-family descriptor for multiple font files, and tell the user when to load which file depending on the style required.
@font-face { font-family: 'Bellota'; src: url('./fonts/Bellota-Regular.woff2') format('woff2'), url('./fonts/Bellota-Regular.woff') format('woff'); font-style: normal; font-weight: 400;}
@font-face { font-family: 'Bellota'; src: url('./fonts/Bellota-Italic.woff2') format('woff2'), url('./fonts/Bellota-Italic.woff') format('woff'); font-style: italic;}
@font-face { font-family: 'Bellota'; src: url('./fonts/Bellota-Bold.woff2') format('woff2'), url('./fonts/Bellota-Bold.woff') format('woff'); font-weight: 700;}With that, when you specify font styles or weights in your CSS, the browser will know which source font file to refer to based on which font descriptors match your font properties:
blockquote { font-family: 'Bellota', sans-serif; font-style: italic; /* browser will know to load the italic font file */}Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson