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

如何解决localhost中Google Fonts无法加载的问题?

问题排查:Localhost下@import引入Google Fonts无效的原因

核心问题是你代码里的**@import语句末尾缺少分号**,这属于CSS语法错误,和woff2格式没有关系。

CSS语法要求所有语句必须以分号结尾,@import作为规则语句也得遵守这个规范。少了分号的话,浏览器没法正确识别这条规则的结束位置,会影响后续的全局字体样式解析,最终导致字体无法生效。

修正后的代码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
    <style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
* {
  font-family: 'Poppins', sans-serif;
}
    </style>
  </head>
  <body>
    <p>trying</p>
  </body>
</html>

另外给你两个额外的排查方向:

  • 按F12打开浏览器控制台,切换到「网络」面板,刷新页面后查看Google Fonts的请求是否返回200状态码,如果是404或其他错误,可能是网络访问问题
  • 也可以尝试用link标签替代@import引入字体,写法如下:
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap">

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:42:08