A CSS value parser / serializer
npm install @seanchas116/cssvalue@seanchas116/cssvalue

A TypeScript library for parsing and serializing CSS property values with strong typing support.
šØ Try the Interactive Demo ā
- Parse CSS values into strongly-typed JavaScript objects
- Serialize back to CSS with toString() methods
- Full TypeScript support with comprehensive type definitions
- CSS spec compliant parsing following official CSS specifications
- Bidirectional transformation - parse and serialize without data loss
``bash`
npm install @seanchas116/cssvalueor
yarn add @seanchas116/cssvalueor
pnpm add @seanchas116/cssvalue
- Background - Full support for complex background values including layers, gradients, and images
- Border - Parse border shorthand and individual border properties
- Box Shadow - Support for multiple shadows with blur, spread, and inset
- Colors - HEX, RGB, RGBA, HSL, HSLA, OKLCH, and CSS Color Module Level 5 color spaces
- Dimensions - Lengths, percentages, and calc() expressions
- Font Family - Parse font family lists with proper quoting
- Gradients - Linear and radial gradients with color stops
- Position - CSS position values with keywords and offsets
- URLs - Parse url() functions with proper escaping
`ts
import {
cssParser,
Background,
BackgroundLayer,
URL,
Position,
Dimension,
HexColor,
} from "@seanchas116/cssvalue";
// Parse a complex background value
const background = cssParser.background.tryParse(
'center / contain no-repeat url("foo.svg"), #eee 35% url("bar.png")',
);
// Work with strongly-typed objects
const expected = new Background({
layers: [
new BackgroundLayer({
position: new Position("center", "center"),
size: "contain",
repeatStyle: ["no-repeat"],
image: new URL("foo.svg"),
}),
new BackgroundLayer({
position: new Position(
{ from: "left", offset: new Dimension(35, "%") },
"center",
),
image: new URL("bar.png"),
}),
],
color: new HexColor("#eee"),
});
// Serialize back to CSS
background.toString(); // => 'center / contain no-repeat url("foo.svg"), #eee 35% url("bar.png")'
`
`ts
import {
cssParser,
RGBColor,
HSLColor,
OKLCHColor,
} from "@seanchas116/cssvalue";
// Parse different color formats
const rgb = cssParser.color.tryParse("rgb(255 128 0 / 0.5)");
const hsl = cssParser.color.tryParse("hsl(210deg 50% 40%)");
const oklch = cssParser.color.tryParse("oklch(0.7 0.2 150)");
// Create colors programmatically
const color = new RGBColor({ r: 1, g: 0.5, b: 0, a: 0.5 });
color.toString(); // => "rgba(255,128,0,0.5)"
`
`ts
import { cssParser, LinearGradient, HexColor } from "@seanchas116/cssvalue";
const gradient = cssParser.gradient.tryParse(
"linear-gradient(45deg, red 0%, blue 50%, green 100%)",
);
// Modify gradient stops
gradient.stops[0].color = new HexColor("#ff0000");
gradient.toString(); // => "linear-gradient(45deg, #ff0000 0%, blue 50%, green 100%)"
`
`ts
import {
cssParser,
BoxShadow,
Dimension,
RGBColor,
} from "@seanchas116/cssvalue";
const shadows = cssParser.boxShadow.tryParse(
"0 4px 6px rgba(0, 0, 0, 0.1), inset 0 2px 4px #00000020",
);
// Create shadows programmatically
const shadow = new BoxShadow({
offsetX: new Dimension(0, "px"),
offsetY: new Dimension(4, "px"),
blurRadius: new Dimension(6, "px"),
color: new RGBColor({ r: 0, g: 0, b: 0, a: 0.1 }),
});
`
All parsers are available through the cssParser object:
- cssParser.background.tryParse(value: string) - Parse CSS backgroundcssParser.border.tryParse(value: string)
- - Parse CSS bordercssParser.boxShadow.tryParse(value: string)
- - Parse CSS box-shadowcssParser.color.tryParse(value: string)
- - Parse CSS colorcssParser.dimension.tryParse(value: string)
- - Parse CSS dimensioncssParser.gradient.tryParse(value: string)
- - Parse CSS gradientcssParser.fontFamily.tryParse(value: string)
- - Parse CSS font-familycssParser.position.tryParse(value: string)
- - Parse CSS position
Each CSS value type is represented by a class with a toString() method:
- Background, BackgroundLayer - Background propertiesBorder
- , BorderStyle - Border propertiesBoxShadow
- - Box shadow effectsColor
- , HexColor, RGBColor, HSLColor, OKLCHColor - Color valuesDimension
- - Length and percentage valuesLinearGradient
- , RadialGradient - Gradient functionsPosition
- - Position valuesURL
- - URL references
This project uses PNPM workspaces and Turbo for monorepo management.
- Node.js 18+
- PNPM 8+
`bashInstall dependencies
pnpm install
$3
Try the live demo at cssvalue.vercel.app or run it locally:
`bash
Start the demo development server
pnpm --filter demo devOpen http://localhost:5173 in your browser
`The demo provides:
- Live parsing of CSS values as you type
- Support for all parser types (background, border, color, gradients, etc.)
- Pretty-printed output showing the parsed JavaScript objects
- Instant feedback on parsing errors
$3
`
.
āāā packages/
ā āāā cssvalue/ # Main CSS value parsing library
ā āāā demo/ # Interactive web demo (React + Vite)
āāā pnpm-workspace.yaml # PNPM workspace configuration
āāā turbo.json # Turbo build configuration
āāā README.md # This file
``Contributions are welcome! Please feel free to submit a Pull Request.
MIT - See LICENSE file for details
Ryohei Ikegami (@seanchas116)