← CSS float 浮动 CSS font-size 字体大小 →

CSS font 字体

原创 2025-09-03 CSS 已有人查阅

CSS字体属性详解:全面掌握文本样式控制

概述

CSS字体属性是控制文本外观的核心技术,通过系统运用这些属性,开发者可以精确调整文字的尺寸、色彩、字型、粗细等视觉特征。字体属性不仅影响美观性,更直接关系到内容的可读性和用户体验。

字体色彩属性

色彩定义方式

CSS提供多种色彩定义方案,满足不同设计需求:

/* 关键字定义法 */
.primary-text { color: black; }
.secondary-text { color: gray; }
.accent-text { color: blue; }

/* 十六进制值定义 */
.hex-color { 
    color: #1a73e8; /* 谷歌蓝 */
}

/* RGB函数定义 */
.rgb-color {
    color: rgb(26, 115, 232);
}

/* RGBA透明度支持 */
.rgba-color {
    color: rgba(26, 115, 232, 0.7);
}

实际应用示例

<!DOCTYPE html>
<html>
<head>
<style>
body {
    font-family: "Microsoft YaHei", sans-serif;
    line-height: 1.6;
}
.title { color: #2c3e50; }
.subtitle { color: #7f8c8d; }
.highlight { color: #e74c3c; }
.info { color: #3498db; }
.success { color: #27ae60; }
</style>
</head>
<body>
<h1 class="title">主标题文字</h1>
<h2 class="subtitle">副标题文字</h2>
<p class="highlight">重点强调内容</p>
<p class="info">信息提示内容</p>
<p class="success">成功状态提示</p>
</body>
</html>

字体族属性

字体分类体系

/* 无衬线字体族 */
.sans-serif {
    font-family: "Helvetica Neue", Arial, sans-serif;
}

/* 衬线字体族 */
.serif {
    font-family: Georgia, "Times New Roman", serif;
}

/* 等宽字体族 */
.monospace {
    font-family: "Courier New", Monaco, monospace;
}

/* 手写字体族 */
.cursive {
    font-family: "Comic Sans MS", cursive;
}

/* 幻想字体族 */
.fantasy {
    font-family: Impact, fantasy;
}

字体栈实践示例

<div class="content-wrapper">
    <article class="blog-post">
        <h2>技术文章标题</h2>
        <p class="post-content">这里是文章正文内容,采用适合长时间阅读的衬线字体,提升阅读体验和舒适度。</p>
        <code class="code-block">console.log("代码示例使用等宽字体");</code>
    </article>
</div>
<style>
.blog-post {
    font-family: "Noto Serif SC", serif;
}

.post-content {
    font-size: 16px;
    line-height: 1.8;
    color: #333;
}

.code-block {
    font-family: "Source Code Pro", Monaco, monospace;
    background: #f4f4f4;
    padding: 10px;
    border-radius: 4px;
}
</style>

字体尺寸属性

尺寸单位详解

/* 绝对单位 */
.px-unit { font-size: 16px; }
.pt-unit { font-size: 12pt; }

/* 相对单位 */
.rem-unit { font-size: 1rem; }
.em-unit { font-size: 1.2em; }
.percent-unit { font-size: 100%; }

/* 视口单位 */
.vw-unit { font-size: 2vw; }
.vh-unit { font-size: 5vh; }

响应式字体系统

:root {
    --base-font-size: 16px;
    --scale-ratio: 1.2;
}

html {
    font-size: var(--base-font-size);
}

h1 {
    font-size: calc(2rem * var(--scale-ratio));
}

h2 {
    font-size: calc(1.5rem * var(--scale-ratio));
}

body {
    font-size: 1rem;
}

.small-text {
    font-size: 0.875rem;
}

/* 移动端适配 */
@media (max-width: 768px) {
    :root {
        --base-font-size: 14px;
        --scale-ratio: 1.1;
    }
}

字体样式属性

样式变体控制

/* 正常样式 */
.normal-style {
    font-style: normal;
}

/* 斜体样式 */
.italic-style {
    font-style: italic;
}

/* 倾斜样式 */
.oblique-style {
    font-style: oblique;
}

/* 自定义倾斜角度 */
.custom-oblique {
    font-style: oblique 15deg;
}

实际应用场景

<div class="literary-work">
    <h2>文学作品选段</h2>
    <p class="normal-text">正常文本样式用于一般叙述。</p>
    <p class="italic-text"><em>斜体文本常用于强调或外语词汇。</em></p>
    <blockquote class="oblique-text">
        倾斜样式可以用于引文或特殊说明内容,创造独特的视觉效果。
    </blockquote>
</div>

字体变体属性

小型大写字母效果

/* 正常变体 */
.normal-variant {
    font-variant: normal;
}

/* 小型大写字母 */
.small-caps-variant {
    font-variant: small-caps;
}

/* 全大写字母 */
.all-small-caps {
    font-variant: all-small-caps;
}

/* 数字变体 */
.lining-nums {
    font-variant-numeric: lining-nums;
}

.oldstyle-nums {
    font-variant-numeric: oldstyle-nums;
}

专业排版应用

<div class="document">
    <h3>学术论文摘要</h3>
    <p class="author">作者:<span class="small-caps">Zhang San</span></p>
    <p class="abstract">本文探讨了CSS字体排版在学术出版中的应用。研究发现,适当的字体变体使用可以提升阅读效率<span class="lining-nums">25%</span>。</p>
</div>

字体粗细属性

权重分级系统

/* 关键字值 */
.light-weight { font-weight: 300; }
.normal-weight { font-weight: 400; }
.medium-weight { font-weight: 500; }
.semibold-weight { font-weight: 600; }
.bold-weight { font-weight: 700; }
.heavy-weight { font-weight: 900; }

/* 数值精度控制 */
.precise-100 { font-weight: 100; }
.precise-200 { font-weight: 200; }
.precise-300 { font-weight: 300; }
.precise-400 { font-weight: 400; }
.precise-500 { font-weight: 500; }

层次化权重应用

<article class="news-article">
    <header>
        <h1 class="article-title">重大新闻标题</h1>
        <p class="article-meta">发布时间:<time>2024-01-20</time> | 来源:<span class="source">新闻中心</span></p>
    </header>
    <div class="article-content">
        <p class="lead">导语内容使用中等粗细字体,引导读者进入正文。</p>
        <p>正文内容使用正常粗细,保证长时间阅读的舒适性。</p>
        <blockquote class="important-quote">
            重要引文使用较粗的字体权重,突出显示关键信息。
        </blockquote>
    </div>
</article>
<style>
.article-title {
    font-weight: 700;
    color: #2c3e50;
}

.article-meta {
    font-weight: 300;
    color: #7f8c8d;
}

.lead {
    font-weight: 500;
    font-size: 1.1em;
}

.important-quote {
    font-weight: 600;
    border-left: 4px solid #3498db;
    padding-left: 15px;
}
</style>

复合字体属性

简写语法优化

/* 完整属性定义 */
.comprehensive {
    font-style: italic;
    font-variant: small-caps;
    font-weight: 600;
    font-size: 1.2rem;
    line-height: 1.6;
    font-family: "Helvetica Neue", sans-serif;
}

/* 简写属性等效 */
.shorthand {
    font: italic small-caps 600 1.2rem/1.6 "Helvetica Neue", sans-serif;
}

/* 小化简写 */
.minimal-shorthand {
    font: 1rem/1.5 sans-serif;
}

本节课程知识要点

  1. 色彩系统:掌握多种色彩定义方法及其适用场景

  2. 字体栈策略:理解字体回退机制和跨平台字体选择原则

  3. 尺寸体系:熟悉不同尺寸单位的特性和响应式适配方案

  4. 样式控制:了解各种字体样式的语义化应用场景

  5. 变体特性:掌握专业排版中的字体变体使用技巧

  6. 权重层次:建立字体粗细的视觉层次管理系统

  7. 性能优化:认识字体加载性能对用户体验的影响

综合实践示例

完整字体系统设计

:root {
    --font-primary: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
    --font-secondary: "Georgia", serif;
    --font-mono: "Fira Code", monospace;
    
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.25rem;
}

.typography-system {
    font-family: var(--font-primary);
    line-height: 1.6;
}

.heading-1 {
    font-size: 2.5rem;
    font-weight: 700;
    line-height: 1.2;
}

.heading-2 {
    font-size: 2rem;
    font-weight: 600;
    line-height: 1.3;
}

.body-text {
    font-size: var(--text-base);
    font-weight: 400;
    line-height: 1.6;
}

.caption {
    font-size: var(--text-sm);
    font-weight: 300;
    color: #666;
}

.code-sample {
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    background: #f8f9fa;
    padding: 1rem;
    border-radius: 6px;
}

通过系统掌握CSS字体属性,开发者能够创建出既美观又实用的文本排版系统,显著提升内容的可读性和视觉吸引力。

← CSS float 浮动 CSS font-size 字体大小 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号