Custom fonts make your website look unique and match your style. They help your text stand out and improve user experience.
Custom font integration in Tailwind
@font-face {
font-family: 'MyCustomFont';
src: url('/fonts/MyCustomFont.woff2') format('woff2');
font-weight: 400;
font-style: normal;
}
/* Then in Tailwind config (tailwind.config.js): */
module.exports = {
theme: {
extend: {
fontFamily: {
custom: ['MyCustomFont', 'sans-serif'],
},
},
},
};The @font-face rule loads your font file so browsers can use it.
In Tailwind config, you add your font name under fontFamily to use it with utility classes.
open for Tailwind usage.@font-face {
font-family: 'OpenSans';
src: url('/fonts/OpenSans-Regular.woff2') format('woff2');
font-weight: 400;
font-style: normal;
}
module.exports = {
theme: {
extend: {
fontFamily: {
open: ['OpenSans', 'sans-serif'],
},
},
},
};font-custom in your HTML to apply the custom font.<p class="font-custom">This text uses the custom font.</p>@font-face rules with different font-weight.@font-face {
font-family: 'MyFont';
src: url('/fonts/MyFont-Bold.woff2') format('woff2');
font-weight: 700;
font-style: normal;
}
module.exports = {
theme: {
extend: {
fontFamily: {
myfont: ['MyFont', 'sans-serif'],
},
},
},
};This example loads a custom font (Roboto from Google Fonts URL) using @font-face. Then it extends Tailwind's fontFamily with custom. The heading and paragraph use font-custom class to show the font.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Custom Font with Tailwind</title> <script src="https://cdn.tailwindcss.com"></script> <style> @font-face { font-family: 'MyCustomFont'; src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu4mxM.woff2') format('woff2'); font-weight: 400; font-style: normal; } </style> <script> tailwind.config = { theme: { extend: { fontFamily: { custom: ['MyCustomFont', 'sans-serif'], }, }, }, }; </script> </head> <body class="p-8"> <h1 class="font-custom text-3xl">Hello with Custom Font!</h1> <p class="mt-4 font-custom text-lg">This text uses the custom font loaded with @font-face and Tailwind.</p> </body> </html>
Make sure your font files are in the correct folder and paths are right.
Use modern font formats like WOFF2 for better browser support and performance.
Test your font on different devices to ensure it looks good everywhere.
Custom fonts help your site look unique and match your style.
Use @font-face to load fonts and extend Tailwind's fontFamily to use them.
Apply your custom font with Tailwind classes like font-custom.
Practice
@font-face in Tailwind CSS when integrating a custom font?Solution
Step 1: Understand the role of
@font-face@font-faceis a CSS rule that lets you load a custom font file into your website.Step 2: Connect
After loading the font with@font-faceto Tailwind usage@font-face, you extend Tailwind'sfontFamilyto use that font in your classes.Final Answer:
To load and define a custom font so it can be used in your styles -> Option AQuick Check:
@font-faceloads fonts = D [OK]
@font-face loads fonts, not styles [OK]- Thinking
@font-facechanges font size - Confusing font loading with animations
- Assuming it downloads fonts automatically
fontFamily in tailwind.config.js to add a custom font named 'MyFont'?Solution
Step 1: Recall Tailwind fontFamily syntax
Tailwind expects font families as arrays with font names and fallbacks, e.g.,fontFamily: { key: ['FontName', 'fallback'] }.Step 2: Match correct syntax
fontFamily: { custom: ['MyFont', 'sans-serif'] } correctly usescustomas key and an array with 'MyFont' and 'sans-serif'. Others misuse keys or values.Final Answer:
fontFamily: { custom: ['MyFont', 'sans-serif'] } -> Option CQuick Check:
fontFamily uses arrays for fonts = C [OK]
- Using strings instead of arrays for fontFamily
- Swapping keys and values
- Missing fallback fonts
fontFamily: { custom: ['MyFont', 'sans-serif'] }and this HTML:
<p class="font-custom">Hello</p>
What font will the paragraph use if 'MyFont' is loaded correctly?
Solution
Step 1: Understand class usage
The classfont-customapplies the font family named 'custom' from Tailwind config.Step 2: Check font loading
If 'MyFont' is loaded correctly via@font-face, it will be used first, with 'sans-serif' as fallback.Final Answer:
'MyFont' custom font -> Option DQuick Check:
font-custom uses 'MyFont' if loaded = A [OK]
- Assuming fallback font is always used
- Confusing class name with default fonts
- Thinking text disappears without font
@font-face { font-family: 'MyFont'; src: url('/fonts/MyFont.woff2'); }But your text with
font-custom class still shows default font. What is the likely mistake?Solution
Step 1: Check font-face usage
The font is loaded with@font-face, so the font file is available.Step 2: Verify Tailwind config
Without extendingfontFamilyintailwind.config.js, the classfont-customwon't apply the new font.Final Answer:
You forgot to extendfontFamilyintailwind.config.js-> Option BQuick Check:
fontFamily extension missing = B [OK]
- Not extending fontFamily in config
- Using wrong class names
- Forgetting to rebuild Tailwind after config change
tailwind.config.js and your CSS to use font-boldcustom and font-lightcustom classes for them?Solution
Step 1: Define separate
Each font variant needs its own@font-facerules@font-facewith uniquefont-familynames like 'MyFontBold' and 'MyFontLight'.Step 2: Extend Tailwind's
InfontFamilytailwind.config.js, add keys 'boldcustom' and 'lightcustom' pointing to these font-family names so you can usefont-boldcustomandfont-lightcustomclasses.Final Answer:
Define two @font-face rules and extend fontFamily with 'boldcustom' and 'lightcustom' -> Option AQuick Check:
Separate font-faces + fontFamily keys = A [OK]
- Trying to combine fonts in one @font-face
- Using CSS font-weight instead of separate font-family
- Not extending fontFamily for each font variant
Ready to prove it?
Pass the test and this pattern moves unknown
