文本和字体属性
2025年3月4日大约 5 分钟
文本和字体属性
控制文本和字体显示的属性,包括 font、text、line 等相关属性。
属性列表
下面是文本和字体属性的完整列表,点击属性名可跳转到 MDN 文档查看详细信息。
字体属性
属性名 | 描述 | 语法 |
---|---|---|
font | 在一个声明中设置所有字体属性 | font: [ [ <'font-style'> || <'font-variant'> || <'font-weight'> || <'font-stretch'> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'> ] | caption | icon | menu | message-box | small-caption | status-bar |
font-family | 指定文本的字体族 | font-family: [ <family-name> | <generic-family> ]# |
font-size | 指定字体的大小 | font-size: <absolute-size> | <relative-size> | <length-percentage> |
font-style | 指定字体的样式 | font-style: normal | italic | oblique | oblique <angle> |
font-weight | 指定字体的粗细 | font-weight: normal | bold | bolder | lighter | <number> |
font-variant | 指定字体变体 | font-variant: normal | none | [ <common-lig-values> || <discretionary-lig-values> || <historical-lig-values> || <contextual-alt-values> || stylistic(<feature-value-name>) || historical-forms || styleset(<feature-value-name>#) || character-variant(<feature-value-name>#) || swash(<feature-value-name>) || ornaments(<feature-value-name>) || annotation(<feature-value-name>) || [ small-caps | all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps ] || <numeric-figure-values> || <numeric-spacing-values> || <numeric-fraction-values> || ordinal || slashed-zero || <east-asian-variant-values> || <east-asian-width-values> || ruby ] |
font-stretch | 指定字体的伸缩变形 | font-stretch: <font-stretch-absolute> |
font-size-adjust | 独立于字体的实际大小,调整其视觉大小 | font-size-adjust: none | <number> |
font-synthesis | 控制浏览器合成字体的粗体或斜体 | font-synthesis: none | [ weight || style ] |
font-kerning | 控制字体的字距调整 | font-kerning: auto | normal | none |
font-variant-caps | 控制大写字母的替代字形 | font-variant-caps: normal | small-caps | all-small-caps | petite-caps | all-petite-caps | unicase | titling-caps |
font-variant-numeric | 控制数字、分数和序号标记的替代字形 | font-variant-numeric: normal | [ <numeric-figure-values> || <numeric-spacing-values> || <numeric-fraction-values> || ordinal || slashed-zero ] |
font-variant-ligatures | 控制文本中使用的连字 | font-variant-ligatures: normal | none | [ <common-lig-values> || <discretionary-lig-values> || <historical-lig-values> || <contextual-alt-values> ] |
文本属性
属性名 | 描述 | 语法 |
---|---|---|
color | 设置文本内容的颜色 | color: <color> |
text-align | 指定文本的水平对齐方式 | text-align: start | end | left | right | center | justify | match-parent |
text-decoration | 设置文本的修饰线 | text-decoration: <'text-decoration-line'> || <'text-decoration-style'> || <'text-decoration-color'> || <'text-decoration-thickness'> |
text-decoration-line | 设置文本修饰线的位置 | text-decoration-line: none | [ underline || overline || line-through || blink ] |
text-decoration-color | 设置文本修饰线的颜色 | text-decoration-color: <color> |
text-decoration-style | 设置文本修饰线的样式 | text-decoration-style: solid | double | dotted | dashed | wavy |
text-decoration-thickness | 设置文本修饰线的粗细 | text-decoration-thickness: auto | from-font | <length> | <percentage> |
text-indent | 指定文本块中首行文本的缩进 | text-indent: <length-percentage> && hanging? && each-line? |
text-overflow | 指定当文本溢出包含元素时的显示方式 | text-overflow: [ clip | ellipsis | <string> ]{1,2} |
text-shadow | 为文本添加阴影 | text-shadow: none | <shadow-t># |
text-transform | 控制文本的大小写 | text-transform: none | capitalize | uppercase | lowercase | full-width | full-size-kana |
white-space | 设置如何处理元素内的空白 | white-space: normal | pre | nowrap | pre-wrap | pre-line | break-spaces |
word-break | 指定单词内断行的规则 | word-break: normal | break-all | keep-all | break-word |
word-spacing | 设置单词之间的间距 | word-spacing: normal | <length-percentage> |
word-wrap | 允许长单词断行到下一行 | word-wrap: normal | break-word |
overflow-wrap | 指定当内容超过行框时是否断行 | overflow-wrap: normal | break-word | anywhere |
letter-spacing | 设置文本字符之间的间距 | letter-spacing: normal | <length> |
line-height | 设置行高 | line-height: normal | <number> | <length> | <percentage> |
line-break | 指定如何处理中文、日文或韩文文本的换行 | line-break: auto | loose | normal | strict | anywhere |
vertical-align | 设置行内元素的垂直对齐方式 | vertical-align: baseline | sub | super | text-top | text-bottom | middle | top | bottom | <percentage> | <length> |
hyphens | 指定单词断字的方式 | hyphens: none | manual | auto |
text-justify | 指定文本对齐时使用的对齐方法 | text-justify: auto | inter-character | inter-word | none |
text-underline-position | 指定下划线的位置 | text-underline-position: auto | from-font | [ under || [ left | right ] ] |
text-rendering | 提供浏览器关于如何优化文本渲染的信息 | text-rendering: auto | optimizeSpeed | optimizeLegibility | geometricPrecision |
书写模式属性
属性名 | 描述 | 语法 |
---|---|---|
direction | 指定文本方向 | direction: ltr | rtl |
unicode-bidi | 与 direction 属性一起使用,处理文档中的双向文本 | unicode-bidi: normal | embed | isolate | bidi-override | isolate-override | plaintext |
writing-mode | 指定文本行是水平还是垂直布局 | writing-mode: horizontal-tb | vertical-rl | vertical-lr | sideways-rl | sideways-lr |
text-orientation | 指定垂直模式下文本的方向 | text-orientation: mixed | upright | sideways |
text-combine-upright | 将多个字符组合到单个字符空间中 | text-combine-upright: none | all | [ digits <integer>? ] |
使用示例
/* 基本字体设置 */
body {
font-family: 'Helvetica Neue', Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #333;
}
/* 标题样式 */
h1 {
font-size: 2.5em;
font-weight: 700;
text-transform: uppercase;
letter-spacing: -0.02em;
}
/* 段落文本样式 */
p {
text-align: justify;
text-indent: 2em;
word-spacing: 0.1em;
}
/* 强调文本 */
.highlight {
color: #e74c3c;
font-style: italic;
text-decoration: underline;
text-decoration-color: rgba(231, 76, 60, 0.5);
text-decoration-style: wavy;
}
/* 截断溢出文本 */
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* 垂直文本 */
.vertical-text {
writing-mode: vertical-rl;
text-orientation: mixed;
}
浏览器兼容性
大多数现代浏览器都支持这些文本和字体属性。较新的属性如 font-variant-*
系列、text-decoration-thickness
和 text-underline-position
在较新的浏览器中得到支持。具体兼容性信息请参考各属性的 MDN 文档。
相关资源
- MDN CSS 参考
- CSS 规范
- CSS 字体加载 API
- Web 字体使用指南
- CSS 文本样式指南
- Google Fonts - 免费开源字体库
- Font Pair - 字体搭配参考