CSS 沒有變數、函式或模組系統(早期規格),隨著專案增長,樣式表極易變成「義大利麵代碼」。良好的註解能:
stylelint、PostCSS)產生文件/*------------------------------------
1. Reset & Normalize
2. Variables & Tokens
3. Base Styles (Typography, Links)
4. Layout (Header, Footer, Grid)
5. Components (Buttons, Cards, Forms)
6. Utilities (Spacing, Visibility)
7. Page-specific Overrides
8. Media Queries (Responsive)
-----------------------------------*/
使用統一的視覺標記,方便編輯器摺疊/搜尋:
/*** Header ***/
#header { height: 145px; position: relative; }
#header h1 { width: 324px; margin: 45px 0 0 20px; float: left; height: 72px; }
/*** Content ***/
#content { background: #fff; width: 650px; float: left; min-height: 600px; overflow: hidden; }
#content h1 { color: #F00; } /* 設定字體顏色 */
#content .posts { overflow: hidden; }
#content .recent { margin-bottom: 20px; border-bottom: 1px solid #f3f3f3; position: relative; overflow: hidden; }
/*** Footer ***/
#footer { clear: both; padding: 50px 5px 0; overflow: hidden; }
#footer h4 { color: #b99d7f; font-family: Arial, Helvetica, sans-serif; font-size: 1.1em; }
針對特定規則補充上下文:
.clearfix::after {
content: "";
display: table;
clear: both;
/* 修正浮動塌陷,支援 IE8+ */
}
@media (max-width: 768px) {
.sidebar { display: none; }
/* 行動版隱藏側欄,改由漢堡選單觸發 */
}
/* TODO: 重構為 Flexbox */、/* FIXME: IE11 flex bug */。cssnano 或 clean-css 自動清除,減少檔案體積。