CHAPTER 52
CSS Web Fonts
Learn how custom web fonts can be loaded and used in websites.
01. What is CSS Web Fonts?
Web fonts allow websites to use fonts that may not be installed on the user's computer.
@font-face can define a custom font.
External font services can also provide web fonts.
02. Syntax
@font-face {
font-family: "MyFont";
src: url("font.woff2");
}
body {
font-family: "MyFont";
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: "DemoFont";
src: url("font.woff2");
}
body {
font-family: "DemoFont", Arial, sans-serif;
}
</style>
</head>
<body>
<h1>AIFA Coding Lab</h1>
<p>Custom web font example.</p>
</body>
</html>
04. Important Points
✓
@font-face defines a custom font.
✓
Always provide suitable fallback fonts.
✓
Modern font formats such as WOFF2 are commonly used.
✓
Font files should be optimized for website performance.
05. Common Mistakes
⚠
Loading unnecessarily large font files.
⚠
Using too many font families.
⚠
Forgetting fallback fonts.
⚠
Using fonts that reduce readability.
06. Real World Use
Web fonts are used in branding, headings, landing pages, education websites and modern web applications.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create a Font Setup
Create an @font-face rule and apply the custom font to the body.