如何让Google Font代码在HTML中正常生效?解决Neocities平台字体添加时的“Name entity expected. Got none”错误
That error almost always boils down to unescaped special characters in your HTML—specifically the & symbol in your Google Fonts URL. Let's break this down and fix it step by step:
Why the error happens
Neocities uses a strict HTML parser that treats & as the start of an HTML entity (like & for & or < for <). In your Google Fonts link, the &display=swap part has an unescaped &, which the parser tries to interpret as an entity... but since there's no valid entity name after it, you get that "Name entity expected" error.
The fix
Escape the & in your Google Fonts URL by replacing it with &. Here's your corrected code:
<link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
Full working example
Make sure these links go inside your <head> tag, and then reference the font in your CSS like this:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My Neocities Site</title> <!-- Corrected Google Fonts links --> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet"> <style> /* Use the font in your styles */ h1, p { font-family: 'Press Start 2P', monospace; } </style> </head> <body> <h1>Testing Press Start 2P Font!</h1> <p>This should now render with your desired font.</p> </body> </html>
Quick checks to ensure it works
- Double-check that all
&symbols in any external URLs (not just Google Fonts) are replaced with& - Confirm your
<link>tags are properly placed inside the<head>section (not in<body>or floating outside) - Clear your browser cache if you don't see the font load right away—sometimes old cached styles can interfere
内容的提问来源于stack exchange,提问作者Meganekogekkilove

