大网数据广告活动
网站运营与故障问答

How to make sure Chinese text is rendered in webpage?

· 52CE 我爱测

I'm making a webpage that's partly in Chinese. I use the UTF-8 encoding for all text. The page contains some parts that are in Chinese, mixed with western letters. The font I use for western text doesn't have Chinese characters i…

问题原文

原帖链接 · PetaspeedBeaver · CC BY-SA 3.0

I'm making a webpage that's partly in Chinese. I use the UTF-8 encoding for all text. The page contains some parts that are in Chinese, mixed with western letters. The font I use for western text doesn't have Chinese characters in it, so I want the Chinese font available on the users computer to be used for rendering the Chinese characters (btw Chinese fonts are super big and not suitable for downloading on the fly).

Can I just put the Chinese characters in the middle of the other text, and expect the browser to find a Chinese font to render them in (if I don't care which font it's rendered in), or should I specify the Chinese font in some way?

How should I specify the font for the Chinese text to make sure the users browser displays 你好 and not □□?


On my browser (Firefox 56.0) the example below renders the Chinese text correct (of course given that there's a Chinese font installed on the computer) in both <span>s. All Chinese text is rendered in the same font, so it looks like the browser in some way can find the font that has the glyphs that are being used in the text, even though I didn't specify the name of any font with Chinese glyphs ... Have no idea how that works :|

<!doctype html>

<html lang="en-GB">

  <head>

    <meta charset="utf-8">

    <style>
      span.serif {
        font-family: "serif";
      }

      span.courier {
        font-family: "courier";
      }
    </style>

  </head>

  <body>
    <span class='serif'>123, wazzup? 一二三,你好嗎?</span><br />
    <span class='courier'>123, wazzup? 一二三,你好嗎?</span>
  </body>

</html>

The above is rendered like this on my computer:

已采纳解答

原帖链接 · unor · CC BY-SA 3.0

If the specified font doesn’t contain a glyph for a character, browsers typically use a fallback font to render this character.

(Browsers don’t have do this, of course, and how it exactly works might also depend on the operating system. But it would be really suprising if there were browsers that don’t use fallback fonts. Not to mention that there are many user-agents that don’t even support CSS, e.g., text browser, feed readers, etc.; for them you couldn’t even specify a font to begin with.)

If no suitable font is found, a "replacement glyph" (aka. "tofu"; often, but not necessarily, something like "□") will be displayed for this character.

The algorithms defined by CSS:

So unless you know of a user-agent that doesn’t handle it, I wouldn’t worry about it. But, ideally, always use the lang attribute (in this context, it could help browsers "to select language-appropriate fonts" as fallbacks).

If you care about visual harmony, you might be interested in font families like Noto or Ubuntu. They try to cover all languages/scripts, rendering them in a visually compatible way (e.g., same height etc.).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。