⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

CSS 文本样式

学习 text-align/line-height/letter-spacing · 难度:入门 · +10XP

CSS 文本样式详解

CSS 提供了丰富的文本样式属性,让你能够控制网页中文字的颜色、对齐方式、装饰效果、间距等。掌握文本样式是网页设计的基础,好的文字排版能显著提升用户体验。

文本颜色 color

color 属性用于设置文本的颜色。颜色值可以是颜色名称(如 red)、十六进制值(如 #ff0000)、RGB 值(如 rgb(255,0,0))或 HSL 值。

p {
  color: #333333;
}
h1 {
  color: #1a73e8;
}
.highlight {
  color: red;
}

文本对齐 text-align

text-align 控制文本在元素内的水平对齐方式。可选值有:left(左对齐)、right(右对齐)、center(居中)和 justify(两端对齐)。

h1 {
  text-align: center;
}
p {
  text-align: justify;
}
.right {
  text-align: right;
}

文本装饰 text-decoration

text-decoration 是一个简写属性,可同时设置 text-decoration-linetext-decoration-colortext-decoration-styletext-decoration-thickness

a {
  text-decoration: none;  /* 去掉下划线 */
}
del {
  text-decoration: line-through;
}
.underline {
  text-decoration: underline wavy #ff6600;
}

文本转换 text-transform

text-transform 用于控制字母的大小写转换。这在处理用户输入时特别有用。

p.uppercase {
  text-transform: uppercase;
}
p.lowercase {
  text-transform: lowercase;
}
p.capitalize {
  text-transform: capitalize;  /* 每个单词首字母大写 */
}

行高 line-height

line-height 设置行与行之间的高度。合理的行高能显著提升阅读体验,建议设置为字体大小的 1.5 到 2 倍。

body {
  line-height: 1.6;
}
p.article {
  font-size: 16px;
  line-height: 1.8;  /* 约 28.8px */
}

文本间距 letter-spacing 与 word-spacing

h2 {
  letter-spacing: 2px;    /* 字间距 */
}
p {
  word-spacing: 5px;      /* 词间距 */
}

常用文本属性速查表

属性作用常用值示例
color文本颜色颜色值color: #333;
text-align水平对齐left/center/right/justifytext-align: center;
text-decoration文本装饰none/underline/line-throughtext-decoration: none;
text-transform大小写转换uppercase/lowercase/capitalizetext-transform: uppercase;
line-height行高数字或长度值line-height: 1.6;
letter-spacing字间距px/emletter-spacing: 2px;
word-spacing词间距px/emword-spacing: 4px;
white-space空白处理normal/nowrap/pre/pre-wrapwhite-space: nowrap;
text-indent首行缩进px/em/%text-indent: 2em;
word-break单词断行normal/break-all/keep-allword-break: break-all;

???? 练习

  1. 创建一个段落,设置颜色为深灰色,行高为 1.8,首行缩进 2em
  2. 为所有链接去掉下划线,并设置鼠标悬停时显示下划线
  3. 创建一个标题,使用 letter-spacing: 3px 使其更有设计感
  4. 使用 text-align: justify 让长段落两端对齐,观察效果差异
  5. 尝试用 text-transform 将一段英文全部转为大写并添加 letter-spacing
Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 122 篇
0 完成
🔥 0