| 風格 | 縮排單位 | 適用場景 | 範例 |
|---|---|---|---|
| **2 空格** | 2 spaces | HTML/CSS/JS 主流、Prettier 預設 | 最推薦,節省寬度 |
| **4 空格** | 4 spaces | Python、Java 風格團隊 | 視覺層級更明顯 |
| **Tab** | `\t` | 個人偏好、需自訂寬度 | 需全專案一致,配合 EditorConfig |
<table>
<caption>2024 季度營收</caption>
<thead>
<tr>
<th scope="col">季度</th>
<th scope="col">營收 (萬)</th>
<th scope="col">成長率</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Q1</th>
<td>1,200</td>
<td>+5.2%</td>
</tr>
<tr>
<th scope="row">Q2</th>
<td>1,350</td>
<td>+12.5%</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">合計</th>
<td>2,550</td>
<td>+8.9%</td>
</tr>
</tfoot>
</table>
// .prettierrc
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
}
安裝後全專案統一風格,CI/CD 可加入 `prettier --check .` 當守門員。
# .editorconfig
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
<td>簡短內容</td> <td>另一欄</td>
<td
class="cell cell--highlight"
data-id="12345"
style="width: 120px;"
>
內容
</td>
Vue/React/Blade 等模板語法內的 HTML 同樣遵循專案縮排規範,Prettier 支援大多數框架。
| 錯誤 | 問題 | 修正 |
|---|---|---|
| 混用 Tab/空格 | 不同編輯器顯示錯位 | 統一用空格,設定 EditorConfig |
| 巢狀層級未縮排 | 難辨識父子關係 | 每層 +1 級縮排 |
| 結束標籤獨立行但未對齊 | 視覺跳躍 | 結束標籤與開始標籤同層級 |
| 表格超過 80 欄仍不換行 | 需水平捲動 | 屬性換行、內容換行、或拆元件 |