Safe-Listing Dynamic Classes
Use the safelist option in tailwind.config.js to ensure dynamically constructed class names that JIT cannot statically detect are always included.
The Safe-Listing Problem
JIT's static scan is a strength for most cases, but it creates a challenge when class names are determined at runtime — from a database, an API response, or a user's color preferences. These dynamically determined class names never appear as complete strings in your source files, so JIT does not generate them.
The safelist in tailwind.config.js is the official solution: you explicitly list classes (or patterns) that JIT must always generate, regardless of whether they appear in your template files.
Basic Safelist Configuration
Add a safelist array to your tailwind.config.js. List any complete class names that JIT cannot detect statically. JIT will always generate CSS for every class in this array, even if it does not find them in your content files.
This is the simplest form of safe-listing — individual class names. It works well for a small number of known dynamic classes.
// tailwind.config.js
module.exports = {
content: ['./src/**/*.{html,js,ts}'],
safelist: [
'bg-red-500',
'bg-green-500',
'bg-blue-500',
'bg-yellow-500',
'text-white',
'text-gray-900',
],
theme: {
extend: {},
},
plugins: [],
}All lessons in this course
- How the JIT Engine Works
- Safe-Listing Dynamic Classes
- Analyzing and Reducing Bundle Size
- Arbitrary Values and Their Cost