You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Github Pages中index.html头部引入的Google Fonts无法生效问题排查

Hey there! I’ve run into this exact snag before with GitHub Pages and external font resources, so let’s break down what might be going wrong and how to fix it.

Common Causes & Fixes

  • Outdated Google Fonts URL Format
    Google Fonts has moved to a v2 API (css2) that’s more reliable for HTTPS environments like GitHub Pages. If you’re still using the old css endpoint, switching to the v2 version often resolves loading issues. For example:

    <!-- Old (may fail on HTTPS sites) -->
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto">
    
    <!-- New, recommended format -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap">
    

    The v2 link includes explicit weight specifications and display=swap (for better loading performance) — it’s optimized specifically for modern secure environments.

  • Missing or Incorrect Link Attributes
    Double-check your <link> tag has all required attributes. Small omissions can break resource loading:

    <!-- Correct structure -->
    <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=YourFont">
    

    rel="stylesheet" is mandatory, while type="text/css" helps older browsers interpret the resource correctly.

  • Protocol Mismatch or Mixed Content Blocks
    GitHub Pages serves your site over HTTPS, so any HTTP-only resources will be blocked by modern browsers. If you removed http: but didn’t switch to https: or use the protocol-relative //, make sure your link starts with either https:// (most explicit) or // to avoid mixed content errors.

  • Browser Cache or Extension Interference
    Sometimes your browser caches a broken version of the font resource. Try a hard refresh (Ctrl+Shift+R) or test in an incognito window to bypass cache. Also, ad blockers or privacy extensions might block Google Fonts — temporarily disable them to rule this out.

  • Typos in Font Names or Parameters
    A tiny typo in the font family name (e.g., Robot instead of Roboto) or weight values will prevent the font from loading. Double-check your URL against the link generated directly from Google Fonts’ official interface to ensure accuracy.

Quick Test

If you’re still stuck, replace your current font link with the Roboto example above. If that loads successfully, the issue was with your original URL’s structure or parameters.

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

火山引擎 最新活动