JIT mode in Tailwind creates styles only when you use them. This makes your website faster and smaller.
JIT mode and on-demand generation in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
extend: {},
},
plugins: [],
}In Tailwind CSS v3 and later, JIT mode is enabled by default, so the mode: 'jit' line is no longer needed.
The content array tells Tailwind where to look for class names.
index.html.module.exports = {
content: ['./index.html'],
theme: {},
plugins: [],
}module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx,html}'],
theme: {
extend: {
colors: {
customBlue: '#1e40af',
},
},
},
plugins: [],
}This simple page uses Tailwind classes that JIT mode generates on demand. Only the classes used here will be in the CSS file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>JIT Mode Example</title> <link href="./dist/output.css" rel="stylesheet"> </head> <body class="bg-gray-100 p-6"> <h1 class="text-3xl font-bold text-blue-600">Hello, Tailwind JIT!</h1> <button class="mt-4 px-4 py-2 bg-green-500 text-white rounded hover:bg-green-700"> Click me </button> </body> </html>
JIT mode watches your files and creates styles instantly as you add classes.
Make sure your content paths include all files where you use Tailwind classes.
JIT mode helps keep your CSS file very small and fast to load.
JIT mode creates only the CSS you use, making your site faster.
It watches your files and updates styles instantly while you code.
Set content to list your files to use it. In Tailwind v3+, JIT is enabled by default.
Practice
Solution
Step 1: Understand JIT mode purpose
JIT mode creates CSS only for classes used in your files, not all possible classes.Step 2: Compare options
It generates only the CSS classes you actually use, making the site faster. matches this behavior; others describe incorrect or unrelated features.Final Answer:
It generates only the CSS classes you actually use, making the site faster. -> Option BQuick Check:
JIT mode = on-demand CSS generation [OK]
- Thinking JIT preloads all CSS
- Confusing JIT with disabling CSS
- Assuming JIT converts CSS to JS
Solution
Step 1: Identify correct config keys
Tailwind usesmode: 'jit'andcontentarray for files.Step 2: Check options
Only module.exports = { mode: 'jit', content: ['./src/**/*.{html,js}'] } uses correct keys and values; others use wrong keys or values.Final Answer:
module.exports = { mode: 'jit', content: ['./src/**/*.{html,js}'] } -> Option DQuick Check:
Config keys = mode and content [OK]
- Using wrong keys like jit or files
- Setting mode to 'fast' instead of 'jit'
- Using 'paths' instead of 'content'
module.exports = { mode: 'jit', content: ['./index.html'] }And this HTML:
<div class="bg-blue-500 p-4">Hello</div>
What CSS classes will be generated?
Solution
Step 1: Understand JIT CSS generation
JIT mode generates CSS only for classes found in the content files.Step 2: Check classes in HTML
Classesbg-blue-500andp-4appear, so their styles are generated.Final Answer:
Only styles for bg-blue-500 and p-4 classes. -> Option AQuick Check:
JIT generates CSS only for used classes [OK]
- Thinking all classes generate CSS
- Assuming JIT disables CSS generation
- Believing partial classes generate partial CSS
Solution
Step 1: Check JIT file watching setup
JIT mode watches files listed incontentto generate CSS on changes.Step 2: Identify cause of missing CSS
If edited files are not incontent, JIT won't generate CSS for new classes.Final Answer:
The content array in Tailwind config does not include the edited files. -> Option CQuick Check:
Content array must include all source files [OK]
- Forgetting to update content array
- Assuming JIT needs browser restart
- Thinking Tailwind lacks dynamic class support
bg-${color}-500 where color is a variable. How can you ensure Tailwind JIT generates the correct CSS?Solution
Step 1: Understand JIT limitation with dynamic classes
JIT cannot detect dynamic class names built from variables in code.Step 2: Provide all possible classes explicitly
You must list all possible class names in your files or use the safelist option in Tailwind config.Final Answer:
Add all possible classes like bg-red-500, bg-blue-500 explicitly in your files or safelist in config. -> Option AQuick Check:
Dynamic classes need explicit listing or safelist [OK]
- Expecting JIT to detect dynamic classes automatically
- Disabling JIT unnecessarily
- Using inline styles instead of Tailwind classes
