Typography 由 font-* 和 text-* 属性族控制。正确设置它们是使页面看起来精致且可读的大部分工作。
css
{
: , system-ui, sans-serif;
: ;
: ;
: italic;
: ;
: ;
}
font-family: "Inter", system-ui, -apple-system, sans-serif;
浏览器依次尝试每个字体,直到找到可用的为止。始终以通用字体族 (sans-serif、serif、monospace) 结尾,以便在自定义字体失败时仍能呈现文本。system-ui 使用操作系统的原生字体(快速,无需下载)。
.text {
text-align: center; /* left | right | center | justify */
text-decoration: underline;/* underline | line-through | none */
text-transform: uppercase; /* UPPERCASE | lowercase | Capitalize */
white-space: nowrap; /* prevent wrapping */
word-break: break-word; /* break long words/URLs */
}
line-height: 1.5; /* ✅ 1.5 × the element's font-size — scales correctly */
line-height: 24px; /* ⚠️ fixed — doesn't adapt if font-size changes */
无单位 line-height 将每个元素的 font-size 相乘,所以当嵌套元素有不同大小时它保持成比例——这是推荐的方法。
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* shows "..." when text is cut off */
}
文本属性驱动可读性和视觉层次——用 font-size/weight 表示强调,用 line-height(~1.5)实现舒适阅读,用回退堆栈确保可靠性,用 text-overflow 实现清晰截断。
好的排版是优质 UI 的大部分。