color - 文本颜色设置文本的前景色。
p {
color: red;
color: #ff0000;
color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.8); /* 带透明度 */
color: hsl(0, 100%, 50%); /* 色相、饱和度、明度 */
}
text-align - 水平对齐设置文本的水平对齐方式。
p {
text-align: left; /* 左对齐(默认) */
text-align: right; /* 右对齐 */
text-align: center; /* 居中对齐 */
text-align: justify; /* 两端对齐 */
}
vertical-align - 垂直对齐设置行内元素或表格单元格的垂直对齐方式。
img {
vertical-align: middle; /* 图片与文本中线对齐 */
}
span {
vertical-align: super; /* 上标 */
vertical-align: sub; /* 下标 */
vertical-align: top; /* 顶部对齐 */
vertical-align: bottom; /* 底部对齐 */
}
text-decoration - 文本装饰简写属性,用于设置文本的装饰线。
a {
text-decoration: none; /* 去除链接下划线 */
}
h1 {
text-decoration: overline; /* 上划线 */
text-decoration: line-through; /* 删除线 */
text-decoration: underline; /* 下划线 */
text-decoration: underline wavy red; /* 波浪形红色下划线 */
}
/* 也可以拆分为更详细的属性 */
.fancy {
text-decoration-line: underline;
text-decoration-style: dashed;
text-decoration-color: blue;
text-decoration-thickness: 2px;
}
text-transform - 文本转换控制文本的大小写。
p {
text-transform: capitalize; /* 每个单词首字母大写 */
text-transform: uppercase; /* 全部大写 */
text-transform: lowercase; /* 全部小写 */
text-transform: none; /* 默认,原样显示 */
}
text-shadow - 文本阴影为文本添加阴影效果。
h1 {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
/* 格式:水平偏移 垂直偏移 模糊半径 颜色 */
}
/* 多重阴影效果 */
.fancy-text {
text-shadow: 1px 1px 0 #fff, 2px 2px 0 #999;
}
font-family - 字体家族设置文本的字体系列。
body {
font-family: "Microsoft YaHei", Arial, sans-serif;
/* 浏览器会从左到右依次查找可用的字体 */
}
更过详情内容,请看下文 font-family 字体文件如何获取和配置
font-size - 字体大小p {
font-size: 16px; /* 像素 */
font-size: 1em; /* 相对于父元素 */
font-size: 1rem; /* 相对于根元素(html) */
font-size: 100%; /* 百分比 */
font-size: larger; /* 相对大小 */
}
font-weight - 字体粗细p {
font-weight: normal; /* 正常 */
font-weight: bold; /* 粗体 */
font-weight: 700; /* 数字值:100-900 */
font-weight: lighter; /* 更细 */
}
font-style - 字体样式p {
font-style: normal; /* 正常 */
font-style: italic; /* 斜体 */
font-style: oblique; /* 倾斜体 */
}
line-height - 行高设置行间的距离(行高)。
p {
line-height: 1.6; /* 无单位数字,相对于当前字体大小 */
line-height: 24px; /* 固定值 */
line-height: 150%; /* 百分比 */
}
letter-spacing - 字符间距h1 {
letter-spacing: 2px; /* 增加间距 */
letter-spacing: -1px; /* 减少间距 */
}
word-spacing - 单词间距p {
word-spacing: 5px; /* 增加单词间距 */
}
text-indent - 首行缩进p {
text-indent: 2em; /* 首行缩进2个字符 */
text-indent: 20px; /* 固定值缩进 */
}
white-space - 空白处理控制元素中空白的处理方式。
pre {
white-space: pre; /* 保留所有空白和换行 */
}
.nowrap {
white-space: nowrap; /* 文本不换行 */
}
.normal {
white-space: normal; /* 默认,合并空白,自动换行 */
}
word-wrap / overflow-wrap - 单词换行允许长单词或 URL 地址换行到下一行。
p {
word-wrap: break-word; /* 旧属性 */
overflow-wrap: break-word; /* 新标准 */
}
word-break - 断行规则指定怎样在单词内断行。
p {
word-break: normal; /* 默认断行规则 */
word-break: break-all; /* 允许在任意字符间断行 */
word-break: keep-all; /* 主要在空格或连字符处换行 */
}
text-overflow - 文本溢出确定如何向用户发出未显示的溢出内容信号。
.truncate {
white-space: nowrap; /* 必须的 */
overflow: hidden; /* 必须的 */
text-overflow: ellipsis; /* 显示省略号 */
}
/* 多行省略号(-webkit) */
.truncate1 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /* 显示 2 行 */
overflow: hidden;
}
语法:
font: [font-style] [font-variant] [font-weight] [font-size]/[line-height] [font-family];
示例:
p {
/* 完整写法 */
font-style: italic;
font-weight: bold;
font-size: 16px;
line-height: 1.5;
font-family: Arial, sans-serif;
/* 等效的简写写法 */
font: italic bold 16px/1.5 Arial, sans-serif;
}
body {
/* 更简洁的写法 */
font: 14px/1.6 "Microsoft YaHei", sans-serif;
}
.article {
font-family: "Georgia", serif;
font-size: 18px;
line-height: 1.7;
color: #333;
text-align: justify;
text-indent: 2em;
word-spacing: 1px;
margin-bottom: 1.5em;
}
.heading {
font-family: "Helvetica Neue", Arial, sans-serif;
font-weight: 300;
letter-spacing: 2px;
text-transform: uppercase;
color: #2c3e50;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
a {
color: #3498db;
text-decoration: none;
transition: all 0.3s ease;
}
a:hover {
color: #2980b9;
text-decoration: underline;
}
.card-title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin: 0;
}
font-family 字体文件如何获取和配置| 字体 | 特点 |
|---|---|
| 思源黑体 Noto Sans CJK / Source Han Sans | 免费最全、最标准 |
| 思源宋体 | 中文非常专业 |
| 阿里巴巴普惠体 | 免费商用、风格现代 |
| HarmonyOS Sans | 华为字体,免费商用 |
浏览器对字体格式支持不同,因此建议准备多种格式:
| 格式 | 说明 | 推荐程度 |
|---|---|---|
| woff2 | 最小、加载快、现代浏览器 | ⭐⭐⭐⭐⭐ |
| woff | 兼容好(旧浏览器) | ⭐⭐⭐⭐ |
| ttf / otf | 常用原始格式,体积大 | ⭐⭐⭐ |
| eot | 旧版 IE 用 | ⭐(基本不用) |
最理想组合:woff2 + woff + ttf
假设你下载了字体文件放在:
/assets/fonts/
MyFont.woff2
MyFont.woff
MyFont.ttf
写 @font-face:
@font-face {
font-family: "MyFont";
src: url("/assets/fonts/MyFont.woff2") format("woff2"), url("/assets/fonts/MyFont.woff") format("woff"),
url("/assets/fonts/MyFont.ttf") format("truetype");
font-weight: normal;
font-style: normal;
font-display: swap;
}
说明:
font-family: 'MyFont' 是你自己取的名字font-display: swap; 能避免字体加载中闪烁(FOIT)然后直接使用字体:
body {
font-family: "MyFont", sans-serif;
}
把字体文件放在项目根目录或 assets 目录,CSS 写上面那段 @font-face 即可。
# 字体文件放在:
src/assets/fonts/MyFont.woff2
/* @font-face 写法 */
@font-face {
font-family: "MyFont";
src: url("@/assets/fonts/MyFont.woff2") format("woff2");
}
/* 使用 */
body {
font-family: "MyFont";
}
放在 public/fonts 或 src/assets/fonts:
/* src/index.css: */
@font-face {
font-family: "MyFont";
src: url("./assets/fonts/MyFont.woff2") format("woff2");
}
/* App.css */
body {
font-family: "MyFont";
}
React Native 不支持 woff/woff2,只能使用 ttf!
# 字体文件放在:
/assets/fonts/MyFont.ttf
// react-native.config.js
module.exports = {
assets: ["./assets/fonts/"],
};
Expo 项目(你正在用的环境)更简单:
// app.json
{
"expo": {
"assetBundlePatterns": ["**/*"]
}
}
组件中加载:
import * as Font from "expo-font";
await Font.loadAsync({
MyFont: require("./assets/fonts/MyFont.ttf"),
});
使用:
<Text style={{ fontFamily: "MyFont" }}>Hello</Text>
<link
href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@400;700&display=swap"
rel="stylesheet"
/>
使用:
body {
font-family: "Noto Sans", sans-serif;
}