.selector {
/* 1. 佈局與定位:決定元素在文檔流中的位置與佔據空間 */
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
z-index: 10;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
float: left; /* 避免使用,僅列示順序 */
clear: both;
/* 2. 盒模型:尺寸、邊距、邊框、背景 */
width: 100%;
max-width: 400px;
height: auto;
aspect-ratio: 16/9;
margin: 0 auto;
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 8px;
background: var(--color-bg);
box-shadow: var(--shadow-md);
overflow: hidden;
/* 3. 字體與文本:內容渲染樣式 */
font-family: var(--font-sans);
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: var(--color-text);
text-align: left;
text-decoration: none;
white-space: nowrap;
text-overflow: ellipsis;
/* 4. 視覺效果:變換、動畫、過渡、濾鏡 */
transform: translateZ(0);
transition: all 0.2s ease;
animation: fadeIn 0.3s;
opacity: 1;
filter: none;
backdrop-filter: blur(8px);
clip-path: none;
/* 5. 交互與偽類:狀態變化 */
cursor: pointer;
user-select: none;
pointer-events: auto;
/* 6. 列表、表格、其他 */
list-style: none;
table-layout: fixed;
border-collapse: collapse;
/* 7. SVG 專用 */
fill: currentColor;
stroke: currentColor;
stroke-width: 1;
}
// .stylelintrc.js
module.exports = {
extends: ['stylelint-config-standard', 'stylelint-config-recess-order'],
rules: {
'order/properties-alphabetical-order': false, // 使用邏輯分組而非字母序
'order/order': [
'custom-properties', // CSS 變量優先
'dollar-variables', // Sass 變量
'at-rules', // @media, @supports 等
'declarations', // 普通屬性,按以下分組序
{
type: 'at-rule',
name: 'media',
hasBlock: true
}
],
// 自定義分組順序(需 stylelint-order 6.0+)
'order/properties-order': [
// 1. 佈局定位
'position', 'top', 'right', 'bottom', 'left', 'inset',
'z-index', 'display', 'flex*', 'grid*', 'float', 'clear',
// 2. 盒模型
'width', 'min-width', 'max-width', 'height', 'min-height', 'max-height',
'aspect-ratio', 'margin', 'margin-*', 'padding', 'padding-*',
'border', 'border-*', 'border-radius', 'box-shadow',
'background', 'background-*', 'overflow', 'overflow-*',
// 3. 字體文本
'font', 'font-*', 'line-height', 'color', 'text-*', 'letter-spacing',
'word-*', 'white-space', 'text-overflow', 'direction', 'unicode-bidi',
// 4. 視覺效果
'transform', 'transform-*', 'transition', 'transition-*',
'animation', 'animation-*', 'opacity', 'filter', 'backdrop-filter',
'clip', 'clip-path', 'mask', 'mask-*', 'shape-*',
// 5. 交互
'cursor', 'pointer-events', 'user-select', 'resize', 'scroll-*',
// 6. 其他
'list-style', 'counter-*', 'table-layout', 'border-collapse',
'empty-cells', 'caption-side', 'vertical-align', 'align-*', 'justify-*',
'place-*', 'gap', 'row-gap', 'column-gap',
// 7. SVG
'fill', 'stroke', 'stroke-*', 'marker*', 'paint-order', 'vector-effect'
]
}
};
// .prettierrc
{
"cssDeclarationSorter": true, // 需安裝 prettier-plugin-css-order 或使用 css-declaration-sorter
"singleQuote": true,
"trailingComma": "none"
}
.box {
/* ✅ 邏輯屬性在前,物理屬性作為降級或不寫 */
margin-inline: auto;
padding-block: 16px;
border-inline-start: 1px solid;
border-block-end: 2px dashed;
/* ❌ 避免混用物理屬性,除非兼容極舊瀏覽器 */
/* margin-left: auto; margin-right: auto; */
}
:root {
--color-primary: #1890ff;
--space-md: 16px;
}
.component {
--component-bg: var(--color-bg); /* 組件級變量 */
--component-padding: var(--space-md);
background: var(--component-bg);
padding: var(--component-padding);
}
.card {
padding: 16px;
@media (min-width: 768px) {
padding: 24px; /* 同層級屬性順序不變 */
display: flex;
gap: 16px;
}
@container (min-width: 400px) {
flex-direction: row;
}
}
npm i -D stylelint stylelint-config-recess-order stylelint-ordernpx stylelint "**/*.{css,scss,vue}" --fix"lint:css": "stylelint \"**/*.{css,scss,vue}\""統一的屬性書寫順序不是「強迫症」,而是工程化協作的基礎設施。建議團隊在專案啟動時確立規範,接入自動化工具鏈,將格式爭議從 Code Review 中徹底移除。