การตรวจรูปแบบ Tailwind ด้วย ESLint
ตั้งค่า eslint-plugin-tailwindcss เพื่อแจ้งเตือนชื่อคลาสที่ไม่ถูกต้องและยูทิลิตีที่ขัดแย้งกัน พร้อมบังคับใช้หลักเกณฑ์คลาสของทีม
การตรวจรูปแบบ Tailwind ด้วย ESLint เป็นบทเรียน Tailwind CSS Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Tailwind CSS Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Lint Your Tailwind Classes
Typos in Tailwind class names are silent errors — the browser simply ignores unknown classes with no warning in the console. A missing letter like bg-blue-50O (capital O instead of zero) applies no background, and the bug may go unnoticed for days. ESLint with a Tailwind plugin catches these issues at development time before they reach production.
Installing eslint-plugin-tailwindcss
eslint-plugin-tailwindcss is the most widely used ESLint plugin for Tailwind. It validates class names against your Tailwind config, warns about conflicting utilities, and can enforce your team's class ordering conventions. Install it alongside ESLint in your project's dev dependencies.
npm install -D eslint eslint-plugin-tailwindcss
# If using a flat config (ESLint 9+)
npm install -D @eslint/jsConfiguring the Plugin (Legacy Config)
In the traditional .eslintrc format, add tailwindcss to the plugins array and extend the recommended configuration. The recommended set enables the three core rules: classnames-order, no-custom-classname, and no-contradicting-classname.
// .eslintrc.js
module.exports = {
extends: ['plugin:tailwindcss/recommended'],
plugins: ['tailwindcss'],
rules: {
'tailwindcss/classnames-order': 'warn',
'tailwindcss/no-custom-classname': 'warn',
'tailwindcss/no-contradicting-classname': 'error',
},
};Configuring the Plugin (Flat Config)
For ESLint 9's new flat config format, import the plugin and spread the recommended configuration into your config array. Point the tailwindConfig setting to your config file so the plugin reads your custom theme values and plugins when validating class names.
// eslint.config.js
import tailwind from 'eslint-plugin-tailwindcss';
export default [
...tailwind.configs['flat/recommended'],
{
settings: {
tailwindcss: {
config: './tailwind.config.js',
},
},
},
];The no-contradicting-classname Rule
The no-contradicting-classname rule catches conflicting utilities applied to the same element — for example, using both block and inline on the same div, or w-full and w-auto together without a responsive prefix. These conflicts produce unpredictable results that are hard to debug without linting support.
<!-- ESLint error: block and inline conflict -->
<div class="block inline text-red-500">
<!-- Correct: use responsive prefix to switch at breakpoints -->
<div class="block md:inline text-red-500">The no-custom-classname Rule
The no-custom-classname rule warns when you write a class that is not part of the Tailwind output — neither a built-in utility nor a custom one defined in your config or plugins. This helps catch typos and prevents developers from writing plain CSS class names in a Tailwind project without going through the proper channels.
<!-- ESLint warning: 'card-header' is not a Tailwind utility -->
<div class="card-header p-4 bg-white">
<!-- Option 1: Use only Tailwind utilities -->
<div class="rounded-lg bg-white p-4 shadow">
<!-- Option 2: Add 'card-header' to whitelist in ESLint config -->
/* settings: { tailwindcss: { whitelist: ['card-header'] } } */The classnames-order Rule
The classnames-order rule enforces the same canonical class order that the Prettier plugin produces. When you have both tools, configure one as the source of truth for ordering. Most teams use Prettier for auto-formatting and disable the ESLint ordering rule to avoid conflicts between the two tools.
// .eslintrc.js — disable ordering rule when using Prettier plugin
module.exports = {
extends: ['plugin:tailwindcss/recommended'],
rules: {
// Prettier handles ordering — disable here to avoid conflicts
'tailwindcss/classnames-order': 'off',
// Keep these enabled
'tailwindcss/no-custom-classname': 'warn',
'tailwindcss/no-contradicting-classname': 'error',
},
};Whitelisting Intentional Custom Classes
If your project uses custom CSS class names alongside Tailwind utilities — for example, classes from a third-party animation library or legacy CSS — add them to the whitelist setting. This silences false positives while still catching genuine typos in Tailwind class names.
// .eslintrc.js
module.exports = {
extends: ['plugin:tailwindcss/recommended'],
settings: {
tailwindcss: {
config: './tailwind.config.js',
// Allow these non-Tailwind class names
whitelist: ['animate-fade-in', 'swiper-slide', 'js-modal-open'],
},
},
};Running ESLint in the Terminal
Add a lint script to your package.json that targets all template files. Run it locally and in CI to surface class name errors before they merge. The --fix flag auto-corrects ordering issues but cannot fix typos — those require manual review.
// package.json
{
"scripts": {
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx,html}'",
"lint:fix": "eslint --fix 'src/**/*.{js,jsx,ts,tsx,html}'"
}
}Editor Integration for Real-Time Feedback
Install the ESLint VS Code extension to see underlined errors directly in the editor as you type. Tailwind class errors appear as yellow warnings or red errors inline, making it impossible to miss a typo. Combine this with the Tailwind CSS IntelliSense extension for autocomplete and the linting closes the feedback loop completely.
// .vscode/extensions.json — recommended extensions for the team
{
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"bradlc.vscode-tailwindcss"
]
}Combining ESLint and Prettier Effectively
Use ESLint for validation — catching typos, contradictions, and unknown class names. Use Prettier for formatting — sorting classes into the canonical order. Disable the classnames-order ESLint rule to let Prettier own sorting exclusively. This separation of concerns gives you the best of both tools without conflict.
// Recommended combined setup
// ESLint: validates correctness
// Prettier: formats order
// package.json scripts
"lint": "eslint src/",
"format": "prettier --write src/",
"precommit": "npm run lint && npm run format"Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: installing eslint-plugin-tailwindcss to catch typos and contradictions, the three core rules (no-contradicting-classname, no-custom-classname, classnames-order), and combining ESLint with Prettier so each tool owns its responsibility. Next up we define team conventions and a style guide.
คำถามที่พบบ่อย
บทเรียน “การตรวจรูปแบบ Tailwind ด้วย ESLint” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจรูปแบบ Tailwind ด้วย ESLint” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Tailwind CSS Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจรูปแบบ Tailwind ด้วย ESLint”
ตั้งค่า eslint-plugin-tailwindcss เพื่อแจ้งเตือนชื่อคลาสที่ไม่ถูกต้องและยูทิลิตีที่ขัดแย้งกัน พร้อมบังคับใช้หลักเกณฑ์คลาสของทีม คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Tailwind CSS Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Tailwind CSS Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจรูปแบบ Tailwind ด้วย ESLint” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Tailwind CSS Academy นี้ได้ไหม
ได้ บทเรียน Tailwind CSS Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตรวจสอบผลลัพธ์ CSS
- การเรียงคลาสและปลั๊กอิน Prettier
- การตรวจรูปแบบ Tailwind ด้วย ESLint
- หลักเกณฑ์ของทีมและคู่มือสไตล์