CSS Variable Fallback Chain Resolution

Resolve caller-declared bounded single-var-reference/literal fallback graph with cycle detection, not arbitrary CSS token parsing, registered-property types or property-specific invalid-value fallback. Explicit supplied computed/layout context only; not a browser cascade/layout engine or cross-browser guarantee.

Description

A custom-property reference can resolve through other variables and a missing-reference fallback.

A custom-property reference can resolve through other variables and a missing-reference fallback.1

A simplified reference graph differs from a complete stylesheet cascade and CSS token parser. Use CSS Variable Fallback Chain Resolution to evaluate the supplied inputs under the specific model below.

When to use CSS Variable Fallback Chain Resolution

Use CSS Variable Fallback Chain Resolution to evaluate the supplied inputs under the specific model below. Define the supplied layout, token or rendering scenario and establish the relevant units and browser context independently.

How to use CSS Variable Fallback Chain Resolution and understand its results

  1. Define the supplied layout, token or rendering scenario and establish the relevant units and browser context independently.
  2. Enter Variable names, Literal tokens, Referenced variables, Fallback tokens, Query variable using the field table and validation rules below, then run the tool.
  3. Read Resolved token, Visited variables and interpret units, signs, ordering and placeholders as explained below.
  4. If the tool reports an input error, correct that field and rerun. A successful false result, empty match list or unknown-evidence placeholder has the specific meaning described here; check the limitations before applying the result.
Input fieldMeaningValue typeRequirement
variableNamesVariable namesarray of stringRequired; constraints below
literalTokensLiteral tokensarray of stringRequired; constraints below
referencedVariablesReferenced variablesarray of stringRequired; constraints below
fallbackTokensFallback tokensarray of stringRequired; constraints below
queryVariableQuery variablestringRequired; constraints below

Enter unique custom-property names matching -- followed by ASCII letters, digits, underscores or hyphens, with aligned literal, reference and fallback strings and an exact query name. Follow its single-reference chain, recording visited variables. A visited node cannot contain both a literal and a reference; terminals need a nonempty literal. Missing referenced names require a nonempty fallback on the referring node. Revisited names are a cycle error even when a fallback exists. The queried name must exist; unvisited nodes are not traversed. This bounded graph is not a CSS token parser or complete computed-value cascade.

resolve(name) = literal, recursive referenced value, or local fallback for a missing reference; revisiting a name fails

Field order, units and branch conditions follow the implementation explanation above.

Output fieldMeaningValue type
resolvedTokenResolved tokenstring
visitedVariablesVisited variablesarray of string
json
{
  "input": {
    "variableNames": [
      "--a",
      "--b"
    ],
    "literalTokens": [
      "",
      "red"
    ],
    "referencedVariables": [
      "--b",
      ""
    ],
    "fallbackTokens": [
      "blue",
      ""
    ],
    "queryVariable": "--a"
  },
  "output": {
    "resolvedToken": "red",
    "visitedVariables": [
      "--a",
      "--b"
    ]
  }
}

Limitations and assumptions

CSS variables can be invalid because of dependency cycles. This implementation represents one reference per node; real computed-value resolution requires the complete stylesheet context.2

A simplified reference graph differs from a complete stylesheet cascade and CSS token parser. Apply the exact scope and assumptions described above.

All listed fields require explicit values. Unexpected fields, incorrect types, nonfinite numbers and sparse arrays fail validation. Domain-specific bounds, alignments and supported tokens are stated above; extremely large inputs or outputs can also be rejected. These errors differ from successful results such as false, no matches or unknown evidence.

Alternative or Complementary methods

Use inherit/initial/unset/revert Resolution Table for a related task: CSS-wide keywords choose parent, initial or rollback values under defined rules.

Use @layer revert-layer Cascade Order Lookup for a related task: Cascade layers have ordering rules that differ for normal and important declarations.

References

  1. CSS Variable Fallback Chain Resolution — tool.io

  2. CSS Custom Properties for Cascading Variables — W3C

Similar or alternative tools

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies