求助:@font-face在GitHub Pages中无法生效的问题排查
Hey there! Let’s dig into why your custom fonts aren’t loading on GitHub Pages when they work perfectly locally. I’ve looked at your repo and live site, so here are the key fixes to test out:
Double-check font file paths
Local file systems can be forgiving with paths, but GitHub Pages follows strict relative/root path rules. If your CSS is in a subfolder (like/css/style.css) and fonts are in/fonts/, make sure yoursrcpath is relative to the CSS file—for example,../fonts/your-font.ttfinstead of justfonts/your-font.ttf. You can also use the full project root path (since GitHub Pages serves from your repo name):/StarWarsRPG/fonts/your-font.ttf. To confirm, open your browser’s DevTools > Network tab, filter for font files, and check if any return a 404 error (that means the path is wrong).Fix case sensitivity issues
GitHub runs on Linux, which is case-sensitive for file names—unlike Windows/macOS (by default). If your @font-face code referencesStarJedi.ttfbut the actual file in your repo isstarjedi.ttf, GitHub Pages will throw a 404. Go through your CSS and repo files to make sure every letter’s case matches exactly.Validate your @font-face syntax
Make sure you’re including all necessary font formats (woff2, woff, ttf are the most widely supported) and that eachsrcentry has the correct path and format declaration. For example:@font-face { font-family: 'StarJedi'; src: url('../fonts/StarJedi.woff2') format('woff2'), url('../fonts/StarJedi.woff') format('woff'), url('../fonts/StarJedi.ttf') format('truetype'); font-weight: normal; font-style: normal; }Also, ensure the
font-familyname you use in your CSS (likefont-family: 'StarJedi';) matches exactly what’s defined in @font-face.Verify GitHub Pages deployment status
Sometimes deployments fail silently. Head to your repo’s Settings > Pages and check the deployment status at the top. If there’s an error message, follow its instructions to fix the issue (like missing files or build errors).Clear browser cache
Your browser might be holding onto an old version of your CSS file that doesn’t have the correct font paths. Try a hard refresh (Ctrl+Shift+R on Windows, Cmd+Shift+R on Mac) or open your site in incognito mode to rule out caching.
内容的提问来源于stack exchange,提问作者Brooke Mortensen

