Recursive AI

Resolve CSS variables and fallbacks in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

A var() fallback supplies a value when a CSS variable is missing or caught in a cycle.

Terminal
dotnet add package RecursiveAI.OneHtml

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

sample.html is in the example zip and shown in full below.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
 
var doc = Document.Parse(
    File.ReadAllText("sample.html"));
var styles = DocumentStyles.Create(doc);
foreach (var id in new[] {
    "inherited", "missing", "cycle"
})
{
    var node = doc.GetElementById(id);
    var style = styles.GetStyle(node);
    var color = style.GetComputedValue(
        "color");
    Console.WriteLine(color);
}

Unzip the example, then dotnet run.

The snippet prints the inherited, missing-variable and circular-reference cases in that order:

Text
rgb(0, 0, 128)
rgb(0, 128, 128)
rgb(255, 165, 0)

The sample.html file in the download contains:

HTML
<style>
:root { --ink: navy }
#missing { color: var(--absent, teal) }
#inherited { color: var(--ink, teal) }
#cycle {
  --ink: var(--loop);
  --loop: var(--ink);
  color: var(--ink, orange);
}
</style>
<p id=inherited>Inherited</p>
<p id=missing>Missing</p>
<p id=cycle>Cycle</p>

The full program

The download uses the same three elements and labels each output line with its element ID.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
 
var doc = Document.Parse(File.ReadAllText("sample.html"));
var styles = DocumentStyles.Create(doc);
foreach (var id in new[] { "inherited", "missing", "cycle" })
{
    var node = doc.GetElementById(id);
    var style = styles.GetStyle(node);
    Console.WriteLine($"{id}: {style.GetComputedValue("color")}");
}

dotnet run prints:

Text
inherited: rgb(0, 0, 128)
missing: rgb(0, 128, 128)
cycle: rgb(255, 165, 0)

The child with id=cycle defines its own --ink, so it does not inherit the root’s navy value. Its two variables refer back to each other, and color uses orange instead.

Variations

Try a second variable before the default. Replace sample.html with this and use only "missing" in the ID list of the full program:

HTML
<p id=missing style="--theme: purple;
  color: var(--absent, var(--theme, teal))">Nested fallback</p>
Text
missing: rgb(128, 0, 128)

See what happens when the variable has the wrong type. Keep only "missing" in the ID list and use this input instead:

HTML
<div style="color: navy">
  <p id=missing style="--ink: 12px;
    color: green; color: var(--ink, teal)">Wrong value type</p>
</div>
Text
missing: rgb(0, 0, 128)

The short answer

Parse the HTML, create DocumentStyles, then read GetComputedValue("color") on the element’s style. onehtml resolves var() as part of computing the style. Use TryGetCustomProperty when you want the resolved variable text itself, and GetComputedValue when you want what the consuming property actually uses.

How does var() work in CSS?

CSS custom properties are declarations with names beginning --. They are case-sensitive and, for ordinary unregistered properties, the resolved values inherit. For instance, if a parent sets --accent: var(--ink) and its own --ink resolves to navy, a child inherits navy as --accent. The inherited value is not a formula that gets rerun with the child’s --ink. This recipe covers unregistered properties: onehtml does not apply @property registrations in this release.

Resolve variables on the element that uses them, not just on :root. A local declaration can override an inherited variable. If two local properties refer to each other, both are invalid on that element. The CSS value docs also note that references inside fallback arguments count when finding cycles, even if the fallback would otherwise go unused. The fallback on the consuming color declaration can still supply a value once the cycle’s variables are invalid.

How do I use a fallback variable in CSS?

In var(--absent, var(--theme, teal)), the second argument to the outer var() is another var() expression. If --absent has no usable value, the engine tries --theme. Only if that too has no usable value does it use teal. That is why the first variation prints purple.

Why isn’t my CSS variable fallback being applied?

A fallback is not a type check on the consuming declaration. In the second variation, --ink: 12px exists and substitutes successfully. It just isn’t a color. The browser rule that onehtml follows for this typed property is to treat the winning declaration as unset when its value becomes invalid after substitution. color is inherited, so the result is the parent’s navy, not fallback teal and not the earlier green declaration. For a non-inherited property, unset uses the initial value instead.

All onehtml recipes