Mark Ku's Blog

Stylelint - 提昇讓你的專案中的 CSS 樣式,擁有像 Eslint 的自動排版及檢查錯誤

前言

您是否也有在開發 Vue 的 CSS 或 SCSS 檔案時,是不是也有這個困擾,常常為了加了外層一個 Class ,又得手動重新調整縮排,團隊開發時,每個成員的撰寫風格都不一樣,Stylelint 則就是為了解決上述的問題而存在。

什麼是StyleLint

StyleLint 是一套檢查 CSS Coding Style ,就像是 eslint 一樣,在撰寫 css 時,它可以幫助團隊

  1. 在撰寫 css 能自動排版
  2. 提示屬性錯誤、限制大小寫、屬性的排序等..
  3. 協助團隊養成良好的 css 撰寫風格
  4. 修正風格及錯誤

功能展示影片

影片連結

安裝及設定

安裝相關的套件

npm install --save-dev stylelint stylelint-config-standard stylelint-order stylelint-scss

在專案根目錄中新增 .stylelintrc.json

{
    "extends": "stylelint-config-standard",
      "plugins": [
        "stylelint-scss",
        "stylelint-order"
    ],
    "rules": {
      "property-no-unknown": [
      true,
      {
        "ignore": [
        "promotioncolorfirst",
        "promotioncolorsecond",
        "promotioncolorthird"
        ]
      }
    ],
    "color-hex-case": "upper",
    "no-descending-specificity": null,
    "at-rule-no-unknown": null,
    "order/properties-order": [
        "position",
        "top",
        "right",
        "bottom",
        "left",
        "z-index",
        "display",
        "justify-content",
        "align-items",
        "float",
        "clear",
        "overflow",
        "overflow-x",
        "overflow-y",
        "margin",
        "margin-top",
        "margin-right",
        "margin-bottom",
        "margin-left",
        "border",
        "border-style",
        "border-width",
        "border-color",
        "border-top",
        "border-top-style",
        "border-top-width",
        "border-top-color",
        "border-right",
        "border-right-style",
        "border-right-width",
        "border-right-color",
        "border-bottom",
        "border-bottom-style",
        "border-bottom-width",
        "border-bottom-color",
        "border-left",
        "border-left-style",
        "border-left-width",
        "border-left-color",
        "border-radius",
        "padding",
        "padding-top",
        "padding-right",
        "padding-bottom",
        "padding-left",
        "width",
        "min-width",
        "max-width",
        "height",
        "min-height",
        "max-height",
        "font-size",
        "font-family",
        "font-weight",
        "text-align",
        "text-justify",
        "text-indent",
        "text-overflow",
        "text-decoration",
        "white-space",
        "color",
        "background",
        "background-position",
        "background-repeat",
        "background-size",
        "background-color",
        "background-clip",
        "opacity",
        "filter",
        "list-style",
        "outline",
        "visibility",
        "box-shadow",
        "text-shadow",
        "resize",
        "transition"
      ],
      "selector-pseudo-class-no-unknown": [
      true,
      {
        "ignorePseudoClasses": ["export"]
      }
    ],
    "property-no-unknown": [
      true,
      {
        "ignoreProperties": ["composes", "compose-with"],
        "ignoreSelectors": [":export", "/^:import/"]
      }
    ]
    }
}

為了在儲存時能夠,自動的修正錯誤,請在 vscode 中的 setting.json 請加入 => "source.fixAll.stylelint": true

"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.fixAll.stylelint": true 
  },

VS Code 安裝 style lint plugin

VS Code 介面顯示 Stylelint 擴充功能安裝頁面
VS Code 介面顯示 Stylelint 擴充功能安裝頁面

此時你在開發專案中的 css 時,存檔就能自動幫你自動修正錯誤

如果你想套用在整個專案

終端機輸入以下指令

// 檢查錯誤
 npx stylelint "**/*.css"
// 修正錯誤
 npx stylelint "**/*.css" --fix

或是在 package.json 內設定,透過 npm run lint:style 去執行

/ package.json

{
  "scripts": {
    "check:style": "stylelint **/*.{css,scss,sass,vue}",
    "lint:style": "stylelint **/*.{css,scss,sass,vue} --fix"
  }
}

作者

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。閱讀更多

覺得這篇有幫助?

作者做的免費工具、每日 Podcast 與電子報,都在這裡。

Mark Ku · 本文採用 CC BY 4.0 授權,轉載請註明作者並附上原文連結。

留言

訂閱電子報

訂閱後即時收到新文章通知,不錯過任何技術分享。

提交即表示同意接收電子報,隨時可

熱門文章

View all
Mark Ku
··604

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案

Oracle Cloud 永久免費方案 Linux 主機及固定 IP :0 元打造雲端解決方案
Mark Ku
··471

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南

告別 Postman 收費陷阱!開源 Git 原生 API 測試神器 Bruno 實戰指南
Mark Ku
··329

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略

一款免費開源類似於 Notion 類知識庫系統 — Outline Wiki 佈署與備份全攻略
Mark Ku
··240

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1

打造高效 API 管理平台:從 0 開始部署 Kong Gateway - Part 1
Mark Ku
··235

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調

訓練自己的 AI 語音:硬體門檻、開源模型比較與 LoRA 微調
Mark Ku
··216

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取

在 Ubuntu 上設置 Samba 來共享資料夾,讓 Windows 11 用戶可以存取