Symfony UX CSS: design tokens, atomic CSS and responsive styles in Twig. Use for hover states, dark mode, breakpoints, css(), cva(), Panda CSS syntax, semantic_tokens, preflight, static_css and the generated var/ux_css/styles.css.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Ux Css?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/smnandre-ux-css)
---
name: ux-css
description: 'Symfony UX CSS: design tokens, atomic CSS and responsive styles in Twig. Use for hover states, dark mode, breakpoints, css(), cva(), Panda CSS syntax, semantic_tokens, preflight, static_css and the generated var/ux_css/styles.css.'
license: MIT
metadata:
author: Simon André
url: https://smnandre.dev
---
# UX CSS
A `css()` Twig function inspired by Panda CSS: pass a hash of styles, get atomic class names built from your design tokens. The bundle writes the matching rules to one file, `var/ux_css/styles.css`. Styles are validated when Twig compiles the template, so a typo is a Twig error with the template line, also caught by `lint:twig` and `cache:warmup`. PHP writes the CSS, with zero Node.js tooling.
Since Symfony UX 3.7+. The package is **experimental**: its API may still change.
```twig
<div class="{{ css({ p: 'md', color: 'fg', _hover: { color: 'primary' }, md: { p: 'lg' } }) }}">
{# class="p_md c_fg hover:c_primary md:p_lg" #}
```
## Installation
```bash
composer require symfony/ux-css
```
Flex registers `UXCssBundle`, creates `config/packages/ux_css.yaml` (with `default_tokens: panda`), and adds the import to your stylesheet:
```css
/* assets/styles/app.css */
@layer reset, base, tokens, components, utilities;
@import '../../var/ux_css/styles.css';
/* your own styles */
```
Works with AssetMapper (the bundle registers `var/ux_css/` under the `ux_css` prefix), Reprise and Encore. In debug mode the bundle also writes `config/reference_css.php` (editor types): commit it.
Requires PHP 8.4+, Symfony 7.4 or 8.x, TwigBundle, Twig 3.24+.
## Tokens
```yaml
# config/packages/ux_css.yaml
ux_css:
default_tokens: panda # Panda palette, spacing 0-96, sizes, radii, fonts...
tokens:
colors:
brand: '#4f46e5'
bg: { DEFAULT: '#ffffff', muted: '#f9fafb' } # "bg" and "bg.muted"
spacing: { sm: '0.5rem', md: '1rem', lg: '2rem' }
sizes: { prose: '65ch' }
fonts:
body: ['Inter', 'sans-serif']
semantic_tokens:
colors:
primary: '{colors.blue.500}'
fg: { base: '{colors.gray.900}', _dark: '{colors.gray.50}' }
```
Your tokens add to (and override, path by path) the default ones. Every token becomes a CSS variable (`--colors-primary`, `--spacing-md`) usable in hand-written CSS. Each property takes one fixed category (from Panda): `color`/`bg`/`borderColor` take `colors`, `p`/`m`/`gap`/`inset` take `spacing`, `w`/`h`/`maxW` take `sizes`, `rounded` takes `radii`, `shadow` takes `shadows`, `fontSize` takes `fontSizes`... The full list is in [references/reference.md](references/reference.md).
## Values
```twig
{{ css({ color: 'primary' }) }} {# token #}
{{ css({ display: 'flex', zIndex: 10 }) }} {# keyword, number #}
{{ css({ w: '[37ch]', color: '[#ff6600]' }) }} {# raw value: square brackets #}
{{ css({ bg: 'primary/50' }) }} {# 50% opacity with color-mix() #}
{{ css({ mt: '-md' }) }} {# negative spacing #}
{{ css({ gap: 'var(--card-gap)' }) }} {# your own variable #}
{{ css({ color: 'primary!' }) }} {# !important #}
{{ css({ border: '1px solid token(colors.blue.500, gray)' }) }}
{{ css({ _before: { content: '""' } }) }} {# CSS strings must be complete #}
```
Shorthands: `p px py pt pr pb pl`, `m mx my mt mr mb ml`, `w h minW maxW minH maxH`, `bg bgColor`, `rounded`, `shadow`.
## Conditions and Breakpoints
```twig
{# Condition: underscore prefix, a hash or a conditional value #}
{{ css({ color: 'fg', _hover: { color: 'primary' } }) }}
{{ css({ color: { base: 'fg', _hover: 'primary' } }) }}
{# Nested: hover:dark:bg_gray.900 #}
{{ css({ _hover: { _dark: { bg: 'gray.900' } } }) }}
{# Breakpoints, mobile-first: sm 640px, md 768px, lg 1024px, xl 1280px, 2xl 1536px #}
{{ css({ p: { base: 'sm', md: 'lg' } }) }}
{{ css({ p: ['sm', 'md', 'lg'] }) }} {# base, sm, md #}
{{ css({ display: { base: 'none', mdOnly: 'block' } }) }}
{# Selectors and at-rules #}
{{ css({ '& > li': { mt: 'sm' }, '@media print': { display: 'none' } }) }}
```
Common conditions: `_hover`, `_focus`, `_focusVisible`, `_active`, `_disabled`, `_checked`, `_invalid`, `_expanded`, `_open`, `_placeholder`, `_before`, `_after`, `_first`, `_last`, `_even`, `_odd`, `_dark` (`.dark &`), `_light`, `_osDark`, `_motionReduce`, `_print`, `_groupHover`, `_peerFocus`. Ranges per breakpoint: `mdOnly`, `mdDown`, `smToLg`.
Dark mode follows `.dark` on an ancestor by default; change it once:
```yaml
ux_css:
conditions:
dark: '[data-theme=dark] &'
sidebarOpen: 'body.sidebar-open &' # use as _sidebarOpen
```
## Runtime Values
Values written in the template (including both branches of a ternary, or a `{% set %}` constant above the call) are resolved at compile time and free at render. Values from variables are resolved at render (microseconds), but **their CSS only exists if the same style appears in a template or in `static_css`**:
```yaml
ux_css:
static_css:
css:
- properties:
color: [success, warning, danger]
conditions: [hover]
- properties:
padding: ['*']
responsive: true
```
In debug mode, a runtime class without CSS logs a warning on the `ux_css` channel (profiler Logs panel). Map user input to a known list before passing it to `css()`.
## Cascade Layers
The file writes `@layer tokens, utilities;` (`reset` first with `preflight`). Unlayered CSS beats layered CSS, so a Bootstrap or app rule outside any layer overrides a `css()` class. Put your own CSS in `base` / `components` layers declared before the import, as above.
## Production
```bash
APP_ENV=prod php bin/console cache:warmup # writes the compact CSS, fails on any invalid css()
php bin/console asset-map:compile # or: npm run build (after warmup)
```
In a multi-stage Docker build, copy `var/ux_css/styles.css` from the PHP stage before the Node build. For stylesheet imports and build order by asset pipeline, see [bundlers and deployment](references/reference.md#bundlers-and-deployment).
## Variants with cva()
Since Symfony UX 3.8+, `cva()` describes a component's variants with style hashes, like Panda CSS `cva()`:
```twig
{# templates/components/Button.html.twig #}
{% props size = 'sm', visual = 'solid' %}
{% set button = cva(
base: { px: '3', py: '1', rounded: 'md' },
variants: {
size: { sm: { fontSize: 'sm' }, lg: { fontSize: 'lg', px: '5' } },
visual: { solid: { bg: 'primary', color: 'fg' }, outline: { borderWidth: '1px' } },
},
compoundVariants: [{ size: 'lg', visual: 'outline', css: { borderWidth: '2px' } }],
defaultVariants: { size: 'sm', visual: 'solid' },
) %}
<button {{ attributes.defaults({ class: button.apply({ size: size, visual: visual }) }) }}>
{% block content %}{% endblock %}
</button>
```
The config must be readable at compile time; the chosen variants can come from runtime props. Hashes merge before generating classes, so the last value of each property wins (`lg` gives `px_5` alone). A caller can override per property with `<twig:Button class="{{ css({ px: 'md' }) }}">` (requires `symfony/ux-twig-component` 3.8+).
## css() Returns a Class List
Since Symfony UX 3.8+, `css()` returns a `Symfony\UX\Css\CssClassList`, printed as a string. Append an empty string wherever a plain string is required:
```twig
{{ html_classes(css({ p: 'md' }) ~ '', { active: isActive }) }}
{% if (css({ p: 'md' }) ~ '') starts with 'p_' %}...{% endif %}
```
`html_classes()` refuses the list, the `variants` of `html_cva()` silently drop it, and `in` / `starts with` tests fail on it: use `cva()` for variants, and `~ ''` for the rest. A sandboxed template must allow `CssClassList::__toString()`.
## Key Principles
**Tokens are the vocabulary.** `strict_tokens` (default on) refuses non-token values; use `[raw]` deliberately, or add a token.
**Write the styles in the template.** Compile-time styles are free and always have CSS; runtime values need `static_css`.
**One hash per call.** `css(a, b)` merging is not supported; use a spread `{ ...base, p: 'md' }`.
**Not supported yet**: recipes from the configuration, slot recipes (`sva()`), patterns (`stack`, `grid`), text/layer/animation styles, themes, keyframes, global CSS, custom utilities. For variants, write them with `cva()`.
## References
- [Symfony UX package page](https://ux.symfony.com/css) ([Markdown summary](https://ux.symfony.com/css.md)) · [`symfony/ux-css` on Packagist](https://packagist.org/packages/symfony/ux-css).
- Try the [interactive design tokens and styles demo](https://ux.symfony.com/css#demo).
- For design tokens, writing styles and deployment, see the [Symfony UX CSS documentation](https://symfony.com/bundles/ux-css).
- **Reference** (property categories, configuration, validation messages, editors, Panda differences): [references/reference.md](references/reference.md)
## See Also
- **twig-component**: call `css()` inside component templates; combine with `attributes.defaults({class: css({...})})`.
- **live-component**: every template is scanned, so classes in components rendered later already have CSS.