0Pricing
Tailwind CSS Academy · บทเรียน

กลไก JIT ทำงานอย่างไร

ทำความเข้าใจว่าโหมด JIT เริ่มต้นของ Tailwind v3 สแกนไฟล์ตามต้องการอย่างไร และสร้างเฉพาะคลาสยูทิลิตีที่ใช้จริงในมาร์กอัปของคุณ

กลไก JIT ทำงานอย่างไร เป็นบทเรียน Tailwind CSS Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Tailwind CSS Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is the JIT Engine

Tailwind's Just-In-Time (JIT) engine, introduced in Tailwind v2.1 and made the default in v3, fundamentally changed how Tailwind generates CSS. Instead of pre-generating every possible utility class combination at build time (resulting in megabytes of CSS), JIT scans your project files and generates only the classes you actually use.

The result is a CSS file that is typically just a few kilobytes in development and even smaller in production, with zero wasted code.

Pre-JIT vs JIT Approach

Before JIT, Tailwind used a purge step at the end of build: it generated all possible CSS (~10MB), then stripped any class not found in your templates. This was slow, required careful purge configuration, and broke when class names were dynamically constructed.

JIT inverts this: it scans files first and only generates CSS for classes it finds. The output is immediately small in every environment — development, staging, and production — and the build is significantly faster.

// Old approach: generate ALL classes, then purge
// - ~10MB initial CSS
// - Slow build
// - Purge config errors silently broke production

// JIT approach: scan first, generate only used classes
// - ~3-5KB CSS in development
// - Sub-second builds
// - No separate purge step needed

How JIT Scans Your Files

JIT reads the content array in tailwind.config.js to know which files to scan. It uses a fast regex-based scan (not a full HTML/JS parser) to find class-like strings. Any string that matches a known Tailwind class pattern gets its CSS generated.

This is why correctly configuring the content array is critical — if JIT cannot find a file, classes used in it will not be generated.

// tailwind.config.js
module.exports = {
  content: [
    './src/**/*.{html,js,jsx,ts,tsx}',
    './pages/**/*.{js,jsx,ts,tsx}',
    './components/**/*.{js,jsx,ts,tsx}',
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

On-Demand Class Generation

When JIT detects a class in your markup, it generates the exact CSS rule for it immediately. If you use bg-blue-500, only that one color rule is generated — not the entire blue color scale. If you later add bg-blue-700, JIT generates that new rule on the next rebuild.

In development, this happens in milliseconds thanks to the file watcher. JIT's approach means you get zero-overhead CSS — every byte in your stylesheet is actively used.

/* Only classes actually used in your templates are generated */

/* If you use: bg-blue-500, text-white, px-4, py-2, rounded-lg */
/* JIT generates: */
.bg-blue-500 { background-color: #3b82f6; }
.text-white { color: #fff; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.rounded-lg { border-radius: 0.5rem; }

/* NOT generated: bg-blue-100, bg-blue-200, ... bg-blue-900, text-gray-*, etc. */

Arbitrary Values Powered by JIT

JIT unlocks arbitrary value syntax — the ability to use any CSS value directly in a class name using square bracket notation. Before JIT, arbitrary values were impossible without custom configuration. JIT generates the rule on demand when it detects the bracket syntax.

For example, w-[347px], text-[#1a2b3c], or top-[calc(100vh-64px)] all work out of the box. JIT detects the brackets, parses the value, and generates exactly the CSS needed.

<!-- JIT generates CSS for arbitrary values on demand -->
<div class="w-[347px]">Exactly 347px wide</div>
<p class="text-[#1a2b3c] text-[22px]">Custom color and size</p>
<div class="top-[calc(100vh-64px)]">Viewport height minus navbar</div>
<div class="grid-cols-[1fr_2fr_1fr]">Custom grid columns</div>

JIT and Dynamic Class Names

A critical limitation of JIT's scan-based approach is that it cannot detect dynamically constructed class names. If you build a class name by string concatenation in JavaScript, JIT will not see the result at scan time.

The solution is to avoid partial class construction. Always use complete class strings, or use a safelist in the config for dynamically determined classes.

// BAD: JIT cannot statically detect the assembled class
const color = 'blue';
const shade = '500';
const className = 'bg-' + color + '-' + shade; // 'bg-blue-500' — NOT detected!

// GOOD: Use complete class strings
const classMap = {
  blue: 'bg-blue-500',
  red: 'bg-red-500',
  green: 'bg-green-500',
};
const className = classMap[color]; // full string — JIT detects all values in the object

JIT Watch Mode in Development

Running npx tailwindcss -i input.css -o output.css --watch starts JIT in watch mode. It monitors all files matching the content globs and rebuilds the CSS file within milliseconds whenever a file changes.

In a Vite or Next.js project, Tailwind's JIT is integrated into the build tool's HMR (Hot Module Replacement) pipeline, so CSS updates appear in the browser instantly without a full page reload.

# Start Tailwind JIT in watch mode (standalone)
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

# Vite integrates Tailwind automatically via postcss.config.js:
# postcss.config.js
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Variant Generation by JIT

Before JIT, you had to explicitly enable variants in the config file (like hover, focus, dark) for each utility group. Enabling every variant for every utility pre-generated an enormous CSS file.

JIT solves this completely — every variant works on every utility without configuration. hover:bg-blue-500, dark:text-white, lg:hover:bg-blue-700 — all generated on demand with zero config.

<!-- All of these work without any config in Tailwind v3 JIT -->
<button class="bg-blue-600 hover:bg-blue-700 active:bg-blue-800
               focus-visible:ring-2 focus-visible:ring-blue-500
               disabled:opacity-50 disabled:cursor-not-allowed
               dark:bg-blue-500 dark:hover:bg-blue-400
               sm:px-4 lg:px-6">
  Click me
</button>

Content Glob Patterns

The content array uses glob patterns to specify which files JIT should scan. A glob like './src/**/*.{html,js}' matches all HTML and JS files anywhere inside the src directory tree.

Common mistakes include forgetting file extensions, using incorrect base paths, or not including node_modules packages that contain Tailwind classes (like UI component libraries). You can also pass string arrays instead of globs for specific files.

// tailwind.config.js
module.exports = {
  content: [
    // All templates in src/
    './src/**/*.{html,js,jsx,ts,tsx,vue,svelte}',
    // Specific config file
    './index.html',
    // A UI library that uses Tailwind classes
    './node_modules/@my-ui-lib/components/**/*.{js,ts}',
  ],
}

JIT in Production vs Development

JIT generates the same set of CSS in both development and production — only the classes found in your content files. The difference in production is that Tailwind also applies minification (via the CSS minifier in your build tool) and potentially PostCSS autoprefixer for older browser support.

There is no separate purge step in Tailwind v3. Setting NODE_ENV=production in your build command enables minification but does not change which classes are generated — they were already minimal in development thanks to JIT.

# Development build (readable output)
NODE_ENV=development npx tailwindcss -i input.css -o output.css

# Production build (minified)
NODE_ENV=production npx tailwindcss -i input.css -o output.css --minify

# Result sizes:
# Development: ~8KB (readable, not minified)
# Production: ~3KB (same classes, minified + whitespace removed)

Layer Order and JIT

JIT respects Tailwind's CSS layer order: base styles come first, then component styles, then utility styles. This ordering is important because utilities have the highest specificity by convention — they should always win when conflicting with component styles.

Custom CSS written with @layer utilities is also generated by JIT alongside the built-in utilities. Custom CSS written outside any @layer falls between base and components in specificity, which can cause unexpected overriding behavior.

/* input.css */
@tailwind base;       /* Reset styles, lowest specificity */
@tailwind components; /* @layer components { .btn { ... } } */
@tailwind utilities;  /* All utilities, highest specificity */

/* Custom utility in JIT layer order --*/
@layer utilities {
  .scrollbar-hide {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  .scrollbar-hide::-webkit-scrollbar {
    display: none;
  }
}

Quick Check

Test your understanding of Tailwind CSS Mastery concepts from this lesson.

Lesson Recap

In this lesson you learned: JIT scans your content files as text to find class strings and generates CSS only for those classes, eliminating unused styles from day one; arbitrary values like w-[347px] are enabled by JIT's on-demand generation; and dynamic class construction breaks JIT detection because it cannot execute JavaScript — always use complete class strings in safelisted objects. Next up we learn about safe-listing dynamic classes.

คำถามที่พบบ่อย

บทเรียน “กลไก JIT ทำงานอย่างไร” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลไก JIT ทำงานอย่างไร” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Tailwind CSS Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Tailwind CSS Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กลไก JIT ทำงานอย่างไร”

ทำความเข้าใจว่าโหมด JIT เริ่มต้นของ Tailwind v3 สแกนไฟล์ตามต้องการอย่างไร และสร้างเฉพาะคลาสยูทิลิตีที่ใช้จริงในมาร์กอัปของคุณ คุณปฏิบัติ Tailwind CSS Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Tailwind CSS Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Tailwind CSS Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “กลไก JIT ทำงานอย่างไร” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Tailwind CSS Academy นี้ได้ไหม

ได้ บทเรียน Tailwind CSS Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กลไก JIT ทำงานอย่างไร
  2. การระบุคลาสแบบไดนามิกในรายการที่อนุญาต
  3. การวิเคราะห์และลดขนาดชุดไฟล์
  4. ค่าแบบกำหนดเองและต้นทุนของค่าเหล่านั้น
← กลับไปที่ Tailwind CSS Academy