# AliasCSS > AliasCSS is a deterministic compiler that turns class names into native CSS. A class name *is* a CSS declaration, written as `[@scope-][selector-]property-value`. There is no design-token vocabulary to learn: if you know CSS, you can write AliasCSS. It works as a browser script (compiles classes on the page at runtime) or as a CLI/build step (writes a .css file). It ships optional theme-aware color tokens and an `x-` component library as examples of building your own design system. Website: https://aliascss.com Package: `aliascss` on npm This file describes version 1.5.6. Every example was compiled with that release. ## Install Browser script, **for development and experiments only** (it is large and compiles at runtime; ship the CLI output in production): ```html ``` Keep `defer` (or put the script at the end of ``): the runtime watches the page for new elements and needs `` to exist. Classes added later by JavaScript are compiled automatically. Debug warnings are on in this build; they are hints, not errors. CLI / build (production): ```bash npm i -D aliascss npx aliascss "src/**/*.html" public/css/acss.css # input glob, output file npx aliascss --config --watch # uses aliascss.config.js (or .mjs) npx aliascss --help ``` ```js // aliascss.config.js export default { input: ['src/**/*.{html,jsx,tsx,vue}'], output: { location: 'public/css/acss.css' }, minify: false, // prefix, layers, media, custom, extend, statement, group, ignore … are optional // extractorFunction: 'acss', // name of the wrapper the CLI scans in JS/TS (default String) }; ``` ### What the CLI can see The CLI reads source files as text. It finds classes in exactly two places: 1. `class="…"` / `className="…"` string literals 2. calls to the extractor function, `String('…')` by default So every class that lives in a JS expression (variable, object, ternary, template literal) must be wrapped, and class names must never be assembled at runtime: ```tsx const tone = { paid: String('bgc-greenTheme3 c-greenTheme11'), due: String('bgc-redTheme3 c-redTheme11') };
  • // never: 'bgc-' + color, `p-${n}px` ``` Keyframes attributes (`keyframes-name="…"`) and `--as-` exports are read from the source text too. **Naming the wrapper.** In a project that runs a linter or formatter with auto-fix, `String('…')` around a string can be removed as a no-op, and those classes silently disappear from the CSS. A named helper avoids that: ```ts export const acss = (classes: string): string => classes; // returns its argument unchanged // aliascss.config.js: extractorFunction: 'acss'
  • ``` ## Core rule: a class is one CSS declaration ``` property-value display-flex → display: flex alias-value d-flex → display: flex @scope-property-value @md-d-flex → @media (min-width: 768px) { … } --state-property-value --hover-c-red → :hover { color: red } @scope--state-property-value @md--hover-c-red ``` - The property-value pair is always last; everything before it is scope and selector. - Full CSS property names always work (`background-color-red`, `justify-content-space-between`). Short aliases exist for common ones. When unsure, use the full name. - Values are written as in CSS with `-` between tokens: `b-1px-solid-red` → `border: 1px solid red`, `p-8px-16px` → `padding: 8px 16px`. ### Common aliases | Alias | Property | Example | |---|---|---| | `d` | display | `d-flex`, `d-grid`, `d-none`, `d-inline-flex` | | `p`, `m` | padding, margin | `p-16px`, `m-0`, `p-8px-16px` | | `w`, `h` | width, height | `w-100%`, `h-40px` | | `mw`, `xw` | min-width, max-width | `mw-0px`, `xw-680px` | | `xh` | max-height | `xh-240px` | | `g` | gap | `g-12px` | | `aic`, `jcc`, `jcsb` | align-items:center, justify-content:center / space-between | | | `c`, `bgc` | color, background-color | `c-red`, `bgc-slateTheme1` | | `fs`, `fw`, `lh`, `ff` | font-size, font-weight, line-height, font-family | `fs-14px`, `fw-600`, `lh-1.5` | | `les` | letter-spacing | `les--0.02em` | | `b`, `br` | border, border-radius | `b-1px-solid-red`, `b-0`, `br-8px` | | `t`, `r`, `btm`, `l` | top, right, bottom, left | `t-0` | | `gtc`, `gc`, `gr` | grid-template-columns, grid-column, grid-row | `gtc(repeat(3,1fr))`, `gc-1/-1`, `gc-span-2` | | `o`, `zi` | opacity, z-index | `o-0.5`, `zi-10` | | `tf` | transform | `tf-scale-0.98`, `tf-translateY--2px` | | `td` | text-decoration | `td-none` | | `cont` | `content: ""` (for ::before/::after) | `--before-cont` | Less common properties: use the full name (`overflow-wrap-break-word`, `scroll-margin-top-24px`). ## Values | Need | Write | Output | |---|---|---| | Negative number | `--` before it | `m--8px` → `margin: -8px`, `tf-translateY--2px` | | Decimal | `.` with a leading zero | `o-0.5`, `lh-1.5`, `m-0.25rem` (`.5` also compiles to `0.5`, but write `0.5`) | | Percent | `%` | `w-100%` | | Color with alpha | `/` + decimal 0–1 | `c-red/0.5`, `bgc-ccc/0.7` → `oklch(from … / 0.7)` | | Hex color | with or without `#` | `c-e3e3e3` and `c-#e3e3e3` are the same | | `!important` | `--imp` (or `--important`) at the end | `c-red--imp`, `@md-m-0--imp` | | Complex value | property function `prop(value)` | see below | ### Dashed names (anchors, timelines) In `anchor-name`, `position-anchor`, `anchor-scope`, `animation-timeline`, `scroll-timeline`, `view-timeline` (and their `-name` forms), `timeline-scope`, `position-try` and `font-palette`, AliasCSS adds the `--` and `--x` is a name, not `var()`: ``` anchor-name-menu / anchor-name--menu anchor-name: --menu anchor-name-menu__card anchor-name: --menu, --card position-anchor-auto position-anchor: auto scroll-timeline-hero_y scroll-timeline: --hero y position-try-fallbacks(--below,flip-block) position-try-fallbacks: --below, flip-block anchor-name--var--x anchor-name: var(--x) (an actual variable) ``` ### Property functions: `property(raw value)` Use when the value has spaces, commas or functions: ``` padding(8px_16px) → padding: 8px 16px grid-template-columns(repeat(3,1fr)) → grid-template-columns: repeat(3,1fr) box-shadow(0_1px_3px_rgb(0_0_0_/_0.1)) → box-shadow: 0 1px 3px rgb(0 0 0 / 0.1) w(calc(100%_-_32px)) → width: calc(100% - 32px) ``` - `_` means a space. `\_` is a literal underscore. Commas stay commas. - Inside `calc()`, `+` and `-` need spaces: write `calc(100%_-_32px)`, never `calc(100%-32px)` (invalid CSS). `*` and `/` don't need them: `calc(var(--x)*-1)`. - Hex inside a function keeps its `#`: `c(#e3e3e3)`. ## CSS variables ``` --x-gap:16px defines --x-gap: 16px --x-pad:(8px_16px) defines --x-pad: 8px 16px (raw value in parentheses) --x-border-color:--x-accent:blue defines --x-border-color: var(--x-accent, blue) g--x-gap uses gap: var(--x-gap) g--x-gap:16px uses gap: var(--x-gap, 16px) (with fallback) m--x-shift:--8px uses margin: var(--x-shift, -8px) (negative fallback: always write --8px) bgc--x-bg:slateTheme1 uses background-color: var(--x-bg, ) ``` Variable names must not start with a digit. The end of a variable name decides how its value is compiled: names ending in `color` or `bg` compile colors (`slateTheme1` → `light-dark(…)`), names ending in `ring` compile a focus ring. Pick neutral names (`--x-gap`, `--x-border-color`) when you want the value as written. **Start every custom variable with `--x-`.** Short selector aliases own the start of a class: `--a-` (:active), `--h-` (:hover), `--f-` (:focus), `--l-`, `--m-` (::marker), `--n-` (:not), `--s-` (::selection), `--sb-` (::-webkit-scrollbar), `--ui-` (:user-invalid), `--v-`, `--w-` (:where) and about 60 more. `--sb-bg:slateDark1` is read as "scrollbar + `bg:slateDark1`" and compiles nothing; `--x-sidebar-bg:slateDark1` works. To use a variable as a value write `w--x-width` (or `w--x-width:200px` with a fallback). `w-var(--x-width)`, `w-var(--x-width,200px)` and `w(var(--x-width))` work too. ## Selectors and states ``` --hover-c-red :hover --focus-visible-… --active-… --disabled-… --before-cont ::before --after-… --placeholder-… --marker-… --first-child-… --last-child-… --checked-… --open-… --popover-open-… --nth-child(2n+1)-… :nth-child(2n+1) short: --nc(3)-… , --nth-child-3-… --not(--first-child)-… :not(:first-child) (parentheses for anything longer than one word) --not--hover-… :not(:hover) (one-word form) --has(--checked)-… :has(:checked) --has(nav)-… :has( nav) a bare word inside ( ) is an element; --has(_nav) is the same, --has(__nav) is :has(> nav) --is(…) --where(…) :is(…) :where(…) Capitalised (--Has, --Is, --Where) adds a space: " :has(…)" --hover--before-c-red :hover::before (state first, then pseudo-element) ``` Never put a state after `::before`/`::after` (`--before--hover` → `::before:hover`, which never matches). Vendor parts (each browser only applies its own prefix): `--webkit-slider-thumb`, `--webkit-slider-runnable-track`, `--moz-range-thumb`, `--moz-range-track`, `--moz-range-progress`, `--webkit-progress-bar`, `--webkit-progress-value`, `--moz-progress-bar`, `--scrollbar`, `--scrollbar-thumb`, `--file-selector-button`, `--details-content`. ### Attribute selectors ``` [aria-expanded=true]-c-red .x[aria-expanded="true"] { color: red } [aria-current=page][c-red,fw-600] attribute first, then a group [data-open]-d-block attribute present [class~=active]-bgc-blue ``` Attribute values are written **without quotes**: quotes are never allowed in class names (`[data-score=2]`, not `[data-score='2']`). ### Combinators (target other elements) | Write | Selector | |---|---| | `_li-c-red` | descendant `li` | | `__li-c-red` | child `> li` | | `___p-c-red` | next sibling `+ p` | | `____p-c-red` | later siblings `~ p` | | `_all-m-0` | descendants `*` | | `__all-m-0` | children `> *` | | `_.card-c-red` | descendant `.card` | | `_.(card-title)-c-red` | descendant `.card-title`: **class names containing `-` go in parentheses** | | `_.(card.is-active)-o-1` | `.card.is-active` | | `_ul__li--hover-c-red` | `ul > li:hover` | ### Selector before the element: `&` Everything before `&` becomes an ancestor selector: ``` _.dark&c-white .dark . { color: white } [data-theme=dark]&c-white [data-theme="dark"] . { … } [data-open]&[o-1,tf-none] [data-open] . { opacity:1; transform:none } [data-collapsed=true]&--hover_.(tip)-o-1 [data-collapsed="true"] .:hover .tip { opacity:1 } [data-score=2]&[data-on]-bgc-amberTheme9 [data-score="2"] .[data-on] { … } ``` This is the main tool for component state: put one `data-*` / `aria-*` attribute on the root and let every child read it with `[attr=value]&…`. JavaScript (or React) only changes the attribute. ## Scopes (`@…-`) | Scope | Meaning | |---|---| | `@xs-` | max-width: 575.99px | | `@sm-` | min-width: 576px | | `@md-` | min-width: 768px | | `@lg-` | min-width: 992px | | `@xl-` | min-width: 1200px | | `@xxl-` | min-width: 1408px (`xxl`, not `2xl`) | | `@hover-capable-` | `(hover: hover)` | | `@motion-reduce-` | `prefers-reduced-motion: reduce` | | `@container-xs-` … `@container-xxl-` | container queries | | `@reset- @base- @theme- @components- @utils-` | CSS cascade layers (`@rs`, `@comps`, `@utilities` also work) | | `@starting-style-` / `@ss-` | `@starting-style` (entry animations) | | `@[components,starting-style]-…` | several scopes at once | Layer order: `@layer reset, base, theme, components, utilities;` then the breakpoint layers. ### Responsive rules (important) 1. **Breakpoint classes live in layers, so a plain class always beats them.** Never mix `p-32px @xs-p-16px`; the plain one wins everywhere. Write every size with a prefix: `@xs-p-16px @sm-p-24px @md-p-32px`. 2. **`@xs` is max-width, the others are min-width.** If you start with `@xs`, also set `@sm`, or 576–767px gets nothing: `@xs-x-col-12 @sm-x-col-12 @lg-x-col-8`. 3. Larger breakpoints win over smaller ones (`@md` over `@sm`). 4. **A default for every width that a breakpoint can still change goes in the components layer**, which sits below the breakpoint layers: `@components[d-none] @lg-d-block` is hidden below 992px and a block from there; `@components[p-16px] @lg-p-32px` is 16px everywhere and 32px from `@lg`. A plain class can't do this (rule 1), and `@xs` stops at 575px. ## Groups `[…]` Apply one prefix to several declarations: ``` @md[d-flex,g-16px,p-24px] --hover[c-red,bgc-blue] @components--before[cont,position-absolute,inset-0] [d-block,--hover-bgc-gray100,[aria-current=true]-fw-600] states and attributes are allowed inside ``` A group styles **one element in all its states**. Element nesting (`[_th[…]]`) is not supported: use statements for that. Only atomic property-value classes go inside `[…]`. `x-` names inside a group are read as the `x` property, not as components, and `@scope-` items are not allowed. ## Export a component class: `--as-name` ```html ``` - `--as-[btn,button]` exports several names; `/` is allowed (`--as-card/header`). - Only atomic property-value classes can be exported: no `x-` statements and no `@scope-` items (`@motion-reduce-…`, `@md-…`) inside the brackets. Put scoped classes on the element itself, next to the exported name. - States, attributes and ancestor selectors do work inside: `[…,--hover-bgc-slateTheme3,[aria-current=page]-c-accentRATheme1100,[data-collapsed=true]&--hover_.(nav-tip)-o-1]--as-nav-item`. - Use exports for any class list repeated 3+ times (nav links, rows, chips, tooltips). The hidden element can live anywhere in the source; in React render it as ``. - Pick export names that don't start with a property alias followed by a value: `c3-card` compiles as `color: 3-card`. Prefix them by component (`nav-item`, `deck-card`). ## Other HTML attributes ```html
    ``` `class_card` works like `class-card`. `class:card` (colon) is only read with `useColon: true` in the config. In JSX, use an export instead of a scoped group: `[` isn't allowed in JSX attribute names. ## Keyframes ```html
    …
    ``` - One stop: `@0-[…]`, `@0[…]` or `@50%[…]` (the dash and `%` are optional). - One class needs no brackets: `@25-tf-translateY-3px`, `@25dn`, `@[25]-dn`. Two or more classes go in `[…]`. - Stops are numbers: `from`/`to` are rejected (write `0` and `100`). - Several stops with the same style: `@[0,25,50][…]`. There is no `@0:50` form. - Inside the brackets: comma-separated AliasCSS classes; commas inside `( )` are left alone (`@0-[transform(translate(0,0)_scale(1)),o-1]`). - **Names are lower-case kebab-case.** HTML lower-cases attribute names, so `keyframes-fadeIn` becomes `fadein` in the browser. - Put a class on the keyframes element (`class="d-none"`): the dev runtime's first pass only visits elements with a class. The CLI doesn't care. - Use them with `animation(name_.3s_ease-out_both)`, `animation-name-name`, and `animation-delay(calc(var(--i)_*_40ms))` for staggers. Restart a one-shot animation by toggling an attribute (`[data-play]&animation(…)`) or, in React, by changing the element's `key`. ## Colors and themes - Theme-aware tokens (switch with light/dark): Radix scales `slateTheme1`…`slateTheme12` (also red, green, amber, blue … `Theme1-12`), fixed dark steps `slateDark1-12`, alpha `blackA1-12`. - Accent scale (React Aria style), driven by one variable `--accentColor`: `accentRA100`…`accentRA1600` (fixed), `accentRATheme100`…`accentRATheme1600` (light/dark). - Theme tokens compile to `light-dark()`, which needs `color-scheme`. Put `x-theme` on ``. - Rebrand everything: ``. Force a subtree: `x-theme-dark` / `x-theme-light`. - Known issue: inside a forced subtree on a page of the other scheme, `slateTheme*` tokens switch correctly but `accentRATheme*` tokens keep the page's scheme (Chrome, relative colors inside `light-dark()`). Prefer slate tokens in subtrees you force. - Brand gradients: define two color variables once (`--x-from-color:accentRA900 --x-to-color:pinkTheme9`) and use `background(linear-gradient(135deg,var(--x-from-color),var(--x-to-color)))`. ## x- components (optional starter library) All are statements in the `@components` layer, use theme tokens, and can be customised with `--x-*` variables. Utilities override them. - Forms: `x-button` (+ `-secondary -ghost -danger -sm -lg -icon`), `x-input`, `x-textarea`, `x-select`, `x-checkbox`, `x-radio`, `x-switch`, `x-range`, `x-file`, `x-progress`, `x-field`, `x-label`, `x-hint`, `x-error-text`, `x-choice`, `x-input-group`, `x-input-addon` - Surfaces: `x-card` (+ `-header -body -footer -title -description -elevated -flat -interactive`), `x-badge`, `x-alert`, `x-table` (+ `-striped -bordered -hover -compact -sticky`), `x-table-card` - Overlays: `x-dialog` (native ``), `x-dropdown` (native `popover`), `x-accordion` (`
    `) - Navigation: `x-tab-list`, `x-tab`, `x-tab-panel`, `x-breadcrumb`, `x-pagination`, `x-menu` (hamburger → X, uses `aria-expanded`) - Feedback: `x-spinner`, `x-skeleton`, `x-avatar`, `x-avatar-group` - Layout: `x-stack`, `x-cluster`, `x-center`, `x-grid`, `x-switcher`, `x-sidebar`, `x-cover`, `x-frame`, `x-reel` - 12-column grid: `x-row` (gap 16px; `x-row-xs/sm/md/lg/xl` = 4/8/16/24/32px; `x-row-20px` any gap), `x-col-1`…`x-col-12`, `x-col` (fill), `x-col-auto` (fit content), `x-col-240px` (fixed), `x-col-offset-1`…`11`. Uses flex + gap, no negative margins, so a card can be the column itself. - Slide-in panels: `x-absolute-top/-bottom/-left/-right` (hide with `data-absolute-hide` or class `x-absolute-hide`), `x-absolute-0` (fill parent) - Content: `x-prose`, `x-kbd`, `x-code`, `x-code-block`, `x-divider`, `x-divider-vertical`, `x-divider-text` (+ `-start -end`), `x-arrow` (+ `-up -down -left -right`) - Theme: `x-theme`, `x-theme-light`, `x-theme-dark` Example: ```html
    …
    ``` ## Build your own design system The `x-` library is an example, not a fixed API. Define your own in the config: ```js export default { extend: { 'ui-btn': { type: 'statement', statement: ` @base-all-unset @components[d-inline-flex,aic,g-8px,p-8px-16px,br-8px,bgc--ui-btn-bg:accentRA1000,c-white,cursor-pointer] @components--hover-bgc--ui-btn-hover:accentRA1100 @components--focus-visible[outline-2px-solid-currentColor,outline-offset-2px] ` }, 'ui-reset': { type: 'raw', statement: `*{box-sizing:border-box}` }, }, }; ``` - `type: 'statement'`: a class made of AliasCSS classes (multi-line, scopes allowed). - `type: 'raw'`: plain CSS output as is. - `type: 'group'`: a function compiler that takes a value (`x-col-6`, `x-row-24px`). - Convention: custom function compilers are capitalised (`Theme(…)`), custom classes get a prefix (`x-`, `ui-`). ## Console messages The browser build prints warnings in the console (debug mode). Typical ones: ``` AliasCSS: can't compile "-bad-thing" (not a valid class: nothing is output) AliasCSS: "border-bottom-0": couldn't compile the value "0", used it as written (the CSS is still correct) ``` Values are not validated against CSS: `bgc-notacolor` outputs `background-color:notacolor` and the browser ignores it. Many mistakes compile without any warning (`calc(100%-32px)`, `--before--hover-…`), so check the generated CSS, and the table below, when something doesn't look right. ## Common mistakes | Wrong | Right | Why | |---|---|---| | `p-32px @xs-p-16px` | `@xs-p-16px @sm-p-32px` | plain classes beat breakpoint classes | | `@xs-x-col-12 @lg-x-col-6` | `@xs-x-col-12 @sm-x-col-12 @lg-x-col-6` | 576–991px would get nothing | | `@2xl-…` | `@xxl-…` | there is no 2xl | | `w(calc(100%-32px))` | `w(calc(100%_-_32px))` | calc needs spaces around + and - | | `c-red/50` | `c-red/0.5` | alpha is a decimal 0–1 | | `--before--hover-c-red` | `--hover--before-c-red` | state before pseudo-element | | `_.card-title-c-red` | `_.(card-title)-c-red` | hyphenated class names need parentheses | | `--not--first-child-…` | `--not(--first-child)-…` | multi-word selectors need parentheses | | `m--x-gap:-8px` | `m--x-gap:--8px` | negative values (fallbacks too) use `--` | | `