css-extras

CSS Extras Function Reference

Complete reference for all CSS custom functions in css-extras.

Total functions: 49


--negate() ↗︎

Negates a value (returns the negative).

Parameters

Returns

Number: The negated value.

Example

padding: --negate(1em);

--abs() ↗︎

Returns the absolute value of a number.

Parameters

Returns

Number: The absolute value.

Example

margin: --abs(-20px);

--lerp() ↗︎

Linear interpolation between two values.

Parameters

Returns

Number: Interpolated value.

Example

width: --lerp(100px, 200px, 0.5);

--map-range() ↗︎

Maps a value from one range to another.

Parameters

Returns

Number: Mapped value.

Example

font-size: --map-range(50vw, 320px, 1920px, 14px, 24px);

--ratio() ↗︎

Returns the ratio of two values. Supports values with different units, unlike regular division.

Parameters

Returns

Number: The ratio between two values.

Example

scale: --ratio(16px, 1em);

--sign() ↗︎

Returns the sign of a number (-1, 0, or 1).

Parameters

Returns

Number: -1 for negative, 1 for positive, 0 for zero.

Example

transform: scaleX(--sign(var(--direction)));

--round-to() ↗︎

Rounds a value to the nearest multiple.

Parameters

Returns

Length: Rounded value.

Example

width: --round-to(127px, 8px);

--floor-to() ↗︎

Rounds a value down to the nearest multiple (floor).

Parameters

Returns

Length: Rounded down value.

Example

width: --floor-to(127px, 8px);

--ceil-to() ↗︎

Rounds a value up to the nearest multiple (ceil).

Parameters

Returns

Length: Rounded up value.

Example

width: --ceil-to(127px, 8px);

--hypot() ↗︎

Calculates the hypotenuse using Pythagorean theorem.

Parameters

Returns

Length: Hypotenuse length.

Example

diagonal: --hypot(100px, 100px);

--opacity() ↗︎

Returns a semi-transparent version of any color.

Parameters

Returns

Color: Color with opacity.

Example

background: --opacity(blue, 50%);

--tint() ↗︎

Lightens a color by mixing with white. Uses OKLab color space for perceptually uniform mixing.

Parameters

Returns

Color: Lightened color.

Example

background: --tint(blue, 20%);

--shade() ↗︎

Darkens a color by mixing with black. Uses OKLab color space for perceptually uniform mixing.

Parameters

Returns

Color: Darkened color.

Example

background: --shade(blue, 20%);

--saturate() ↗︎

Adjusts color saturation. Uses OKLCH color space for perceptually uniform chroma adjustment. Chroma is clamped to 0.4 for safe display.

Parameters

Returns

Color: Adjusted color.

Example

color: --saturate(red, 1.5);

--lighten() ↗︎

Adjusts color lightness. Uses OKLCH color space for perceptually uniform lightness adjustment. Maintains chroma independently.

Parameters

Returns

Color: Adjusted color.

Example

background: --lighten(blue, 20%);

--darken() ↗︎

Darkens a color by reducing lightness. Uses OKLCH color space for perceptually uniform lightness adjustment. Unlike --shade() which mixes with black, this directly reduces the lightness value.

Parameters

Returns

Color: Darkened color.

Example

background: --darken(blue, 20%);

--rotate-hue() ↗︎

Rotates the hue of a color. Uses OKLCH color space for perceptually uniform hue rotation.

Parameters

Returns

Color: Color with rotated hue.

Example

background: --rotate-hue(blue, 180deg);

--complement() ↗︎

Returns the complementary color. Uses OKLCH color space for perceptually accurate complementary colors.

Parameters

Returns

Color: Complementary color.

Example

border-color: --complement(blue);

--invert() ↗︎

Inverts a color.

Parameters

Returns

Color: Inverted color.

Example

background: --invert(white);

--grayscale() ↗︎

Converts a color to grayscale. Uses OKLCH color space by setting chroma to 0.

Parameters

Returns

Color: Grayscale color.

Example

filter: --grayscale(var(--brand-color));

--text-on() ↗︎

Returns black or white text color for optimal contrast on a background.

Parameters

Returns

Color: Black or white for optimal readability.

Example

color: --text-on(var(--bg-color));

--opaque() ↗︎

Removes transparency from a color, making it fully opaque.

Parameters

Returns

Color: Fully opaque version of the color.

Example

background: --opaque(var(--semi-transparent-bg));

--mix() ↗︎

Mixes two colors in OKLab color space. Uses perceptually uniform OKLab color space for natural-looking color mixing.

Parameters

Returns

Color: Mixed color.

Example

background: --mix(red, blue, 30%);

--triadic() ↗︎

Returns a triadic color harmony. Triadic colors are evenly spaced around the color wheel (120° apart).

Parameters

Returns

Color: Triadic color.

Example

color: --triadic(blue, 1);

--tetradic() ↗︎

Returns a tetradic (square) color harmony. Tetradic colors are evenly spaced around the color wheel (90° apart).

Parameters

Returns

Color: Tetradic color.

Example

color: --tetradic(blue, 2);

--black() ↗︎

Creates a semi-transparent black.

Parameters

Returns

Color: Semi-transparent black.

Example

box-shadow: 0 2px 4px --black(20%);

--white() ↗︎

Creates a semi-transparent white.

Parameters

Returns

Color: Semi-transparent white.

Example

background: --white(90%);

--fluid-type() ↗︎

Creates fluid typography that scales with viewport. NOTE: This function is mathematically equivalent to --responsive-value() but optimized for typography. Use this for font-size, --responsive-value() for other properties.

Parameters

Returns

Length: Fluid font size.

Example

font-size: --fluid-type(16px, 24px, 320px, 1280px);

--modular-scale() ↗︎

Creates a modular scale value.

Parameters

Returns

Length: Scaled value.

Example

font-size: --modular-scale(1rem, 1.25, 3);

--line-height-length() ↗︎

Calculates line height as a length value based on font size. Returns a length (e.g., 24px) rather than a unitless ratio. Use this when you need an absolute line height value.

Parameters

Returns

Length: Line height as a length.

Example

line-height: --line-height-length(16px, 1.6);

--line-height-ratio() ↗︎

Calculates line height as a unitless ratio. Returns a number (e.g., 1.5) which is recommended for better inheritance in CSS.

Parameters

Returns

Number: Unitless line height ratio.

Example

line-height: --line-height-ratio(24px, 16px);

--line-height-unitless() ↗︎

Creates unitless line height from font size (recommended for better inheritance). NOTE: Only works correctly with pixel font sizes. For rem/em values, use --line-height-length() or --line-height-ratio() instead.

Parameters

Returns

Number: Unitless line height.

Example

line-height: --line-height-unitless(16px, 1.5);

--sidebar-layout() ↗︎

Creates responsive sidebar layout columns.

Parameters

Returns

Length: Grid template columns value.

Example

grid-template-columns: --sidebar-layout(250px, 20ch);

--conditional-radius() ↗︎

Conditional border radius that removes at viewport edges.

Parameters

Returns

Length: Computed border radius.

Example

border-radius: --conditional-radius(1rem, 8px);

--responsive-value() ↗︎

Creates a responsive value that scales between two sizes. NOTE: This function is mathematically equivalent to --fluid-type() but uses a simpler lerp-based approach. Use this for spacing/sizing, --fluid-type() for typography.

Parameters

Returns

Length: Responsive value.

Example

padding: --responsive-value(1rem, 2rem, 320px, 1200px);

--aspect-height() ↗︎

Calculates height from aspect ratio and maximum constraints.

Parameters

Returns

Length: Computed height.

Example

height: --aspect-height(16/9, 100vw, 100vh);

--aspect-width() ↗︎

Calculates width from aspect ratio and maximum constraints.

Parameters

Returns

Length: Computed width.

Example

width: --aspect-width(16/9, 100vh, 100vw);

--spacing() ↗︎

Creates consistent spacing based on a scale. Recommended range: 0-10. Higher values create exponentially larger spacing.

Parameters

Returns

Length: Computed spacing.

Example

margin: --spacing(3);

--container-padding() ↗︎

Creates inset spacing for containers.

Parameters

Returns

Length: Responsive padding.

Example

padding: --container-padding(2rem, 1200px);

--ease-out() ↗︎

Creates a simple easing curve value.

Parameters

Returns

Number: Eased value.

Example

transform: translateY(--ease-out(var(--progress)));

--elastic-ease() ↗︎

Creates elastic easing.

Parameters

Returns

Number: Eased value.

Example

transform: scale(--elastic-ease(var(--progress), 1.2));

--px-to-rem() ↗︎

Converts pixels to rem.

Parameters

Returns

Length: Rem value.

Example

font-size: --px-to-rem(24px);

--rem-to-px() ↗︎

Converts rem to pixels.

Parameters

Returns

Length: Pixel value.

Example

width: --rem-to-px(2rem);

--auto-grid() ↗︎

Creates responsive grid columns.

Parameters

Returns

Grid: Grid template columns value.

Example

grid-template-columns: --auto-grid(250px, 4);

--grid-span() ↗︎

Creates a CSS grid span value. Ensures the span is an integer value.

Parameters

Returns

Span: Grid column span (rounded to integer).

Example

grid-column: --grid-span(3, 12);

--smooth-shadow() ↗︎

Creates a smooth shadow. Generates three shadow layers. The spread-factor controls how distributed the shadows are.

Parameters

Returns

Shadow: Layered box shadow.

Example

box-shadow: --smooth-shadow(black, 20px, 3);

--glow() ↗︎

Creates a glow effect.

Parameters

Returns

Shadow: Glow shadow.

Example

box-shadow: --glow(cyan, 10px, 0.5);

--light-dark() ↗︎

Theme-aware value switcher for light/dark mode. Uses CSS if() with color-scheme query. Requires color-scheme: light dark on :root. Works with ANY value type (colors, lengths, etc.), not just colors.

[!NOTE] CSS has a native light-dark() function for colors. The custom --light-dark() function is more powerful as it works with any value type, not just colors.

Parameters

Returns

Any: Theme-appropriate value.

Example

padding: --light-dark(0.75rem, 1rem);

--theme-color() ↗︎

Creates a theme-aware color with automatic adjustment. Uses CSS if() with color-scheme query. Requires color-scheme: light dark on :root. In light mode, mixes the base color with white (default 85% white). In dark mode, mixes the base color with black (default 15% black).

Parameters

Returns

Color: Theme-adjusted color.

Example

background: --theme-color(blue, 80%, 20%);