CSS font-weight:如何设置字重并检查实际使用的字体
CSS 的 font-weight 用于请求文字的字重。normal 对应 400,bold 对应 700。同样指定 650 时,只提供 400 和 700 的静态字体家族可能使用 700,字重轴覆盖该值的可变字体则可以显示中间字重。
字重取值及其含义
初始值为 normal。没有其他声明时,元素继承父元素的计算字重。浏览器也可能为部分 HTML 元素设置默认字重。需要明确请求某个字重时可以使用数字。
| 值 | 行为 |
|---|---|
normal / 400 |
请求常规字重 400。 |
bold / 700 |
请求粗体字重 700。 |
1–1000 |
接受 1 到 1000 的数字,也可用 650.5 这样的分数值。字体可能只提供其中一部分字重。 |
bolder / lighter |
按预定义档位,根据继承的字重计算相对值。 |
取值范围、继承和相对关键字的规则见W3C 的 font-weight 规范。
比较静态字体和可变字体的 650
将完整代码保存为 UTF-8 编码的 font-weight-example.html,用浏览器打开。两行使用相同的拉丁字母文本。静态示例使用本机已安装的 Georgia 和 Georgia Bold,可变示例使用 Bahnschrift。两行都请求 650。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS 字重对比</title>
<style>
@font-face {
font-family: "Demo Static";
src: local("Georgia");
font-weight: 400;
}
@font-face {
font-family: "Demo Static";
src: local("Georgia Bold");
font-weight: 700;
}
@font-face {
font-family: "Demo Variable";
src: local("Bahnschrift");
font-weight: 300 700;
}
body { margin: 0; color: #20242b; background: #fff; font: 1rem/1.5 system-ui, sans-serif; }
main { max-width: 44rem; margin: auto; padding: 1rem; overflow-wrap: anywhere; }
h1 { font-size: 1.4rem; }
.samples { font-weight: 400; }
.weight { margin: .2rem 0 1rem; font-size: 30px; font-weight: 650; }
.static { font-family: "Demo Static", serif; }
.variable { font-family: "Demo Variable", sans-serif; }
</style>
</head>
<body>
<main>
<h1>CSS 字重对比</h1>
<section class="samples">
<h2>静态字体 · Georgia</h2>
<p class="weight static">Font weight in CSS</p>
<h2>可变字体 · Bahnschrift</h2>
<p class="weight variable">Font weight in CSS</p>
</section>
</main>
</body>
</html>
第一行只为 Georgia 注册 400 和 700。计算样式仍然显示 650,实际用的是 Georgia Bold。将公用的 .weight 规则中的 font-weight 依次改为 400、500、650 和 700。在本例中,400、500 使用常规字体,650、700 使用粗体。
Bahnschrift 的字重轴 wght 支持 300 到 700。第二行用同一个文件也可以显示不同的 650 和 700。逐个比较后恢复为 650。
local() 引用设备上安装的字体。如果字体缺失,或者浏览器限制访问本地字体,回退字体可能改变结果。请先查看实际渲染的字体。
声明网页字体支持的字重
提供可变网页字体时,在 @font-face 的 font-weight 描述符中声明文件支持的范围。下面的规则适用于支持 300 到 700 的网页字体文件。
@font-face {
font-family: "Site Variable";
src: url("./site-variable.woff2") format("woff2");
font-weight: 300 700;
}
.title {
font-family: "Site Variable", sans-serif;
font-weight: 650;
}
把示例中的路径换成相对于样式表的实际文件路径,并把 .title 应用到标题。使用静态文件时,应分别用相同的家族名注册各个文件,并设置 400 或 700 等对应的具体字重。
如果可变字体的粗细没有变化,请核对声明范围与文件实际范围。如果 font-variation-settings 固定了 "wght",可先停用该声明,再只改 font-weight。
bolder 和 lighter 如何计算
bolder 和 lighter 按预定义档位计算。例如父元素是 400 时,bolder 为 700,父元素是 600 时则为 900。下表列出几个常见父级值的计算结果。
| 父元素的计算字重 | bolder |
lighter |
|---|---|---|
| 100 | 400 | 100 |
| 400 | 700 | 100 |
| 600 | 900 | 400 |
| 700 | 900 | 400 |
| 900 | 900 | 700 |
保持示例中 .samples 的 400,在开发者工具中给第一行设置 font-weight: bolder。计算值是 700。试用 lighter 会得到 100。如果缺少 100 对应的字体文件,浏览器可能用其他字重渲染。完成后恢复 650。
为什么不同数值看起来一样
- 在开发者工具里选中目标文字。“计算值”中的
font-weight是请求的字重,“已渲染的字体”则显示实际绘制字符的字体。面板名称因浏览器而异。 - 如果 500 和 650 显示不符合预期,检查可用字重、
@font-face描述符和网络响应。混合不同文字系统时,单个字符可能使用别的字体。 - 如果计算值本身有差异,请检查 CSS 层叠。后应用的
font: 1rem serif等简写未指定字重,可能将其重设为normal。
没有真实粗体时,浏览器可以合成加粗效果。使用 font-synthesis-weight: none 可禁止合成,现有的真实粗体仍可被选中。相关规则见W3C 的 font-synthesis-weight 规范。
表示内容特别重要时使用HTML 的 strong 元素。外观的粗细由 CSS 控制,重要性由 HTML 表达。
同时设置字体家族、字号和字重时可参考CSS 字体设置教程。回退字体的顺序由 font-family 指定。


