CSS
🎯 What is @font-face?
The CSS @font-face rule allows you to use custom fonts on your website even if they are not installed on the user’s device.
Normally, a font must be installed on the user’s computer to be used. However, with @font-face, font files are uploaded to your server and automatically downloaded by the browser.
In short: You can use fonts that are not installed on the user's device.
🧩 Properties Inside @font-face
1️⃣ font-family
Defines the name of the font. You will use this name in your CSS.
font-family: "MyCustomFont";
2️⃣ src
Specifies the path of the font file. The browser downloads the font from this location.
src: url("fonts/myfont.woff2");
3️⃣ local
Uses the font if it already exists on the user’s device; otherwise, it downloads it from the server.
4️⃣ font-style
Defines whether the font is normal, italic, etc.
5️⃣ font-weight
Specifies the thickness of the font (400, 700, bold, etc.).
📁 Font File Types
Different browsers support different font formats, so multiple formats are used for compatibility.
- EOT → Internet Explorer, old Edge
- WOFF2 → Modern browsers (Chrome, Firefox, Opera)
- WOFF → All modern browsers
- TTF / OTF → General support
- SVG → Older iOS / Safari versions
That’s why multiple formats are defined inside @font-face.
🧪 Example Usage
For example, if you want to use a font called “HandwrittenFont”:
@font-face {
font-family: "HandwrittenFont";
src: url("fonts/handwritten.woff2") format("woff2"),
url("fonts/handwritten.woff") format("woff"),
url("fonts/handwritten.ttf") format("truetype");
}
Then you can use it like this:
h1 {
font-family: "HandwrittenFont";
}
The browser downloads the best supported format automatically and applies the font.
📌 How It Works
- The font is uploaded to the server.
- The browser checks available formats.
- The best supported format is downloaded.
- The font is applied to the text.
-
🧠 Summary
- @font-face allows custom fonts to be used on websites.
- Fonts are downloaded from the server by the browser.
- Multiple formats ensure cross-browser compatibility.
- Once defined, fonts behave like standard system fonts.
