# Emmet 快速参考

*HTML 缩写、嵌套、属性、CSS 快捷方式*

> Source: Emmet Documentation (emmet.io) · MIT

## HTML 缩写

### 元素快捷方式

| Command | Description |
|---------|-------------|
| `div` | 生成 <div></div> |
| `p` | 生成 <p></p> |
| `h1` | 生成 <h1></h1> |
| `a` | 生成 <a href=""></a> |
| `img` | 生成 <img src="" alt=""> |
| `input` | 生成 <input type="text"> |
| `btn` | 生成 <button></button> |
| `link:css` | CSS 样式表 link 标签 |

### HTML 模板

```
!    → full HTML5 boilerplate
doc  → HTML5 doctype only
```

### 隐式标签

| Command | Description |
|---------|-------------|
| `.class` | 解析为 <div class="class"> |
| `ul>.item` | 子元素解析为 <li class="item"> |
| `table>.row` | 子元素解析为 <tr class="row"> |
| `select>.opt` | 子元素解析为 <option class="opt"> |

## 嵌套

### 嵌套运算符

| Command | Description |
|---------|-------------|
| `>` | 子元素 |
| `+` | 同级元素 |
| `^` | 向上一级 |
| `^^` | 向上两级 |

### 嵌套示例

```
div>ul>li       → nested child elements
div+p+footer    → sibling elements
div>p>span^h2   → climb up: h2 is sibling of p
div>p>span^^h2  → climb up twice: h2 sibling of div
```

## 分组

### 括号分组

```
div>(header>nav)+main+footer
→ groups header>nav as a unit
(div>h2+p)*3
→ repeat grouped structure 3 times
```

### 复杂布局

```
.container>(header>h1+nav>ul>li*3)+main+footer
→ full page layout skeleton
```

## 乘法

### 重复运算符

| Command | Description |
|---------|-------------|
| `li*5` | 生成 5 个 <li> 元素 |
| `div>p*3` | div 包含 3 个子段落 |
| `ul>li*4>a` | 4 个列表项，每项含链接 |
| `tr>td*4` | 含 4 个单元格的表格行 |

### 乘法示例

```
ul>li.item*5           → 5 list items with class
table>tr*3>td*4        → 3x4 table grid
.row>.col*3>p{Content} → 3-column row with text
```

## 编号

### 项目编号（$）

| Command | Description |
|---------|-------------|
| `$` | 顺序编号（1, 2, 3...） |
| `$$` | 两位零填充（01, 02...） |
| `$$$` | 三位零填充（001, 002...） |
| `$@3` | 从 3 开始编号 |
| `$@-` | 倒序编号（递减） |
| `$@-3` | 从 3 开始倒序 |

### 编号示例

```
li.item$*3    → item1, item2, item3
li.item$$*3   → item01, item02, item03
li.item$@3*3  → item3, item4, item5
```

## 属性

### ID 与 Class

| Command | Description |
|---------|-------------|
| `#id` | 添加 id 属性：<div id="id"> |
| `.class` | 添加 class：<div class="class"> |
| `.c1.c2` | 多个 class：class="c1 c2" |
| `#main.container` | ID + class 组合 |

### 自定义属性

```
td[colspan=2]        → <td colspan="2">
a[href=#]{Click}     → <a href="#">Click</a>
input[type=email placeholder=Email]
div[data-id=$]*3     → data-id="1", "2", "3"
```

## 文本

### 文本内容（{}）

| Command | Description |
|---------|-------------|
| `p{Hello}` | <p>Hello</p> |
| `a{Click me}` | <a href="">Click me</a> |
| `p{Item $}*3` | Item 1, Item 2, Item 3 |

### 文本与嵌套

```
p>{Click }+a{here}+{ to continue}
→ <p>Click <a href="">here</a> to continue</p>
ul>li{Item $}*4
→ list with numbered items
```

## CSS 缩写

### 盒模型

| Command | Description |
|---------|-------------|
| `m10` | margin: 10px; |
| `m10-20` | margin: 10px 20px; |
| `p10` | padding: 10px; |
| `w100` | width: 100px; |
| `w100p` | width: 100%; |
| `h50` | height: 50px; |

### 显示与定位

| Command | Description |
|---------|-------------|
| `d:f` | display: flex; |
| `d:g` | display: grid; |
| `d:n` | display: none; |
| `d:b` | display: block; |
| `pos:r` | position: relative; |
| `pos:a` | position: absolute; |

### Flexbox

| Command | Description |
|---------|-------------|
| `jc:c` | justify-content: center; |
| `jc:sb` | justify-content: space-between; |
| `ai:c` | align-items: center; |
| `fxd:c` | flex-direction: column; |
| `fw:w` | flex-wrap: wrap; |

### 字体与颜色

| Command | Description |
|---------|-------------|
| `fz14` | font-size: 14px; |
| `fw:b` | font-weight: bold; |
| `ta:c` | text-align: center; |
| `c:#333` | color: #333; |
| `bgc:#fff` | background-color: #fff; |
