In Tailwind CSS v4, editors add utility classes to marketing content fetched from a CMS at run time; how do you guarantee those utilities are generated?
answer
- content that is never a file
- force generation from the stylesheet
- an inline argument to @source
- shell-style brace expansion
- v3 config option dropped
basics
~20 sSafelist them in the stylesheet with @source inline(), added in Tailwind CSS 4.1, which forces the listed utilities to be generated and brace-expands patterns such as {hover:,}bg-rose-{100,600}. v3's safelist option is not supported in v4.
solid answer
~40 sThe scanner only reads files, so classes that arrive in CMS content at run time are never generated. Since Tailwind CSS 4.1.0, `@source inline("…")` forces generation: a quoted, space-separated list of class names, brace expanded, so `{hover:,}bg-rose-{100,600}` yields `bg-rose-100`, `bg-rose-600` and their `hover:` versions. Each name still has to be a real utility, so custom colors must exist in the theme. `@source not inline("…")` does the opposite and blocks a class even when it is detected. v3's `safelist` option is not supported in v4, even through `@config`. Expansion multiplies quickly, so safelist only the vocabulary editors are allowed; better still, store a closed field such as `tone` in the CMS and map it to complete classes in code, which needs no safelist.
code
css · 8 lines@import "tailwindcss";
/* Callout styles the CMS editor guide allows, with hover states */
@source inline("{hover:,}bg-{emerald,amber,rose}-{100,600}");
@source inline("text-{emerald,amber,rose}-700");
/* Old CMS templates use a .container class of their own */
@source not inline("container");go deeper
Recall that classes coming from outside your files are not generated, and that v4 safelists them with @source inline() in the stylesheet.
Explain the argument: space-separated class names, brace expansion with ranges and an empty alternative for variants, and that each name must still match a real utility.
Show judgment: prefer a closed CMS field mapped in code, safelist only a published vocabulary, block collisions with @source not inline(), and migrate any v3 safelist array.
Own the boundary between content and code: decide which styling choices editors may make, how that vocabulary is published and reviewed, and what it costs in shipped CSS.
## The problem safelisting solves Tailwind generates only the utilities whose class names it finds in scanned files. That rule is usually a feature: the CSS stays small. It fails when class names reach the page from somewhere that is **never a file in the build**. The clinic's marketing page is a good example. Editors write callouts in a CMS and add utility classes such as `bg-emerald-100` or `text-rose-700` to the rich text, and the app fetches that HTML at run time. The scanner cannot read a database, so those classes are not generated and the callouts render plain. This differs from a class assembled in your own code: there you control the source and can write the class out in full; here you do not. ## `@source inline()`: the v4 safelist Since **Tailwind CSS 4.1.0**, `@source inline("…")` forces utilities to be generated whether or not a scanned file mentions them: ```css @import "tailwindcss"; @source inline("bg-emerald-100 text-rose-700 underline"); ``` - The argument is a quoted string; separate several class names with spaces. - Each name goes through the **same matching** as a scanned token. A class that maps to no utility, for example a color the theme does not define, produces nothing. - Variants are part of the class name, so `hover:underline` is safelisted by writing it in full. ## Brace expansion The string is **brace expanded**, the same way a shell expands braces, so one line can describe a family of classes: | Input | Expands to | |---|---| | `bg-{emerald,amber,rose}-100` | three background classes | | `bg-rose-{100..900..100}` | shades 100 to 900 in steps of 100 | | `bg-rose-{50,{100..900..100},950}` | the same plus 50 and 950 | | `{hover:,}text-rose-700` | `hover:text-rose-700` and `text-rose-700` | The empty alternative after the comma in `{hover:,}` is what produces the version without the variant. Expansion multiplies: three colors, eleven shades and two states are 66 rules. Every one ships to every visitor, so safelist the vocabulary editors actually use, not every palette. ## Blocking with `@source not inline()` `@source not inline("…")`, also new in 4.1.0, prevents utilities from being generated **even when the scanner detects them**. It accepts the same brace expansion. Typical uses: - A class name in imported markup that collides with a Tailwind utility, such as a legacy `container` class in old CMS templates, which would otherwise pick up Tailwind's `container` rule. - A utility the design system has banned, so that using it has no visible effect and gets caught in review. A class named in both an inline safelist and a `not inline()` block is not generated: the block wins. ## What replaced v3's `safelist` Tailwind v3 had a `safelist` option in `tailwind.config.js`. The v4 upgrade guide is explicit: the `corePlugins`, `safelist` and `separator` options are **not supported in v4**, even when a legacy JavaScript config is loaded with `@config`. A migrated project that kept its `safelist` array loses those utilities; the replacement is `@source inline()` in the stylesheet. ## Checking that the safelist works 1. Build the CSS and search it for one safelisted class that no source file mentions, such as `hover:bg-amber-600`; it should be present. 2. Search for one blocked class, such as `container`; it should be absent even though old templates use it. 3. Render a CMS page with each allowed callout style and compare it with the editor guide. A safelisted name that does not appear in the output usually means the name maps to no utility, usually a typo or a color missing from the theme. ## Designing it so the list stays small 1. **Prefer a closed field over raw classes.** If the CMS stores a `tone` field (`success`, `warning`, `danger`) and a component in your code maps it to complete class strings, the classes live in source and no safelist is needed at all. 2. **When editors must type classes, publish the vocabulary.** Safelist exactly the set the editor guide allows, and keep the `@source inline()` lines next to a comment pointing at that guide. 3. **Expect drift.** A class an editor invents outside the list renders unstyled with no error, so check new content against the list before it is published. 4. **On a 4.0 project,** `@source inline()` does not exist; upgrade within v4, or make the classes appear in full in a file the scanner reads.
- Why is a closed `tone` field in the CMS better than letting editors type any utility?A closed field lets a component in your code map each tone to complete class strings, so the classes live in source and need no safelist. Open-ended classes force you to safelist every shade and state up front, which grows the CSS for every visitor, and editors can still type something outside the list.
- A safelisted `bg-brand-600` still produces no CSS. Why?Names from `@source inline()` go through the same matching as scanned tokens, so a class that maps to no utility is dropped. Without a `brand` color in the theme there is no `bg-brand-600` utility to generate; define the color in the theme first.
- How do `@source not inline()` and an inline safelist interact when both name the same class?The block wins. `@source not inline()` marks the class as one never to generate, and that applies to scanned tokens and safelisted names alike. Use it for markup that collides with a utility name or for utilities the design system has retired.
- Keep v3's safelist array; @config loads it in v4.
- @source inline() can generate any class name, even one with no matching utility.
- Safelist whole palettes with every variant, since unused CSS costs nothing.
- @source not inline() only excludes files from scanning, not individual classes.
- Tailwind reads CMS content at run time, so no safelist is needed.
Assumes Tailwind CSS v4 (4.3.3); @source inline() and @source not inline() arrived in 4.1.0, and v3's safelist option is not supported in v4.