Atomic CSS in dx-styles: a build option, and when to use it
dx-styles 1.6 added a second output model, and 1.7 makes it cheaper when the page renders. By default a style still becomes one class with all of its declarations. With one build option, output: "atomic", every declarati
dx-styles 1.6 added a second output model, and 1.7 makes it cheaper when the page renders. By default a style still becomes one class with all of its declarations. With one build option, output: "atomic", every declaration becomes a shared class of its own, an atom, and every other style that sets the same property to the same value reuses it. You write styles the same way in both models; only the build output changes.
Atomic CSS is not new. StyleX, Panda and Compiled are built around it, and the usual pitch is that the stylesheet stops growing with the size of the application. That holds, but it is only part of the trade: the class names move into your JavaScript, the cascade needs rules of its own, and some styles cannot be expressed at all. Which model fits depends on what your application looks like, so we measured both on our public benchmark and wrote down where each one wins.
The two models
Here is one style in both:
const button = css({ color: "red", padding: 4 });
/* semantic (default) */
.button_bhhycyd { color: red; padding: 4px; }
/* atomic */
.dx-39_vtc9dhbe { color: red; }
.dx-9b_dfhgmh3l { padding: 4px; }
With minifyClassNames: true, button is the string "dx-39_vtc9dhbe dx-9b_dfhgmh3l" in the atomic model. A second style that sets color: red gets the same dx-39_vtc9dhbe, so the rule exists once in the CSS however many components use it.
You switch with a build option:
wyw({
processors: {
dxStyles: { output: "atomic", minifyClassNames: true },
},
});
Without it, or with output: "semantic", the CSS and the class names are the same as before the option existed. The runtime grows a little in both models, because cx now recognizes atoms.
How the atomic model keeps the cascade
In semantic CSS, when two declarations set the same property on one element, the later one in the stylesheet wins. Atoms are shared between styles, so their order in the stylesheet means nothing: the same atom serves a style that wants it first and another that wants it last. dx-styles needs two things to make atoms behave like the styles you wrote.
The first is merging. cx, which recipes use too, keeps only the last atom of each property and condition, so cx(base, active) drops base's color atom when active has one. That works only if every class string goes through cx (or css(a, b), or a recipe). A template string that glues two class strings together keeps both atoms, and then the stylesheet order picks the winner by chance.
The second is ranking. Some conflicts are inside one style: a :hover color and an :active color, a padding and a paddingTop, a value inside @media and one outside it. dx-styles ranks atoms by fixed rules and writes the rank into the selector, repeating the atom's class:
r = 1 + L + 4Β·A + 8Β·P
L is how deep the property sits under its shorthands (padding 0, padding-top 2), A is 1 inside @media, @supports or @container, and P ranks the pseudo-class (:hover 1 up to :disabled 4). So :active beats :hover, a declaration inside @media beats one outside it, and a longhand beats its shorthand, whatever order the rules end up in. The longest selector, a border-side longhand under :disabled inside @media, repeats its class 40 times. It compresses well, and it matters when you override atoms from outside (more on that below).
When the rules and your code disagree, dx-styles fails the build. If a style's result would depend on the order of its declarations, or would differ from what the semantic model gives, the error tells you how to rewrite it:
css({ paddingTop: 4, padding: 0 }); // error: a longhand wins over its shorthand
css({ padding: 0, paddingTop: 4 }); // fine
css({
"@media (min-width: 40rem)": { gap: 8 },
"@media (min-width: 64rem)": { gap: 16 },
}); // error: overlapping @media on one property
css({
"@media (40rem <= width < 64rem)": { gap: 8 },
"@media (width >= 64rem)": { gap: 16 },
}); // fine
The atomic model never ships a style that looks silently different from the semantic one, at least for the element the style is attached to. Rules that style other elements ("& span", "& > summary") are the documented exception: the build cannot see the DOM, so it cannot check them against those elements' own styles. The docs show the cases and a CSS-variable pattern that keeps both models equal.
The other atomic engines settle the same conflicts by a fixed sort and build anyway. In StyleX the last style applied wins, and rules are ordered by priority numbers for pseudo-classes and at-rules. Panda puts unconditional rules first, then selector-conditioned, then at-rule-conditioned, with an opt-in lint rule for a shorthand next to its longhand. Compiled ignores the order of properties in your source and sorts shorthands before longhands, with a lint rule that sorts them for you. Each of these is a reasonable design. dx-styles stops the build instead, because a cascade that silently differs from the code you wrote is the one difference that neither a code review nor a type check will catch.
What we measured
The numbers come from dx-styles/benchmarks, which builds generated scenes with each library written in its own idiom. Two scenes matter here.
The small scene is a design system: 104 tokens and 34 recipe families with 1,308 variants, rendered as one page of 3,642 elements. Its recipes take their values from the tokens and a few fixed sizes, so declarations repeat across variants, which is the case atoms are made for. Most elements get their class from a recipe call when the page renders. Every library builds it, and the results give build time, size, first paint and a render check for each.
The app scene puts an application on top of that design system: F feature components with their styles next to their code, tokens read through re-export barrels, and about 5.5 ordinary modules of logic per feature. Its JavaScript is mostly that logic, about 8 MB raw at 2,000 features. For this post the scene got two parameters:
- V, the share of a feature's declarations that take a value no other declaration in the scene has. At 0, every value comes from the design system's tokens, so most declarations repeat across features: the best case for atoms. At 50%, half of them are one-off values, each of which becomes an atom that no other style shares. Real one-off values repeat now and then, so a given V is a slightly pessimistic version of an application with that many ad hoc values.
- B, the share of feature rules that set one property (a padding, the gap or the font size) under two non-overlapping width ranges with
@media.
The size sweep builds dx-styles 1.7.0 in both models from 250 to 2,000 features, at four levels of V and two of B. The two models share one configuration (minifyClassNames: true, no prefixer, Vite 8.3.5 and its default CSS minifier); only output differs. Sizes do not depend on the machine, so one build per point is enough. At every level the atomic build renders like the semantic one: Chromium compares the computed styles of every element, at rest and in its :hover, :focus-visible, :active and :disabled states, at 1,000 features and, for the responsive levels, at three window widths. None of the 27 comparisons found a difference.
Size
With every value taken from the design tokens (V = 0), the atomic stylesheet stops growing after the first few hundred features, because almost every new declaration already has an atom. The semantic stylesheet grows with every feature.
| Features | Semantic CSS | Atomic CSS + added JS | Atomic CSS |
|---|---|---|---|
| 250 | 31.7 KB | 20.7 KB | 7.6 KB |
| 1,000 | 70.0 KB | 50.2 KB | 7.6 KB |
| 2,000 | 119.3 KB | 86.4 KB | 7.6 KB |
Gzip, app scene, V = 0, B = 0, dx-styles 1.7.0.
The class names have to go somewhere, though. Every element carries a string of atoms instead of one class name, and those strings live in your JavaScript. The middle column adds that JavaScript to the atomic CSS: it takes back part of the saving, and the atomic build still comes out smaller. Brotli compresses the repeated atom names much better than gzip, so under brotli the net saving is about twice as large. The rest of the bundle is the same in both builds, so every difference here comes from styles.
The best case holds only while values repeat. A one-off value becomes an atom that no other style shares, and its class name still travels in the JavaScript. Here is the app scene at 2,000 features; the two total columns are the atomic build minus the semantic one, CSS and JavaScript together:
| One-off values (V) | Atomic CSS, gzip | Semantic CSS, gzip | Total, gzip | Total, brotli |
|---|---|---|---|---|
| 0% | 7.6 KB | 119.3 KB | β33.0 KB | β65.9 KB |
| 10% | 41.2 KB | 131.5 KB | +4.1 KB | β33.3 KB |
| 25% | 90.4 KB | 144.8 KB | +61.7 KB | +10.8 KB |
| 50% | 177.5 KB | 163.5 KB | +163.7 KB | +89.7 KB |
Under gzip the atomic build loses its lead when about one value in ten is one-off, under brotli when about one in four is. With half of the values one-off, even the atomic stylesheet is larger than the semantic one, because each such value needs a selector of its own instead of sharing one with the rest of its style. Brotli results on a bundle this large also jump by a few kilobytes from one size to the next, so read the brotli column as a trend.
The size of the application moves the break-even too. With one value in ten one-off, the atomic build is ahead in a small application and falls behind as it grows:
| Features | 250 | 1,000 | 2,000 |
|---|---|---|---|
| Atomic minus semantic, gzip | β6.9 KB | β0.3 KB | +4.1 KB |
App scene, 10% one-off values, B = 0, CSS and JavaScript together.
Deploys and caching
A stylesheet shipped as one file is invalidated for every visitor by any change to it, in either model. In the sweep the CSS file changed at every step in both models, even where the atomic build gained no new atom: atoms come out in module order, so new modules move existing ones. The models differ in how much of the file changes:
| Change in the app scene at 2,000 features | Semantic CSS | Atomic CSS |
|---|---|---|
| one value changed to another token value | one rule replaced | the same file, byte for byte |
| one feature added | 3 new rules | no new atom at V = 0, one at V = 25%; the file changes anyway |
| 250 features added, token values only | 1,448 new rules | 1 new atom |
| 250 features added, 10% one-off values | 1,448 new rules | 318 new atoms |
| 250 features added, 50% one-off values | 1,448 new rules | 1,656 new atoms |
So the atomic stylesheet survives edits to existing components that stay within your tokens, and does not survive new components. If your deploys mostly tweak existing screens, returning visitors keep the cached CSS; if they mostly add screens, both models invalidate it just as often.
Responsive rules and the minifier
At B = 25%, a quarter of the feature rules set a padding, the gap or the font size under two width ranges, and the CSS minifier decides what the atomic build costs. lightningcss, Vite's default, keeps every copy of an atom that a feature module repeats inside @media, so the atomic stylesheet grows with every feature again. esbuild merges the copies, and the stylesheet stays flat:
| Atomic build, 2,000 features, B = 25% | lightningcss | esbuild |
|---|---|---|
| CSS, raw | 444 KB | 95 KB |
| CSS, gzip | 14.5 KB | 8.2 KB |
| Total against the semantic build, gzip | β30.1 KB | β35.3 KB |
Gzip hides most of the duplication, but the browser parses the raw bytes. With atoms and media queries in Vite, set build.cssMinify: "esbuild".
The scene writes its ranges so that they do not overlap, as the atomic model requires: two min-width queries on one property, the usual mobile-first cascade, fail the build and have to be written as ranges (the example near the top of this post). That is a rewrite to plan for in a codebase that relies on overlapping breakpoints.
Build time and first paint
Atoms cost build time: the atomic pass splits every style into atoms and checks that no result depends on their order. Warm builds, median of 7 rounds:
| Warm build | Small scene | App scene, 2,000 features |
|---|---|---|
| semantic | 0.62 s | 9.68 s |
| atomic | 0.89 s | 10.28 s |
| difference | +0.27 s | +0.60 s (6.2%) |
Two identical semantic builds in each run differed by less than 1%, so the difference is the atomic pass and not noise. Cold builds and the other libraries are in the results of the small scene and the app scene.
In 1.6 the atomic page also painted later than the semantic one, although it ships about as much JavaScript as StyleX. First paint in Chromium on the small scene, median of 10 loads:
| dx-styles | Semantic | Atomic | StyleX |
|---|---|---|---|
| 1.6 (run of November 23) | 148 ms | 166 ms | 152 ms |
| 1.7 (run of December 2) | 152 ms | 160 ms | 156 ms |
The time went into recipe calls. A recipe joins its base and variant classes with cx, and with atoms that join has to keep the last atom of every property, so it splits a dozen or two atom names per call where the semantic build joins two class names. A profile of the first load put the extra script time in cx, and on the small scene it was often just enough to push the first paint to the next frame.
dx-styles 1.7 makes that join cheaper: a recipe parses each of its class strings once and reuses the result, and cx reads the key of each atom once instead of twice. The joined class names are the same as before. The gap shrank by more than half, and the atomic page now paints about where StyleX does. What is left is the joining itself, which still costs more with atoms, and the larger JavaScript. Compare within a row: the second run was a few milliseconds slower for most libraries.
Next to other atomic engines
On the small scene all four atomic builds come out smaller than semantic dx-styles:
| Build | Warm build, s | Total gzip, KB | CSS gzip, KB | First paint, ms | Renders like semantic dx-styles |
|---|---|---|---|---|---|
| dx-styles 1.7.0, semantic | 0.62 | 99.6 | 19.6 | 152 | |
| dx-styles 1.7.0, atomic | 0.89 | 90.0 | 6.2 | 160 | yes |
| StyleX 0.19.1 | 1.90 | 89.5 | 5.4 | 156 | yes |
| Panda 2.4.0 | 0.22 | 93.8 | 4.2 | 176 | yes |
| Compiled 2.0.0 | 5.21 | 88.7 | 5.1 | 156 | no, 5 elements |
Compiled's default class hash gives two different declarations the same class, a known issue we described in the previous post. With its collisionResistantHash option it renders like the rest, at 90.1 KB gzip (diagnostics).
At 2,000 features in the app scene:
| Build | Warm build, s | Total gzip, KB | CSS gzip, KB | Renders like semantic dx-styles |
|---|---|---|---|---|
| dx-styles 1.7.0, semantic | 9.68 | 1,023.1 | 119.3 | |
| dx-styles 1.7.0, atomic | 10.28 | 990.2 | 7.6 | yes |
| StyleX 0.19.1 | 10.72 | 931.3 | 6.8 | yes |
| Panda 2.4.0 | 3.32 | 981.6 | 5.1 | yes |
| Compiled 2.0.0 | 33.58 | 935.5 | 6.2 | no, 243 elements |
Here StyleX and Compiled come out smaller than atomic dx-styles. The stylesheets are about the same size; the difference is in the JavaScript. The StyleX and dx-styles bundles carry the same number of class references, but a StyleX class name is half as long as a dx-styles atom name (dx-0v_ntop8sce). Panda builds about three times faster than dx-styles and StyleX. Compiled renders differently the 243 elements whose colors hit its hash collision.
First paint is left out of this table because on this scene it moved between runs by more than the libraries differ from each other: the same dx-styles 1.6.0 bundle painted at 524 ms in one run and at 558 ms in the next.
When to choose which
The sizes above come from one generated scene, so treat them as directions rather than thresholds. The setting is one line, and the reliable answer is to build your own application both ways and compare.
| If your application⦠| Fits better | From the measurements |
|---|---|---|
| takes nearly every value from design tokens | atomic | at 2,000 features the CSS stays at 7.6 KB and the total is 33 KB smaller under gzip, 66 KB under brotli |
| has many one-off values | semantic | under gzip atomic is behind from about 10% one-off values, under brotli from about 25% |
| is small | either | at 250 features the two totals stay within 11 KB gzip of each other at every level of one-off values |
| ships mostly edits to existing screens | atomic | a changed value leaves the atomic CSS file byte for byte the same |
| ships mostly new screens | either | a new feature changes the CSS file in both models |
uses @media and Vite's default minifier |
semantic, or atomic with build.cssMinify: "esbuild"
|
lightningcss keeps repeated atoms: 444 KB raw against 95 KB |
| needs the fastest build | semantic | the atomic pass adds 0.27 s on the small scene and 6.2% on the app scene |
| renders many recipe-styled elements at once | either | on a page of 3,642 elements the atomic build paints 8 ms after the semantic one and 4 ms after StyleX |
| is a library | semantic | class names are a stable contract for consumers |
| is overridden by other CSS, layered or not | semantic | an atom repeats its class up to 40 times and wins over any layer; the semantic build can put its own rules in a layer |
uses :has(&), @layer or @scope inside styles, overlapping breakpoints, or rules that style other elements |
semantic, or rewrite first | these fail the atomic build or fall outside its checks |
What the atomic model costs
- Class names move into your JavaScript. Each element carries a string of atoms instead of one class, so part of the CSS saving comes back as JavaScript, and recipe calls do more work when the page renders.
- Some styles become build errors: declarations whose result depends on their order, overlapping
@mediaon one property, a physical and a flow-relative property of one level (marginTopnext tomarginInlineStart), selectors that do not select the styled element (:has(&),:is(&, .card)), and@layeror@scopeinside a style. Each error names a rewrite, but a large codebase may need a pass before it builds. - Overriding from outside is harder. An atom repeats its class up to 40 times, so external CSS beats it only with a higher specificity. CSS inside
@layeralways loses. The docs list three ways that work: a CSS variable the style reads, another dx-styles style merged withcx, or!importantas a last resort. The semantic model got an easier way in 1.7: with thelayeroption its rules go into a cascade layer, and unlayered application styles override them whatever their specificity. The atomic build fails with that option, because atoms stay outside layers. - Devtools show atoms. With
minifyClassNames: falseevery class string also carries a label, the class name the semantic model would give (button_bhhycyd__tone-accent), andbun run explainlists every style that uses an atom. WithminifyClassNames: true, which is what you ship, there is no label. - The minifier matters. lightningcss, Vite's default CSS minifier, keeps repeated atoms inside
@media; esbuild and cssnano remove them. - CSS code splitting copies atoms. Each CSS chunk carries every atom it needs, so atoms shared by several routes are repeated in their chunks.
- Libraries should stay semantic. Their class names are a stable contract for consumers. On one page, a semantic library and an atomic application meet through ordinary specificity and stylesheet order, as any two stylesheets do.
Turning it on
Update dx-styles and your wyw-in-js integration together:
npm install dx-styles@^1.7.0
npm install -D @wyw-in-js/[email protected]
Then set output: "atomic" in the dxStyles processor options and build. If the build fails, the error names the file, the style, both declarations and a rewrite; the atomic output guide covers every case, including how to order a recipe base against its variants and how to turn a mobile-first min-width cascade into ranges. Join class strings with cx, not template strings. Ship with minifyClassNames: true, and if your styles use @media, consider build.cssMinify: "esbuild" in Vite.
Since 1.6, dx-styles requires wyw-in-js 2.7.2, which moves minimatch to a release with a fix for a denial-of-service advisory.
Anton Evzhakov maintains Linaria and is the author of wyw-in-js, the zero-runtime engine behind Linaria and MUI's Pigment CSS. He builds dx-styles.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.