Skip to content
AliasCSS Logo
v1.5.6
Docs / Installation

Installation

Try AliasCSS with one script tag, then use the CLI to write a static CSS file for production.

Try it in the browser

The quickest way is the playground: write HTML on the left and see the result and the generated CSS on the right. Nothing to install.

To use AliasCSS in your own page without a build step, add the script to any HTML page and start writing classes. It compiles them on the page, including classes added later by JavaScript.

<script defer src="https://cdn.jsdelivr.net/npm/aliascss@latest/dist/aliascss.js"></script>

Keep defer (or put the script at the end of <body>): it watches the page for new elements and needs <body> to exist. It also prints warnings in the console when a class doesn’t compile. They’re hints, not errors.

The browser script is for prototypes and experiments. It’s large and compiles at runtime, so production sites use the CLI.

Install the CLI

npm i -D aliascss
npx aliascss "src/**/*.html" public/css/acss.css

The first argument is a glob of source files, the second is the CSS file to write. Link that file once in your page or import it in your app’s entry. Run npx aliascss --help for every option.

Use a config file

For anything beyond one glob, put the settings in aliascss.config.js and run the CLI with --config. Add --watch while you work:

// aliascss.config.js
export default {
  input: ['src/**/*.{html,jsx,tsx,vue,svelte,astro}'],
  output: { location: 'public/css/acss.css' },
  minify: false,
  // optional: prefix, layers, media, custom, extend, statement, group, ignore
  // extractorFunction: 'acss',
};
npx aliascss --config --watch

Production builds can set minify: true. It merges the compiler’s one-rule-per-declaration output into far fewer rules.

What the CLI can see

The CLI reads your files as text. It finds classes in exactly two places:

  1. class="…" and className="…" string literals.
  2. Calls to the extractor function, String('…') by default.

So a class that lives in a JavaScript expression (a variable, an object, a ternary) has to be wrapped, and class names can never be put together at runtime:

const tone = {
  paid: String('bgc-greenTheme3 c-greenTheme11'),
  due: String('bgc-redTheme3 c-redTheme11'),
};

<li className={String('d-flex g-8px ') + (active ? String('bgc-accentRATheme100') : '')} />

// Never: 'bgc-' + color, `p-${size}px`

Keyframes attributes (keyframes-name="…") and --as- exports are found in the source text too.

Give the wrapper its own name

A linter or formatter with auto-fix can remove String('…') around a string as a no-op, and those classes silently drop out of the CSS. Use a named helper instead and tell the CLI about it:

// lib/acss.ts
export const acss = (classes: string): string => classes;
// aliascss.config.js
export default { /* … */ extractorFunction: 'acss' };
<li className={acss('d-flex g-8px ') + (active ? acss('bgc-accentRATheme100') : '')} />

Next

Set up your framework on the Frameworks page, or read how classes work.