Resolve custom properties

Decide whether CSS variables should remain browser-owned or resolve from authoritative server context.

Preserve var() when the browser owns cascade and inheritance. Resolve it in PHP only when the server has an authoritative variable map for the output it is producing, such as an email, PDF, preview, or generated token file.

Choose where the variable is authoritative

Situation Recommended result
Browser stylesheet and runtime theme Preserve the var() expression
Server-rendered email or PDF Resolve from an explicit server map
Static token export Resolve known values and reject missing required keys
Partial preview context Resolve known values and preserve unresolved expressions

Create a variable with CssColor::var() or parse a var() expression:

use PhpColor\Color\Css\CssColor;
use PhpColor\Color\Css\CssContext;

$expression = CssColor::var('--brand', '#2563eb');
$color = CssColor::resolve($expression, new CssContext());
use PhpColor\Color\Css\CssColor; use PhpColor\Color\Css\CssContext; $expression = CssColor::var('--brand', '#2563eb'); $color = CssColor::resolve($expression, new CssContext());

Resolve fallback chains explicitly

The result is the fallback #2563eb. Nested fallbacks are resolved from the inside only when earlier values are absent:

$expression = CssColor::parse(
    'var(--brand, var(--fallback, #2563eb))',
);

$context = new CssContext([
    '--fallback' => '#1d4ed8',
]);

$color = CssColor::resolve($expression, $context);
$expression = CssColor::parse( 'var(--brand, var(--fallback, #2563eb))', ); $context = new CssContext([ '--fallback' => '#1d4ed8', ]); $color = CssColor::resolve($expression, $context);

This context produces #1d4ed8 from --fallback.

Decide whether missing context is an error

Without a value or fallback, non-strict resolution returns the ColorVar. Strict resolution throws PhpColor\Color\Exception\InvalidColorException:

$context = new CssContext(
    variables: [],
    colorScheme: null,
    strict: true,
);

$color = CssColor::resolve(
    CssColor::parse('var(--missing)'),
    $context,
);
$context = new CssContext( variables: [], colorScheme: null, strict: true, ); $color = CssColor::resolve( CssColor::parse('var(--missing)'), $context, );

Strictness is an application-boundary decision. Use strict mode when unresolved output would be a defect; use non-strict mode when the browser or a later pass still owns the missing value.

PHPColor resolves the variables you provide. It does not implement selector matching, cascade, inheritance, or computed styles.

Continue with Build resolution contexts, Resolve contextual CSS colors, or CssContext.