Tired of Tailwind? A type-safe design system for Twig with Symfony UX CSS

πŸ‡¬πŸ‡§

​The class string that made me leave

This is the primary variant of the badge under every post title, as it was a month ago:

primary: 'text-primary-700 bg-primary-500/10 border-primary-500/20 dark:text-primary-300 dark:bg-primary-400/10 dark:border-primary-400/20',

Six classes, and three of them only say "the same thing, in dark mode". The templates of this site had 61 dark: classes like these.

And if I had typed bg-primry-500/10, Tailwind would have generated nothing and said nothing. It reads templates as plain text, and a class it does not know is just a word.

I liked Tailwind for years. Now I wanted a design system my tools understand: tokens declared once, and an error when I use one that does not exist.

So last month I moved this site to Symfony UX CSS. 45 files changed, and the pages look the same.

Full disclosure: I am the author of the component. Take my enthusiasm with a grain of salt, and judge the code instead.

​The same badge, written as a hash

UX CSS adds a css() function to Twig, in the spirit of Panda CSS. You give it a hash of style properties, and it returns class names. For components with variants, it has cva(), which takes the same hashes. Here is the badge again, size variants trimmed:

{% set style_badge = cva(
    base: {
        display: 'inline-flex',
        alignItems: 'center',
        rounded: 'md',
        borderWidth: 1,
        fontWeight: 'medium',
        transition: 'colors',
    },
    variants: {
        color: {
            primary: {
                color: 'accent.fg',
                bgColor: 'accent/10',
                borderColor: 'accent/20',
            },
            gray: {
                color: 'fg.secondary',
                bgColor: 'fg.muted/10',
                borderColor: 'fg.muted/20',
            },
        },
        hoverable: {
            'true': {
                bgColor: {_hover: 'accent/20'},
            },
            'false': {},
        },
    },
) %}

It is longer, yes. But it reads like CSS, and there is no dark: left. accent.fg is a semantic token, and the token knows what it becomes in dark mode.

The browser gets atomic classes, like c_accent.fg and bg-c_accent/10, and one stylesheet with one rule per class:

.c_accent\.fg {
    color: var(--colors-accent-fg);
}

.bg-c_accent\/10 {
    --mix-backgroundColor: color-mix(in srgb, var(--colors-accent) 10%, transparent);
    background-color: var(--mix-backgroundColor, var(--colors-accent));
}

PHP writes that file, var/ux_css/styles.css, from the templates. There is no Node.js, no binary to download and no watcher. On this site Vite imports it like any other stylesheet, and AssetMapper can serve it as is.

​The design system lives in one YAML file

Tokens are declared in the bundle configuration. Semantic tokens point to other tokens, with one value per condition:

# config/packages/ux_css.yaml (excerpt)
ux_css:
    preflight: true
    default_tokens: panda

    conditions:
        dark: '@media (prefers-color-scheme: dark)'

    semantic_tokens:
        colors:
            fg:
                DEFAULT: { base: '{colors.zinc.700}', _dark: '{colors.zinc.300}' }
                secondary: { base: '{colors.zinc.600}', _dark: '{colors.zinc.300}' }
                muted: { base: '{colors.zinc.500}', _dark: '{colors.zinc.400}' }
            border:
                DEFAULT: { base: '{colors.slate.200}', _dark: '{colors.white/10}' }
            accent:
                DEFAULT: { base: '{colors.primary.500}', _dark: '{colors.primary.400}' }
                fg: { base: '{colors.primary.700}', _dark: '{colors.primary.300}' }

The switch from primary.500 to primary.400 that I used to repeat in every dark: class is now one line. Every token also becomes a CSS variable, like --colors-accent-fg, so hand-written CSS can use the same values.

default_tokens: panda brings Panda's spacing, font sizes, radii and the rest. For colors, I copied the Tailwind v4 palettes into tokens.colors, so the site kept exactly its colors.

After 45 files, I found two differences on the whole site: the border of the gray badge, and the top border of the footer in dark mode. Both are a little lighter.

The configuration is checked too. A semantic token that points to a token that does not exist fails when the container is built, on cache:clear or on the first request.

​"Type-safe", concretely

Type-safe means two things here: your editor completes your tokens, and a typo breaks the page on the next refresh instead of shipping silently.

The hash is checked when Twig compiles the template. A misspelled property, an unknown token or an invalid keyword is a Twig syntax error, with the template, the line and a suggestion. These are the messages lint:twig gives for a few typos, against this site's tokens:

Unknown property "dispaly". Did you mean "display"?
Unknown colors token "accent.emfasis". Did you mean "accent.emphasis"?
Unknown condition "_hovr" in the value of "color". Did you mean "_hover"?
Unknown letterSpacings token "-0.025em". Write a raw value between brackets, like "[-0.025em]".

The check runs wherever Twig compiles templates:

  • in dev, on the next refresh, where the error page points to the line;
  • on cache:clear;
  • in CI, with lint:twig;
  • when deploying, where cache:warmup fails on the first invalid call, so a typo never reaches production.

This is the next refresh after typing accent.emfasis in the 404 page:

Symfony error page in dev: Unknown colors token "accent.emfasis". Did you mean "accent.emphasis"?

The last message of that list comes from a real case. Once the component knew every Panda token category, letterSpacing started taking tokens, and my letterSpacing: '-0.025em' stopped compiling. Panda's tight token has the same value, so the fix was a one-word change, 17 times across 11 templates.

Raw values are still allowed, between brackets: '[-0.025em]'. You just have to mean it.

For completion, the bundle writes config/reference_css.php in debug mode. It holds the PHPDoc type of the hash css() accepts, built from your own tokens, conditions and breakpoints, and you commit it. The Symfony plugin for PhpStorm completes the keys and values of a css() hash in Twig since version 2026.2.314, and Symfony Language Tools since 0.24.0.

It also shows the value of each token, here its color:

PhpStorm completing color tokens in a css() hash, with the color of each token

To be fair about the word "type-safe": string stays in every union of these types. A value that only exists at runtime, like css({ color: status.color }), can only be checked when the template renders, and only in debug mode. The safety net is Twig, not PHPStan.

​Migrating 45 files

The first pass took about 40 minutes, and I did not type it: an AI agent in opencode read the templates, mapped the Tailwind classes to tokens and rewrote the 45 files, while I reviewed. Seven smaller commits followed during the day, between pull requests to the component, and the pull request was merged the same afternoon.

Most of it was mechanical: each class="..." became a css() call, and the templates hold about 170 of them today. A few things needed more thought.

Variants. Components with variants use cva(), like the badge above. It takes the same four arguments as html_cva() from Twig's html-extra (base, variants, compoundVariants and defaultVariants), with style hashes instead of class strings. A defaultVariants key or a compound variant that names an undeclared variant is a Twig syntax error too.

Pseudo-elements. The quote in the footer had its typographic quotes in a hand-written .FooterQuote class. Values can be CSS strings, so the quotes now live in the hash (excerpt):

<blockquote class="{{ css({
    fontStyle: 'italic',
    _before: {
        content: '"β€œ"',
    },
    _after: {
        content: '"”"',
    },
}) }}">

Hand-written CSS still has a place. Posts are Markdown rendered to HTML, and I do not write that HTML, so css() cannot reach it. The prose styles stay in app.css, and they use the same tokens through their CSS variables, var(--colors-border-prose) and friends.

The site was the first real user. Several features of the first release came from migrating it: the preflight option (a CSS reset in its own layer, which replaced the Tailwind reset I had ported by hand), support for every Panda token category, and CSS strings in values. All of them were merged before 3.7.0 was tagged. That is the nice part of dogfooding: the gaps show up on a real site, not months later in an issue.

​Things that bit me

  • Line heights. Tailwind sets a line height with each font size (text-sm is 14px on 20px), Panda does not. I added lineHeights tokens with Tailwind's ratios, and a lineHeight next to each fontSize.
  • Dark mode is a class by default. Panda's preset defines the dark condition as .dark &. This site follows the system preference, so it redefines dark as @media (prefers-color-scheme: dark).
  • The selection turned blue. Panda's reset colors selected text in translucent blue. One line brings back the browser's color: ::selection { background-color: revert; }.
  • Build order. cache:warmup writes var/ux_css/styles.css, and the Vite build imports it. In the Dockerfile, the assets are now built after Composer's post-install-cmd, otherwise Vite has nothing to import.
  • Cascade layers. The generated file declares @layer reset, tokens, utilities. To slot my own base styles and components in between, app.css declares the full order first: @layer reset, base, tokens, components, utilities;.
  • html_cva() with css() inside. I first kept html_cva(), with a css() call in each variant. Since UX CSS 3.8, css() returns an object instead of a string, and html_cva() silently ignores values that are not strings. The badges lost their colors and the callouts their borders, with no error anywhere. Switching to cva() fixed it, and a rendering test now covers the variants.

​What Tailwind still does better

  • Its ecosystem: documentation everywhere and ready-made components, including the Shadcn and Flowbite kits of the Symfony UX Toolkit.
  • Editor support: IntelliSense completes class names and previews their CSS, in any file.
  • Short class names that work outside Twig, in JavaScript for instance.
  • Maturity: UX CSS is still experimental, and may change before its first stable release.

If you want numbers: the benchmark app of the Symfony UX repository renders the same dashboard of 364 components twice. One version uses the Shadcn recipes, with Tailwind classes merged by html_cva() and tailwind_classes. The other uses css() and cva().

In production mode on FrankenPHP, the UX CSS page serves 4.5% more requests per second in classic mode, and 5% more in worker mode. It is a small lead, not a reason to switch.

​Try it on one component

composer require symfony/ux-css

It needs PHP 8.4, Symfony 7.4 or 8.x, and Twig 3.24. Pick one component, like the badge or the button you copy everywhere. Declare three or four semantic tokens, and rewrite it with css().

Then make a typo on purpose and refresh the page. That is the moment I was after.

The documentation has a getting started guide, a comparison with Tailwind, UnoCSS and Bootstrap, and the full list of checks. If something is missing, tell us on symfony/ux.