Search


Search something to see results

CSS-Parser is a high-performance, fault-tolerant, and dependency-free all-in-one CSS parsing solution for Node.js and browsers.

It implements the CSS Syntax Module Level 3 specification and validates CSS using the syntax definitions of the W3C specifications from @webref/css. Every stylesheet is fully parsed into a structured AST, and token values are exposed as typed data so the library can support robust transformations, validation, and plugin-oriented workflows without falling back to raw strings.

In addition to parsing and validation, CSS-Parser provides advanced optimization and minification capabilities. According to this benchmark, it is the most efficient CSS minifier available, producing smaller output than competing solutions while maintaining competitive performance.

Want to see it in action? Try the CSS-Parser playground.

A non-exhaustive list of features is provided below:

  • Zero dependencies — lightweight and easy to integrate into any project.
  • All-in-one CSS parsing solution for parsing, validation, transformation, and minification.
  • Standards-based CSS validation powered by the W3C specification definitions (@webref/css).
  • Full CSS Modules support for modern component-based workflows.
  • Fault-tolerant parsing that follows the CSS Syntax Module Level 3 specification and always yields a complete parse tree.
  • Typed tokens and AST — parsed CSS is exposed as strongly typed tokens and nodes for safer plugin and transform logic.
  • High-performance minification with safe optimizations and no unsafe transforms.
  • Advanced color processing with support for modern color spaces and functions, including color(), lab(), lch(), oklab(), oklch(), color-mix(), light-dark(), system colors, and relative colors.
  • Color conversion engine capable of transforming colors between all supported formats.
  • Automatic CSS nesting generation from compatible selectors.
  • Nested CSS transpilation to legacy-compatible syntax.
  • if() function transpilation for broader browser compatibility.
  • Source map generation for easier debugging and development workflows.
  • Shorthand property computation to reduce output size and improve optimization.
  • Transform function optimization for more compact CSS output.
  • Math function evaluation, including calc(), clamp(), min(), max(), and related expressions.
  • CSS variable inlining where values can be safely resolved.
  • Duplicate declaration removal to eliminate redundant rules.
  • @import flattening to produce self-contained stylesheets.
  • Vendor prefix cleanup to modernize generated CSS.
  • Binary input — the css can be a string, an ArrayBuffer or an ArrayBufferView (utf-8 bytes, Uint8Array, Buffer): an ASCII buffer is tokenized without copy.
  • Experimental: evaluate @mixin and @function
  • 51.5kb once minified and gzipped.

The library is also ported to Rust, in two builds with the same API and the same output as the TypeScript library, faster:

Package Runs in Speed
@tbela99/rust-css-parser-napi Node.js on macOS and Linux up to 2.1x faster than TypeScript
@tbela99/rust-css-parser-wasm Node.js and the browsers up to 1.9x faster than TypeScript

The WebAssembly build is only 2.5 MB (865 KB gzipped), with no native dependency: it runs anywhere WebAssembly runs. The native build is compiled from its Rust sources when it is installed (requires the Rust toolchain).

$ npm install @tbela99/rust-css-parser-wasm
$ npm install @tbela99/rust-css-parser-napi

Switching build only changes the import:

import {transformSync} from '@tbela99/rust-css-parser-wasm';

const result = transformSync('.a { color: #ff0000; width: calc(100px + 20px) }');
console.debug(result.code); // .a{color:red;width:120px}

It is also available as a Rust library on crates.io: rust-css-parser (cargo add rust-css-parser), see Rust crate.

$ npm install @tbela99/css-parser
$ deno add @tbela99/css-parser

It can be imported as an umd module.

<script type="module">

import {transformSync, ColorType} from 'https://unpkg.com/@tbela99/css-parser@2.3.2/web';

const css = `

.table {
border-collapse: collapse;
width: 100%;
}

.table td, .table th {
border: 1px solid #ddd;
padding: 8px;
}

.table tr:nth-child(even){background-color: #f2f2f2;}

.table tr:hover {background-color: #ddd;}

.table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
`;

console.debug(transformSync(css, {beautify: true, convertColor: ColorType.OKLCH}).code);
</script>

It can be imported as a module in the browser.


<script src="https://unpkg.com/@tbela99/css-parser@2.3.2/dist/index-umd-web.js"></script>
<script>

const css = `

.table {
border-collapse: collapse;
width: 100%;
}

.table td, .table th {
border: 1px solid #ddd;
padding: 8px;
}

.table tr:nth-child(even){background-color: #f2f2f2;}

.table tr:hover {background-color: #ddd;}

.table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
`;

console.debug(CSSParser.transformSync(css, {beautify: true, convertColor: CSSParser.ColorType.OKLCH}).code);

</script>

import {transformSync, ColorType} from '@tbela99/css-parser';

const css = `

.ruler {

height: 10px;
background-color: orange
}
.hsl { color: #b3222280; }
`;

const result = transformSync(css, {

beautify: true,
convertColor: ColorType.SRGB
});

console.debug(result.code);

The library exposes two entry points:

  • @tbela99/css-parser or @tbela99/css-parser/node which relies on node fs and fs/promises to read files
  • @tbela99/css-parser/web which relies on the web fetch api to read files

The library is an ES module. The deprecated commonjs entry point (@tbela99/css-parser/cjs) is removed in 2.0.0: use import (or a dynamic import() in a commonjs module, see Migration).

Load as javascript module

<script type="module">

import {transformSync, ColorType} from 'https://unpkg.com/@tbela99/css-parser@2.3.2/web';

const css = `

.ruler {

height: 10px;
background-color: orange
}
.hsl { color: #b3222280; }
`;

const result = transformSync(css, {

beautify: true,
convertColor: ColorType.RGB
});

console.debug(result.code);

</script>

Load as an UMD module


<script src="https://unpkg.com/@tbela99/css-parser@2.3.2/dist/index-umd-web.js"></script>
<script>

const css = `

.table {
border-collapse: collapse;
width: 100%;
}

.table td, .table th {
border: 1px solid #ddd;
padding: 8px;
}

.table tr:nth-child(even){background-color: #f2f2f2;}

.table tr:hover {background-color: #ddd;}

.table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}
`;

console.debug(CSSParser.transformSync(css, {beautify: true, convertColor: CSSParser.ColorType.OKLCH}).code);

</script>

← Index | Native build →