CSSStyle is a Node.js implementation of the CSS Object Model CSSStyleDeclaration class that provides comprehensive support for CSS property parsing, validation, and manipulation. It extends the CSSOM specification with modern CSS features, enabling developers to programmatically handle CSS styles in Node.js environments without browser dependencies.
npm install cssstyleconst { CSSStyleDeclaration } = require("cssstyle");For ES modules:
import { CSSStyleDeclaration } from "cssstyle";const { CSSStyleDeclaration } = require("cssstyle");
// Create a new style declaration
const style = new CSSStyleDeclaration();
// Set individual properties
style.setProperty("color", "red");
style.setProperty("font-size", "16px", "important");
// Access properties
console.log(style.color); // "red"
console.log(style.getPropertyValue("color")); // "red"
console.log(style.getPropertyPriority("font-size")); // "important"
// Work with CSS text
style.cssText = "background: blue; border: 1px solid black;";
console.log(style.cssText); // "background: blue; border: 1px solid black;"
// Enumerate properties
for (let i = 0; i < style.length; i++) {
const property = style.item(i);
console.log(`${property}: ${style.getPropertyValue(property)}`);
}CSSStyle is built around several key components:
margin → margin-top, margin-right, etc.)Core CSSStyleDeclaration class implementing the W3C CSS Object Model interface. Provides property management, CSS text serialization, and change tracking.
class CSSStyleDeclaration {
constructor(onChangeCallback?: Function, opt?: { context?: object });
// CSS text representation
get cssText(): string;
set cssText(value: string);
// Property count
get length(): number;
// Parent rule reference (readonly)
get parentRule(): CSSRule | null;
// CSS float property alias
get cssFloat(): string;
set cssFloat(value: string);
}Methods for getting, setting, and removing CSS properties with priority support and validation.
// Core property methods
getPropertyValue(property: string): string;
setProperty(property: string, value: string, priority?: string): void;
removeProperty(property: string): string;
getPropertyPriority(property: string): string;
item(index: number): string;Comprehensive CSS value parsing and validation utilities supporting all CSS data types, functions, and modern features.
// Core parsing functions
function prepareValue(value: any, globalObject?: object): string;
function parseColor(val: string): string | undefined;
function parseLength(val: string, restrictToPositive?: boolean): string | undefined;
function parseUrl(val: string): string | undefined;
function isValidPropertyValue(prop: string, val: string): boolean;Helper functions for CSS property name transformation, string manipulation, and property descriptor creation.
// Property name transformation
function dashedToCamelCase(dashed: string): string;
function camelCaseToDashed(camelCase: string): string;
// String utilities
function asciiLowercase(s: string): string;
function splitOnASCIIWhitespace(str: string): string[];// Constructor options
interface CSSStyleDeclarationOptions {
context?: Window | Element | CSSRule;
}
// Change callback function type
type ChangeCallback = (cssText: string) => void;
// CSS property priority values
type PropertyPriority = "" | "important";
// Numeric type constants for parsing
const NUM_TYPE = {
UNDEFINED: 0,
VAR: 1,
NUMBER: 2,
PERCENT: 4,
LENGTH: 8,
ANGLE: 0x10,
CALC: 0x20
};CSSStyle throws specific errors for invalid operations:
undefined for invalid CSS values rather than throwing errors