I'm trying to use a custom font (RetroGaming.ttf) in my React project using Vite and Tailwind CSS v4, but the font isn't applying. The @font-face rule seems ignored, and the generated Tailwind class doesn't include the custom font in its font-family property.
Tech Stack
- Framework: React (^19.0.0)
- Build Tool: Vite (^6.2.0)
- Styling: Tailwind CSS (^4.1.3)
- CSS Processing: PostCSS (^8.5.3) with
autoprefixerand@tailwindcss/postcss - Font File: [RetroGaming.ttf]
Goal
Define RetroGamingFont via @font-face and apply it using a Tailwind utility class font-custom-retro.
Problem
- The custom font doesn't render; elements use the fallback font.
- The production CSS bundle (
dist/assets/) lacks the@font-facerule. - The generated
.font-custom-retroclass only includes the fallback font (sans-serif), notRetroGamingFont. - Browser dev tools show no 404 error for [/fonts/RetroGaming.ttf]
- The issue persists whether
@font-faceis insrc/index.cssor directly inindex.html.
Configuration
tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {
fontFamily: {
'custom-retro': ['RetroGamingFont', 'sans-serif'], // Font definition with fallback
},
},
},
plugins: [],
}
postcss.config.js
export default {
plugins: {
'@tailwindcss/postcss': {}, // Required for v4
autoprefixer: {},
},
}
src/index.css (Currently empty regarding @font-face)
@import "tailwindcss"; /* Tailwind v4 import, placed first */
/* @font-face moved to index.html for testing */
index.html (Current state)
<!-- Inside <head> -->
<style>
@font-face {
font-family: 'RetroGamingFont';
src: url('/fonts/RetroGaming.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
</style>
Usage in CompatibilityForm.jsx
<h2 className="text-2xl font-custom-retro text-pink-400">...</h2>
<label className="font-custom-retro text-indigo-200">...</label>
What I've Tried
Correct Tailwind v4 Setup:
- Using
@import "tailwindcss";insrc/index.css - Using
@tailwindcss/postcssplugin, no@tailwindcss/viteplugin.
@font-face Placement:
- Tried in
src/index.css(after@import) and directly inindex.html<style>tag.
Neither worked.
File Path Verification:
- Confirmed
public/fonts/RetroGaming.ttfexists and/fonts/RetroGaming.ttfpath is correct for public assets.
Font Name:
- Simplified to
RetroGamingFont(no spaces).
Dev Tools Inspection:
- Verified
.font-custom-retroclass is applied, but computedfont-familyuses the fallback. - No network errors for the font file.
Question
Why isn't Tailwind CSS v4 / PostCSS / Vite correctly processing or recognizing the @font-face rule, preventing the custom font from being included in the font-family definition of the utility class? Is there a specific configuration step for custom fonts with Tailwind v4 and Vite that I'm missing?
@tailwindcss/viteplugin, I'll still leave a guide here on how you can use TailwindCSS directly in Vite without PostCSS: How to install TailwindCSS v4 & Vite without PostCSS