You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目中引入Google字体失效问题求助

Troubleshooting Google Fonts Not Working in Laravel (Works in Static Site)

Hey there, let's break down why your Google Fonts are working in a static site but failing in Laravel—this is a common gotcha, usually tied to how Laravel handles asset loading or template rendering. Here's what to check first, plus what code snippets you should share to get more targeted help:

Common Causes & Quick Fixes

  • Incorrect Asset Paths for Local Fonts: If you downloaded the font files instead of using the CDN, static sites might use relative paths like ../fonts/Roboto-Regular.ttf, but Laravel requires you to use its asset helpers to generate valid URLs. Replace relative paths with asset('fonts/Roboto-Regular.ttf') (assuming fonts are stored in public/fonts/) or mix('fonts/Roboto-Regular.ttf') if you're using Laravel Mix.
  • CDN Placement Issues: If you're using the Google Fonts CDN link, make sure it's placed before your custom CSS in the <head> section of your Blade layout. If it's loaded after your styles, the font won't be available when the browser parses your font-family rules.
  • Caching Gotchas: Laravel caches views and compiled assets by default. Run these commands to clear stale cache:
    php artisan view:clear
    php artisan cache:clear
    php artisan config:clear
    
    Also, do a hard refresh in your browser (Ctrl+Shift+R on Windows/Linux, Cmd+Shift+R on Mac) to bypass browser cache.
  • Style Priority Conflicts: Check if another CSS rule is overriding your font-family declaration. Use your browser's dev tools (Inspect Element) to see if the font rule is crossed out—this means a higher-priority selector (like an ID or !important flag) is taking over.

Code Snippets to Share for Further Help

To diagnose this properly, please share:

  • The code you're using to load the Google Fonts (either the CDN <link> tag or your @font-face CSS rules for local files)
  • The CSS that applies the font to elements (e.g., body { font-family: 'Roboto', sans-serif; })
  • The section of your Blade layout where you include your CSS and font links (e.g., how you're using @vite, mix(), or plain <link> tags)
  • If using local fonts, the exact file path of your font files in the Laravel project (e.g., public/fonts/Roboto/)

内容的提问来源于stack exchange,提问作者Lien Lien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 06:22:44