作为一名前端开发者,我深知CSS字体和文本属性在网页设计中的重要性。这些看似基础的属性,实际上决定了网页的视觉呈现和用户体验。记得我刚入行时,就因为对line-height理解不透彻,导致整个页面的文字排版一团糟。今天,我将分享这些年来积累的实战经验,带你深入理解这些核心属性。
font-family是文字呈现的第一道关卡。在实际项目中,我总结出几个关键原则:
css复制body {
font-family: -apple-system, BlinkMacSystemFont,
"Segoe UI", Roboto, "Helvetica Neue",
Arial, sans-serif;
}
css复制.chinese-text {
font-family: "PingFang SC", "Hiragino Sans GB",
"Microsoft YaHei", "WenQuanYi Micro Hei",
sans-serif;
}
专业提示:在Mac和iOS上,"PingFang SC"是系统默认字体,而在Windows上"Microsoft YaHei"效果最佳。Web字体虽然美观,但会显著影响页面加载速度,在性能敏感的场景要慎用。
经过多个项目实践,我形成了这样的单位使用策略:
css复制html {
font-size: 62.5%; /* 10px */
}
body {
font-size: 1.6rem; /* 16px */
}
css复制h1 {
font-size: clamp(2rem, 5vw, 3rem);
}
很多开发者只用到normal和bold,其实数字权重更有价值:
css复制@font-face {
font-family: "MyFont";
src: url("myfont-light.woff2") format("woff2");
font-weight: 300;
}
除了常见的italic,还有一些实用技巧:
css复制.button {
font-style: normal;
transition: font-style 0.3s;
}
.button:hover {
font-style: italic;
}
css复制.icon {
font-style: normal;
display: inline-flex;
align-items: center;
}
在大型项目中,我总结出这些最佳实践:
css复制/* 必须包含size和family */
selector {
font: [style] [weight] size/line-height family;
}
css复制.reset-font {
font: initial; /* 重置为浏览器默认 */
}
css复制.system-ui {
font: caption; /* 使用操作系统字体 */
}
其他可用值:icon, menu, message-box等。
除了基础的颜色值,现代CSS提供了更多可能:
css复制.modern-color {
color: oklch(70% 0.3 150);
color: color(display-p3 1 0.5 0);
}
css复制:root {
--primary-text: oklch(30% 0.1 270);
}
@media (prefers-color-scheme: dark) {
:root {
--primary-text: oklch(90% 0.1 270);
}
}
css复制.transparent-text {
color: rgb(0 0 0 / 50%);
}
css复制[dir="rtl"] {
text-align: start; /* 自动适应文本方向 */
}
css复制td {
text-align: end; /* 数值右对齐 */
}
css复制.justify-advanced {
text-align-last: justify;
hyphens: auto;
}
CSS3为text-decoration带来了强大扩展:
css复制.fancy-underline {
text-decoration: underline overline wavy blue;
}
css复制.custom-line {
text-decoration-line: underline;
text-decoration-color: #f06;
text-decoration-style: double;
text-decoration-thickness: 3px;
}
css复制.animated-underline {
text-decoration: underline 0.15s ease;
text-decoration-color: transparent;
transition: text-decoration-color 0.3s;
}
.animated-underline:hover {
text-decoration-color: currentColor;
}
css复制.hanging-indent {
text-indent: -2em;
padding-left: 2em;
}
css复制.drop-cap::first-letter {
text-indent: 0;
float: left;
font-size: 3em;
line-height: 1;
margin-right: 0.1em;
}
css复制ul.custom-indent {
text-indent: -1em;
padding-left: 1em;
}
css复制/* 推荐 */
body {
line-height: 1.5;
}
/* 不推荐 */
body {
line-height: 24px;
}
css复制.baseline-grid {
line-height: 1.5;
background-image: linear-gradient(#eee 1px, transparent 1px);
background-size: 100% 24px;
}
css复制.btn {
height: 48px;
line-height: 48px; /* 单行文本垂直居中 */
}
css复制@font-face {
font-family: 'CustomFont';
src: url('font.woff2') format('woff2');
font-display: swap;
}
css复制.font-loading {
font-size-adjust: 0.5;
}
javascript复制document.fonts.ready.then(() => {
console.log('所有字体加载完成');
});
css复制.optimal-reading {
max-width: 65ch; /* 约60-75字符为佳 */
}
css复制.paragraph {
margin-bottom: 1.5em;
text-align: justify;
hyphens: auto;
}
css复制ul.clean-list {
padding-left: 1em;
li {
text-indent: -1em;
&::before {
content: "• ";
}
}
}
css复制.responsive-text {
font-size: calc(16px + 0.5vw);
}
css复制.hyphenate {
overflow-wrap: break-word;
word-break: break-word;
hyphens: auto;
}
css复制.multicolumn {
column-width: 30ch;
column-gap: 2em;
}
bash复制# 使用pyftsubset生成子集
pyftsubset font.ttf --text="需要包含的字符"
css复制@font-face {
font-family: 'OptimizedFont';
src: url('font.woff2') format('woff2'),
url('font.woff') format('woff');
}
html复制<link rel="preload" href="font.woff2" as="font" crossorigin>
css复制.animate-text {
will-change: font-size;
transition: font-size 0.3s;
}
css复制.perf-text {
transform: translateZ(0);
backface-visibility: hidden;
}
css复制.gpu-text {
transform: translate3d(0, 0, 0);
}
css复制.safe-fonts {
font-family: system-ui, -apple-system,
BlinkMacSystemFont, sans-serif;
}
css复制@supports (font-variation-settings: normal) {
.variable-font {
font-family: 'Inter var', sans-serif;
}
}
css复制.full-underline {
-webkit-text-decoration: underline;
text-decoration: underline;
-webkit-text-decoration-color: red;
text-decoration-color: red;
}
css复制.crisp-text {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
css复制@font-face {
font-family: 'VariableFont';
src: url('font.woff2') format('woff2-variations');
font-weight: 100 900;
}
.dynamic-weight {
font-family: 'VariableFont';
font-weight: 350;
}
css复制@keyframes weight-change {
from { font-weight: 100; }
to { font-weight: 900; }
}
.animate-weight {
animation: weight-change 2s infinite alternate;
}
css复制.text-shadow {
text-shadow: 1px 1px 3px rgba(0,0,0,0.3);
}
css复制.gradient-text {
background: linear-gradient(45deg, #f06, #f90);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
css复制.text-3d {
text-shadow:
1px 1px 0 #999,
2px 2px 0 #888,
3px 3px 0 #777;
}
css复制.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
css复制.multiline-truncate {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
css复制.text-stroke {
-webkit-text-stroke: 1px black;
text-stroke: 1px black;
}
在实际项目中,我发现合理组合这些属性可以创造出丰富的视觉效果。比如,通过调整letter-spacing可以显著提升大写字母的可读性,而word-spacing则适合调整中文段落的气口。记住,好的排版应该让人感觉不到排版的存在,它只是让内容以最舒适的方式呈现给读者。