CSS 規則塊內可包含多個聲明,**分號 `;` 為分隔符** (非結束符):**
selector {
property-1: value-1;
property-2: value-2;
property-3: value-3;
/* 最後一條聲明可省略分號,但強烈建議保留 */
}
| 場景 | 無尾隨分號風險 | 有尾隨分號 |
|---|---|---|
| 新增屬性 | 忘記在前一行加分號 → 語法錯誤、後續失效 | 直接新增行,無風險 |
| 刪除屬性 | 刪除最後一行 → 新最後一行缺分號 | 無影響 |
| 移動行順序 | 移動後可能缺分號 | 無影響 |
| Prettier/格式化 | 可能產生非預期合併 | 穩定輸出 |
| 壓縮工具 | 需智能處理 | 標準輸入 |
.card {
display: flex;
flex-direction: column;
gap: 16px;
padding: 24px;
border: 1px solid #e5e7eb;
border-radius: 12px;
background: white;
box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
}
/* 相關屬性分組 + 空行分隔 */
.card {
/* Layout */
display: flex;
flex-direction: column;
gap: 16px;
/* Box */
padding: 24px;
border: 1px solid #e5e7eb;
border-radius: 12px;
/* Visual */
background: white;
box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
}
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.d-flex { display: flex; }
.flex-1 { flex: 1; }
.text-center { text-align: center; }
.mt-4 { margin-top: 1rem; }
/* 規則:
1. 元件/頁面層級選擇器:多行展開,相關屬性分組
2. 工具類/原子類:單行
3. 變數定義:單行分組
4. @media/@supports 內部:遵循外層風格
*/
.example {
/* 1. 濾波器/混合模式 (影響合成) */
isolation: isolate;
mix-blend-mode: multiply;
filter: drop-shadow(0 4px 6px rgb(0 0 0 / 0.1));
/* 2. 定位/顯示/佈局 */
position: relative;
z-index: 10;
display: flex;
flex-direction: column;
gap: 16px;
/* 3. 盒模型 */
width: 100%;
max-width: 400px;
padding: 24px;
margin: 0 auto;
border: 1px solid #e5e7eb;
border-radius: 12px;
/* 4. 背景/邊框/陰影 (視覺) */
background: white;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
/* 5. 文字/字體 */
font-family: var(--font-family-base);
font-size: 1rem;
line-height: 1.6;
color: #111827;
/* 6. 其他 */
transition: box-shadow 0.2s ease;
cursor: pointer;
}
/* .stylelintrc.js */
{
"plugins": ["stylelint-order"],
"rules": {
"order/order": [
"custom-properties",
"dollar-variables",
"at-rules",
"declarations"
],
"order/properties-order": [
/* Layout */
"position", "z-index", "top", "right", "bottom", "left",
"display", "flex", "flex-flow", "flex-direction", "flex-wrap",
"justify-content", "align-items", "align-content", "gap",
"grid", "grid-template", "grid-area",
"float", "clear",
/* Box Model */
"width", "min-width", "max-width",
"height", "min-height", "max-height",
"margin", "margin-top", "margin-right", "margin-bottom", "margin-left",
"padding", "padding-top", "padding-right", "padding-bottom", "padding-left",
"box-sizing",
/* Visual */
"border", "border-radius", "background", "box-shadow",
"opacity", "transform", "filter", "backdrop-filter",
/* Typography */
"font", "font-family", "font-size", "font-weight", "line-height",
"color", "text-align", "text-decoration", "white-space",
/* Other */
"transition", "animation", "cursor", "pointer-events"
],
"order/alphabetical": false
}
}
/* ========================================
Section: Card Component
======================================== */
.card {
/* Layout: Flex column with gap */
display: flex;
flex-direction: column;
gap: var(--space-4);
/* Box: Padding, border, radius */
padding: var(--space-6);
border: 1px solid var(--color-border);
border-radius: var(--radius-lg);
/* Visual: Background, shadow */
background: var(--color-bg);
box-shadow: var(--shadow-md);
/* State: Hover elevation */
transition: box-shadow var(--transition-base), transform var(--transition-fast);
}
.card:hover {
box-shadow: var(--shadow-lg);
transform: translateY(-2px);
}
/* Modifier: Featured card */
.card--featured {
border-color: var(--color-primary);
background: linear-gradient(135deg, var(--color-primary-50), var(--color-bg));
}