← CSS text-orientation 属性 CSS keyframes 关键帧动画 →

CSS 文本样式

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

CSS 文本样式属性指南

概述

CSS文本属性是网页设计中至关重要的组成部分,它们允许开发者控制文本内容的呈现方式,从而提升用户体验和页面美观度。通过合理运用这些属性,可以将普通文本转换为视觉吸引力强、专业度高的内容展示。

核心文本属性详解

1. 文本颜色 (color)

功能:设置HTML元素的文本颜色

语法

selector {
    color: value;
}

颜色值格式

  • 关键字:预定义颜色名称(如red、blue)

  • RGB:红绿蓝三色光组合(rgb(255, 0, 0))

  • RGBA:RGB加透明度(rgba(255, 0, 0, 0.5))

  • HSL:色相、饱和度、亮度(hsl(0, 100%, 50%))

  • HSLA:HSL加透明度(hsla(0, 100%, 50%, 0.5))

  • 十六进制:十六进制颜色代码(#FF0000)

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本颜色示例</title>
    <style>
        .color-demo {
            padding: 20px;
            background: #f5f5f5;
            border-radius: 8px;
        }
        .keyword { color: coral; }
        .rgb { color: rgb(60, 179, 113); }
        .rgba { color: rgba(60, 179, 113, 0.7); }
        .hsl { color: hsl(210, 80%, 50%); }
        .hsla { color: hsla(210, 80%, 50%, 0.6); }
        .hex { color: #6a5acd; }
    </style>
</head>
<body>
    <div class="color-demo">
        <h3>代码号编程学习:文本颜色示例</h3>
        <p class="keyword">关键字颜色 - coral</p>
        <p class="rgb">RGB颜色 - rgb(60, 179, 113)</p>
        <p class="rgba">RGBA颜色 - rgba(60, 179, 113, 0.7)</p>
        <p class="hsl">HSL颜色 - hsl(210, 80%, 50%)</p>
        <p class="hsla">HSLA颜色 - hsla(210, 80%, 50%, 0.6)</p>
        <p class="hex">十六进制颜色 - #6a5acd</p>
    </div>
</body>
</html>

2. 文本背景色 (background-color)

功能:设置文本背景颜色

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本背景色示例</title>
    <style>
        .bg-demo {
            padding: 20px;
            line-height: 2;
        }
        .highlight1 { background-color: #ffeb3b; padding: 5px; }
        .highlight2 { background-color: rgba(33, 150, 243, 0.3); padding: 5px; }
        .highlight3 { 
            background: linear-gradient(90deg, #ff9a9e 0%, #fad0c4 100%);
            padding: 5px;
            color: white;
        }
    </style>
</head>
<body>
    <div class="bg-demo">
        <h3>代码号学习重点标记</h3>
        <p>在<span class="highlight1">编程学习</span>过程中,重点概念需要特别标记</p>
        <p><span class="highlight2">CSS属性</span>的掌握是前端开发的基础</p>
        <p>通过<span class="highlight3">代码号平台</span>可以系统学习各种编程技术</p>
    </div>
</body>
</html>

3. 字体家族 (font-family)

功能:指定文本字体

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 字体家族示例</title>
    <style>
        .font-demo {
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .serif { font-family: "Times New Roman", Times, serif; }
        .sans-serif { font-family: Arial, Helvetica, sans-serif; }
        .monospace { font-family: "Courier New", Courier, monospace; }
        .custom-stack { 
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
    </style>
</head>
<body>
    <div class="font-demo">
        <h3>代码号字体选择指南</h3>
        <p class="serif">衬线字体 - 适合正式文档和长篇文章阅读</p>
        <p class="sans-serif">无衬线字体 - 现代感强,适合网页内容</p>
        <p class="monospace">等宽字体 - 代码显示的选择</p>
        <p class="custom-stack">系统字体栈 - 优化性能和显示一致性</p>
    </div>
</body>
</html>

4. 字体大小 (font-size)

功能:设置文本尺寸

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 字体大小示例</title>
    <style>
        .size-demo {
            padding: 20px;
            background: #f8f9fa;
        }
        .px-size { font-size: 16px; }
        .em-size { font-size: 1.2em; }
        .rem-size { font-size: 1.1rem; }
        .viewport-size { font-size: 2vw; }
        .keyword-size { font-size: large; }
    </style>
</head>
<body>
    <div class="size-demo">
        <h3>响应式字体大小设置</h3>
        <p class="px-size">固定像素大小 (16px) - 传统单位</p>
        <p class="em-size">相对EM单位 (1.2em) - 相对于父元素</p>
        <p class="rem-size">相对REM单位 (1.1rem) - 相对于根元素</p>
        <p class="viewport-size">视口单位 (2vw) - 响应式设计</p>
        <p class="keyword-size">关键字大小 (large) - 预定义尺寸</p>
    </div>
</body>
</html>

5. 字体样式 (font-style)

功能:设置文本样式(斜体等)

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 字体样式示例</title>
    <style>
        .style-demo {
            padding: 20px;
            line-height: 2;
        }
        .italic { font-style: italic; }
        .oblique { font-style: oblique; }
        .normal { font-style: normal; }
        .emphasis {
            font-style: italic;
            color: #d35400;
        }
    </style>
</head>
<body>
    <div class="style-demo">
        <h3>文本强调样式示例</h3>
        <p>这是<span class="italic">斜体文本</span>,用于强调内容</p>
        <p>这是<span class="oblique">倾斜文本</span>,视觉效果类似斜体</p>
        <p>这是<span class="normal">正常文本</span>,取消继承的样式</p>
        <p class="emphasis">在编程学习中,重点概念需要特别强调</p>
    </div>
</body>
</html>

6. 字体粗细 (font-weight)

功能:设置文本粗细程度

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 字体粗细示例</title>
    <style>
        .weight-demo {
            padding: 20px;
            background: #ecf0f1;
        }
        .light { font-weight: 300; }
        .normal { font-weight: 400; }
        .medium { font-weight: 500; }
        .bold { font-weight: 700; }
        .black { font-weight: 900; }
        .numeric { font-weight: 600; }
    </style>
</head>
<body>
    <div class="weight-demo">
        <h3>字体粗细层次对比</h3>
        <p class="light">细体 (300) - 轻盈优雅</p>
        <p class="normal">正常 (400) - 标准重量</p>
        <p class="medium">中等 (500) - 适度强调</p>
        <p class="bold">粗体 (700) - 强烈强调</p>
        <p class="black">特粗 (900) - 较大权重</p>
        <p class="numeric">数值设置 (600) - 精确控制</p>
    </div>
</body>
</html>

7. 文本装饰 (text-decoration)

功能:添加文本装饰线

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本装饰示例</title>
    <style>
        .decoration-demo {
            padding: 20px;
            line-height: 2.5;
        }
        .underline { text-decoration: underline; }
        .overline { text-decoration: overline; }
        .line-through { text-decoration: line-through; }
        .custom-underline {
            text-decoration: underline wavy #e74c3c;
        }
        .custom-overline {
            text-decoration: overline double #3498db;
        }
        .no-decoration { text-decoration: none; }
    </style>
</head>
<body>
    <div class="decoration-demo">
        <h3>文本装饰效果展示</h3>
        <p class="underline">下划线 - 用于链接或强调</p>
        <p class="overline">上划线 - 特殊装饰效果</p>
        <p class="line-through">删除线 - 表示删除或过时内容</p>
        <p class="custom-underline">波浪下划线 - 自定义样式</p>
        <p class="custom-overline">双上划线 - 高级装饰效果</p>
        <p><a href="#" class="no-decoration">无装饰链接</a> - 去除默认下划线</p>
    </div>
</body>
</html>

8. 文本阴影 (text-shadow)

功能:为文本添加阴影效果

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本阴影示例</title>
    <style>
        .shadow-demo {
            padding: 30px;
            background: #2c3e50;
            color: white;
            text-align: center;
        }
        .simple-shadow {
            text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
            font-size: 2em;
        }
        .glow-effect {
            text-shadow: 0 0 10px #f39c12, 0 0 20px #f39c12;
            font-size: 2em;
        }
        .multiple-shadow {
            text-shadow: 1px 1px 0 #e74c3c, 
                        2px 2px 0 #3498db,
                        3px 3px 0 #2ecc71;
            font-size: 2em;
        }
        .emboss-effect {
            text-shadow: 1px 1px 1px rgba(255,255,255,0.1),
                        -1px -1px 1px rgba(0,0,0,0.5);
            font-size: 2em;
        }
    </style>
</head>
<body>
    <div class="shadow-demo">
        <h3>文本阴影效果展示</h3>
        <p class="simple-shadow">简单阴影效果</p>
        <p class="glow-effect">发光文字效果</p>
        <p class="multiple-shadow">多重阴影叠加</p>
        <p class="emboss-effect">浮雕立体效果</p>
    </div>
</body>
</html>

9. 文本对齐 (text-align)

功能:设置文本水平对齐方式

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本对齐示例</title>
    <style>
        .alignment-demo {
            padding: 20px;
        }
        .left-align { text-align: left; }
        .center-align { text-align: center; }
        .right-align { text-align: right; }
        .justify-align { 
            text-align: justify;
            text-justify: inter-word;
        }
        .alignment-box {
            width: 300px;
            border: 1px solid #bdc3c7;
            padding: 15px;
            margin: 10px 0;
            background: #ecf0f1;
        }
    </style>
</head>
<body>
    <div class="alignment-demo">
        <h3>文本对齐方式对比</h3>
        
        <div class="alignment-box left-align">
            <h4>左对齐</h4>
            <p>这是左对齐文本示例。在代码号学习编程时,文本对齐是重要的排版技巧。</p>
        </div>
        
        <div class="alignment-box center-align">
            <h4>居中对齐</h4>
            <p>这是居中对齐文本。适合标题和强调内容,增强视觉平衡感。</p>
        </div>
        
        <div class="alignment-box right-align">
            <h4>右对齐</h4>
            <p>这是右对齐文本示例。常用于特殊布局和设计需求。</p>
        </div>
        
        <div class="alignment-box justify-align">
            <h4>两端对齐</h4>
            <p>这是两端对齐文本。使段落左右边缘整齐,提高阅读体验,常用于多行文本排版。</p>
        </div>
    </div>
</body>
</html>

10. 字母间距 (letter-spacing)

功能:调整字符间距离

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 字母间距示例</title>
    <style>
        .spacing-demo {
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        .tight-spacing { letter-spacing: -1px; }
        .normal-spacing { letter-spacing: normal; }
        .wide-spacing { letter-spacing: 2px; }
        .very-wide-spacing { letter-spacing: 5px; }
        .custom-spacing { letter-spacing: 0.1em; }
        
        .spacing-box {
            margin: 15px 0;
            padding: 15px;
            background: #f8f9fa;
            border-left: 4px solid #3498db;
        }
    </style>
</head>
<body>
    <div class="spacing-demo">
        <h3>字母间距调整效果</h3>
        
        <div class="spacing-box tight-spacing">
            <h4>紧凑间距 (-1px)</h4>
            <p>字母间距紧凑,适合空间有限的场景</p>
        </div>
        
        <div class="spacing-box normal-spacing">
            <h4>正常间距</h4>
            <p>默认字母间距,标准阅读体验</p>
        </div>
        
        <div class="spacing-box wide-spacing">
            <h4>宽松间距 (2px)</h4>
            <p>增加字母间距,提升可读性和现代感</p>
        </div>
        
        <div class="spacing-box very-wide-spacing">
            <h4>超宽间距 (5px)</h4>
            <p>艺术化效果,用于标题和装饰文本</p>
        </div>
        
        <div class="spacing-box custom-spacing">
            <h4>相对单位间距 (0.1em)</h4>
            <p>使用相对单位,响应式设计更灵活</p>
        </div>
    </div>
</body>
</html>

11. 单词间距 (word-spacing)

功能:调整单词间距离

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 单词间距示例</title>
    <style>
        .word-spacing-demo {
            padding: 20px;
            max-width: 600px;
        }
        .tight-words { word-spacing: -2px; }
        .normal-words { word-spacing: normal; }
        .wide-words { word-spacing: 8px; }
        .very-wide-words { word-spacing: 15px; }
        
        .spacing-example {
            background: #e8f4f8;
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
            border-left: 4px solid #2980b9;
        }
    </style>
</head>
<body>
    <div class="word-spacing-demo">
        <h3>单词间距调整示例</h3>
        
        <div class="spacing-example tight-words">
            <h4>紧凑单词间距</h4>
            <p>This text demonstrates tight word spacing for compact layout design in web development.</p>
        </div>
        
        <div class="spacing-example normal-words">
            <h4>正常单词间距</h4>
            <p>This text shows normal word spacing which provides optimal readability for most content.</p>
        </div>
        
        <div class="spacing-example wide-words">
            <h4>宽松单词间距</h4>
            <p>This example displays wide word spacing that creates a more open and airy text appearance.</p>
        </div>
        
        <div class="spacing-example very-wide-words">
            <h4>超宽单词间距</h4>
            <p>This text uses very wide word spacing for artistic and decorative typography effects.</p>
        </div>
    </div>
</body>
</html>

12. 行高 (line-height)

功能:设置文本行间距离

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 行高设置示例</title>
    <style>
        .line-height-demo {
            padding: 20px;
            max-width: 700px;
        }
        .tight-lines { line-height: 1; }
        .normal-lines { line-height: 1.5; }
        .wide-lines { line-height: 2; }
        .very-wide-lines { line-height: 2.5; }
        
        .line-example {
            background: #f0f7ff;
            padding: 15px;
            margin: 15px 0;
            border: 1px solid #cce5ff;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="line-height-demo">
        <h3>行高对阅读体验的影响</h3>
        
        <div class="line-example tight-lines">
            <h4>紧凑行高 (1.0)</h4>
            <p>紧凑行高适合空间有限的场景,但可能影响阅读舒适度。在代码号学习编程时,合理的行高设置可以显著改善代码可读性。紧凑排版适合显示大量数据,但需要谨慎使用。</p>
        </div>
        
        <div class="line-example normal-lines">
            <h4>标准行高 (1.5)</h4>
            <p>标准行高提供良好的阅读体验,是网页正文的常用设置。适当的行距让眼睛更容易跟踪文本行,减少阅读疲劳。这种设置在大多数内容展示场景中都是安全的选择。</p>
        </div>
        
        <div class="line-example wide-lines">
            <h4>宽松行高 (2.0)</h4>
            <p>宽松行高创造开放、现代的视觉效果,特别适合标题和短文本内容。增加行距可以增强页面的呼吸感,让内容看起来更加优雅和专业。这种设置常用于高端设计和品牌展示。</p>
        </div>
        
        <div class="line-example very-wide-lines">
            <h4>超宽行高 (2.5)</h4>
            <p>超宽行高主要用于艺术化排版和特殊设计效果。这种极端的行距设置不适合长文本内容,但可以用于创建视觉冲击力强的标题或引文。在使用时需要确保不影响内容的可读性。</p>
        </div>
    </div>
</body>
</html>

13. 文本变换 (text-transform)

功能:控制文本大小写转换

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本变换示例</title>
    <style>
        .transform-demo {
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        .uppercase { text-transform: uppercase; }
        .lowercase { text-transform: lowercase; }
        .capitalize { text-transform: capitalize; }
        .none-transform { text-transform: none; }
        
        .transform-box {
            padding: 15px;
            margin: 10px 0;
            background: #f8f9fa;
            border-left: 4px solid #27ae60;
        }
    </style>
</head>
<body>
    <div class="transform-demo">
        <h3>文本大小写变换效果</h3>
        
        <div class="transform-box uppercase">
            <h4>全部大写</h4>
            <p>This text is transformed to uppercase letters. Useful for headings and emphasis.</p>
        </div>
        
        <div class="transform-box lowercase">
            <h4>全部小写</h4>
            <p>THIS TEXT IS TRANSFORMED TO LOWERCASE LETTERS. Good for creating consistent formatting.</p>
        </div>
        
        <div class="transform-box capitalize">
            <h4>首字母大写</h4>
            <p>this text has the first letter of each word capitalized. Ideal for titles and proper nouns.</p>
        </div>
        
        <div class="transform-box none-transform">
            <h4>无变换</h4>
            <p>This text maintains its original case without any transformation applied.</p>
        </div>
    </div>
</body>
</html>

14. 文本缩进 (text-indent)

功能:设置首行文本缩进

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本缩进示例</title>
    <style>
        .indent-demo {
            padding: 20px;
            max-width: 600px;
        }
        .no-indent { text-indent: 0; }
        .small-indent { text-indent: 20px; }
        .large-indent { text-indent: 50px; }
        .negative-indent { text-indent: -20px; }
        .percentage-indent { text-indent: 10%; }
        
        .indent-example {
            background: #fff3e0;
            padding: 15px;
            margin: 15px 0;
            border-radius: 5px;
            border: 1px solid #ffb74d;
        }
    </style>
</head>
<body>
    <div class="indent-demo">
        <h3>文本缩进样式示例</h3>
        
        <div class="indent-example no-indent">
            <h4>无缩进</h4>
            <p>这是没有缩进的文本段落。在网页设计中,有时需要保持段落开头对齐,特别是在简洁的现代设计中。</p>
        </div>
        
        <div class="indent-example small-indent">
            <h4>小缩进 (20px)</h4>
            <p>这是有20像素缩进的文本段落。适度的缩进可以帮助区分段落开始,改善长篇内容的可读性。</p>
        </div>
        
        <div class="indent-example large-indent">
            <h4>大缩进 (50px)</h4>
            <p>这是有50像素缩进的文本段落。较大的缩进创建明显的视觉分隔,适合正式文档和印刷风格设计。</p>
        </div>
        
        <div class="indent-example negative-indent">
            <h4>负缩进 (-20px)</h4>
            <p>这是有负20像素缩进的文本段落。负缩进可以创建悬挂缩进效果,常用于参考文献和特殊列表格式。</p>
        </div>
        
        <div class="indent-example percentage-indent">
            <h4>百分比缩进 (10%)</h4>
            <p>这是使用百分比缩进的文本段落。百分比缩进相对于父元素的宽度,在响应式设计中更加灵活。</p>
        </div>
    </div>
</body>
</html>

15. 文本溢出 (text-overflow)

功能:处理溢出文本的显示方式

示例

<!DOCTYPE html>
<html>
<head>
    <title>代码号 - 文本溢出处理示例</title>
    <style>
        .overflow-demo {
            padding: 20px;
            font-family: Arial, sans-serif;
        }
        .clip-overflow {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: clip;
            width: 200px;
            border: 1px solid #e74c3c;
            padding: 10px;
            margin: 10px 0;
        }
        .ellipsis-overflow {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            width: 200px;
            border: 1px solid #3498db;
            padding: 10px;
            margin: 10px 0;
        }
        .custom-overflow {
            white-space: nowrap;
            overflow: hidden;
            width: 200px;
            border: 1px solid #2ecc71;
            padding: 10px;
            margin: 10px 0;
        }
        .multi-line-ellipsis {
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
            width: 300px;
            border: 1px solid #f39c12;
            padding: 10px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="overflow-demo">
        <h3>文本溢出处理技术</h3>
        
        <div class="clip-overflow">
            这是使用clip处理的溢出文本,超出部分直接被裁剪
        </div>
        
        <div class="ellipsis-overflow">
            这是使用ellipsis处理的溢出文本,超出部分显示为省略号
        </div>
        
        <div class="multi-line-ellipsis">
            这是多行文本溢出处理示例。通过CSS限制显示行数,超出部分显示省略号。
            这种方法在移动端和内容摘要中非常有用,可以保持布局整洁。
            代码号平台提供了丰富的编程学习资源。
        </div>
        
        <p><strong>提示:</strong>文本溢出处理需要结合white-space: nowrap和overflow: hidden使用</p>
    </div>
</body>
</html>

本节课程知识要点

  1. 颜色系统掌握:理解各种颜色表示方法(关键字、RGB、HSL、十六进制)及其适用场景

  2. 字体选择原则:根据内容类型选择合适的字体家族,确保可读性和美观性

  3. 尺寸单位应用:区分px、em、rem、vw等单位的特性和使用场景

  4. 间距控制技巧:合理运用letter-spacing和word-spacing改善排版效果

  5. 行高设置标准:掌握1.5-2.0的行高范围,优化阅读体验

  6. 响应式文本设计:使用相对单位和媒体查询确保文本在不同设备上的显示效果

  7. 溢出处理策略:根据内容重要性选择合适的文本溢出处理方式

  8. 性能优化考虑:权衡字体文件大小和视觉效果,确保页面加载速度

实践建议

  1. 建立类型比例:使用一致的字体大小比例创建视觉层次

  2. 限制字体数量:单个页面使用2-3种字体,保持设计一致性

  3. 考虑可访问性:确保文本颜色对比度符合WCAG标准

  4. 测试多语言支持:检查所选字体对多语言字符的支持情况

  5. 使用字体加载策略:优化字体加载性能,避免布局偏移

  6. 实现响应式排版:使用CSS变量和媒体查询创建自适应文本系统

通过掌握这些CSS文本属性,开发者可以创建出专业、美观且用户友好的网页界面,提升用户体验和内容可读性。

更多CSS教程和编程学习资源,请访问代码号编程学习

← CSS text-orientation 属性 CSS keyframes 关键帧动画 →
分享笔记 (共有 篇笔记)
验证码:
微信公众号