---
title: "Resolve CSS variables and fallbacks in C#"
url: https://recursiveai.org/onehtml/cookbook/resolve-css-custom-properties-with-fallback/
description: "Resolve CSS custom properties with onehtml in C#. See computed colors for an inherited variable, a missing variable, and a circular reference."
version: "1.6.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/resolve-css-custom-properties-with-fallback/resolve-css-custom-properties-with-fallback.zip)

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

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

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

```csharp
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](https://recursiveai.org/onehtml/cookbook/resolve-css-custom-properties-with-fallback/resolve-css-custom-properties-with-fallback.zip), 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.

```csharp
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)
```

## Related recipes

- [Get computed CSS styles in C#](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)
- [Parse and evaluate CSS colors in C#](https://recursiveai.org/onehtml/cookbook/parse-and-evaluate-css-colors/)

## 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](https://www.w3.org/TR/css-variables-1/#defining-variables) 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](https://recursiveai.org/onehtml/docs/css-values/#var) 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](https://www.w3.org/TR/css-variables-1/#invalid-variables) 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.
