Github Pages中index.html头部引入的Google Fonts无法生效问题排查
href Links on GitHub Pages 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 oldcssendpoint, 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, whiletype="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 removedhttp:but didn’t switch tohttps:or use the protocol-relative//, make sure your link starts with eitherhttps://(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.,Robotinstead ofRoboto) 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




