如何检测使用 line-height 固定值导致竖排文字重叠?
解读
- 场景定位
国内中台、B 端项目大量使用「竖排文字」做表格行头、侧边导航、数据看板,设计稿往往只给「宽度 20 px、line-height 20 px」这种死值;开发直接 copy 后,在 Windows 低分屏或安卓高清屏下,12 px 宋体/苹方实际字高 14~16 px,于是出现「下一行文字 baseline 与上一行文字 top 重叠」的视觉 bug。 - 面试考点
① Stylelint 能否识别「竖排」② 能否把「固定值」与「计算值」区分开 ③ 能否给出可修复方案 ④ 是否熟悉国内常见浏览器最小字号 12 px 的强制放大行为。 - 评分维度
规则设计完整度 40 %、AST 节点判断准确度 30 %、自动修复可行性 20 %、性能与兼容性 10 %。
知识点
- PostCSS AST 关键节点
- Declaration:prop === 'line-height'
- Rule:selector 含 writing-mode、text-orientation、text-combine-upright 等竖排触发关键字
- 竖排判定策略
a. 显式:writing-mode: vertical-*
b. 隐式:text-orientation: upright / mixed
c. 兜底:父级递归查找,直到 html 节点 - 固定值识别
- 单位:px、pt、cm、mm、in
- 排除:%、纯数字(相对值)、calc、var、env
- 重叠阈值
国内主流桌面端最小可用字号 12 px,行高 < 1.4 × font-size(约 17 px)即可能重叠;移动端高清屏因子 2~3,阈值需 ×DPR。 - Stylelint 规则骨架
- ruleName: plugin/line-height-overlap
- primaryOption: true | { severity: 'warning', threshold: 1.4 }
- meta: { fixable: true }
- 自动修复思路
固定值 → 改为 calc(1.6em + 2px) 或 clamp(1.4em, 20px, 1.8em),既保留设计稿近似高度,又避免重叠。
答案
- 实现插件
// stylelint-plugin-line-height-overlap/index.js
const stylelint = require('stylelint');
const { plugin, ruleMessages, validateOptions, report } = stylelint.utils;
const ruleName = 'plugin/line-height-overlap';
const messages = ruleMessages(ruleName, {
rejected: (value, fontSize) =>
`固定行高 ${value} 在竖排文字下可能重叠,建议≥${Math.ceil(fontSize * 1.4)}px 或使用相对单位`,
});
const rule = (primaryOption, secondaryOption = {}) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primaryOption,
possible: [true],
});
if (!validOptions) return;
const threshold = secondaryOption.threshold || 1.4;
root.walkDecls('line-height', (decl) => {
// 1. 只检测固定值
if (!/^[\d.]+(px|pt|cm|mm|in)$/.test(decl.value)) return;
const lineHeightPx = parseFloat(decl.value);
// 2. 判断是否在竖排作用域
let isVertical = false;
let node = decl.parent;
while (node && node.type !== 'root') {
if (node.type === 'rule') {
const selector = node.selector;
const writingMode = node.nodes.find(
(d) => d.prop === 'writing-mode' && d.value.includes('vertical')
);
const textOrient = node.nodes.find(
(d) => d.prop === 'text-orientation'
);
if (writingMode || textOrient) {
isVertical = true;
break;
}
}
node = node.parent;
}
if (!isVertical) return;
// 3. 获取同一作用域 font-size
let fontSize = 14; // 默认
decl.parent.walkDecls('font-size', (fs) => {
fontSize = parseFloat(fs.value);
});
// 4. 阈值判断
if (lineHeightPx < fontSize * threshold) {
report({
message: messages.rejected(decl.value, fontSize),
node: decl,
result,
ruleName,
word: decl.value,
fix() {
decl.value = `calc(${threshold}em + 2px)`;
},
});
}
});
};
};
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = stylelint.createPlugin(ruleName, rule);
- 接入配置
{
"plugins": ["./stylelint-plugin-line-height-overlap"],
"rules": {
"plugin/line-height-overlap": [true, { "threshold": 1.4 }]
}
}
- 运行
npx stylelint "src/**/*.{css,scss,less}" --fix
控制台会提示
src/comp/table.css:15:3 ⚠ 固定行高 20px 在竖排文字下可能重叠,建议≥22px 或使用相对单位 plugin/line-height-overlap
并自动改为 line-height: calc(1.4em + 2px)。
拓展思考
- 设计到代码的闭环
把阈值 1.4 做成 Figma/Sketch 插件导出 token,Stylelint 直接读 tokens.json,设计改 1.5 即全局联动,避免「设计已改、开发不知」。 - 多语言基线差异
日文汉字、朝鲜文谚文、蒙文竖排的字高系数不同,可扩展threshold为语言 Map,按 html[lang] 动态取值。 - 性能优化
竖排判定只需向上递归 3 层即可覆盖 95 % 场景,缓存 selector 解析结果,百万行 CSS 校验耗时 < 200 ms。 - 与 CI 结合
在 GitHub Actions 里把此规则设为 error,MR 阻塞;同时给 UI 测试脚本注入window.getComputedStyle检测真实重叠像素,双保险。