The @font-face Rule
What you'll learn: How to use the CSS @font-face rule to declare custom font files and make them available throughout your webpage.
What Is @font-face?
The @font-face rule is your way of telling the browser: "Hey, I have a special font file I want you to download and use." Instead of relying only on fonts already installed on your visitor's computer, you can load custom typefaces from files hosted on your server or a CDN.
Think of @font-face as introducing a new friend to a party — you're formally declaring the font's name and telling the browser where to find it so it can be used anywhere in your stylesheets.
The Basic Syntax
Here's the structure:
@font-face {
font-family: 'MyCustomFont';
src: url('path/to/font.woff2') format('woff2');
}
Breaking it down:
font-family: The name you're giving this font (you'll reference it later in your CSS)src: Where the browser can download the font fileurl(): The path to your font fileformat(): Helps the browser know what type of font file it is (like.woff2,.woff, or.ttf)
Using Your Custom Font
Once declared, you can use it like any other font:
body {
font-family: 'MyCustomFont', sans-serif;
}
The browser will download your custom font file and apply it to elements using that font-family name. Always include fallback fonts (like sans-serif) in case the custom font fails to load.
Key Takeaway: The @font-face rule declares custom fonts by giving them a name and specifying where the browser can download the font file, making unique typography available across your entire website.