---
title: "Evaluate CSS @supports in C#"
url: https://recursiveai.org/onehtml/cookbook/check-css-supports-condition/
description: "Evaluate a CSS @supports condition or check one property and value in C#. Results reflect onehtml’s parser, not a browser’s rendering capabilities."
version: "1.6.0"
---

# Evaluate CSS @supports 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/check-css-supports-condition/check-css-supports-condition.zip)

A CSS feature query checks whether a declaration is supported. This tests onehtml’s parser, not a browser or rendering engine.

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

```csharp
using RecursiveAI.OneHtml.Css;

var test = SupportsCondition.Parse(
    "(display: grid)");
Console.WriteLine(test.Evaluate());
var value = CssTree
    .ParseComponentValueList("not-a-color");
Console.WriteLine(DeclarationSupport
    .IsSupported("color", value.Values));
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/check-css-supports-condition/check-css-supports-condition.zip), then `dotnet run`.

The snippet prints the condition result, then the result of checking the `color` value:

```text
True
False
```

## The full program

```csharp
using RecursiveAI.OneHtml.Css;

var condition = SupportsCondition.Parse(
    "(display: grid)");
Console.WriteLine(condition.Evaluate());

var value = CssTree.ParseComponentValueList(
    "not-a-color");
var supported = DeclarationSupport
    .IsSupported("color", value.Values);
Console.WriteLine(supported);
```

```text
True
False
```

An invalid condition also evaluates to `False`; check `IsValid` if you need to distinguish it from a valid condition that doesn’t match.

## Variations

**Require two declarations.** After the `using` line, replace the example body with this. It prints `True`.

```csharp
var test = SupportsCondition.Parse(
    "(display: grid) and (color: teal)");
Console.WriteLine(test.Evaluate());
```

**Invert the check.** The parentheses belong around the declaration. This prints `True` because `color: not-a-color` is rejected.

```csharp
var test = SupportsCondition.Parse(
    "not (color: not-a-color)");
Console.WriteLine(test.Evaluate());
```

## Related recipes

- [Evaluate CSS media queries](https://recursiveai.org/onehtml/cookbook/evaluate-css-media-queries/)
- [Validate CSS declarations with custom rules](https://recursiveai.org/onehtml/cookbook/validate-css-declarations-with-custom-rules/)

## The short answer

Use `SupportsCondition.Parse(condition).Evaluate()` for the text between `@supports` and the opening brace. If you already have a property and value separately, use `DeclarationSupport.IsSupported` with the property name and the parsed CSS value list. The first API understands conditions that combine several tests. The second answers a question about one declaration, without asking you to build a parenthesized CSS string.

## Does `@supports` tell you whether CSS will work in a browser?

In a browser, [`@supports`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@supports) is a feature query against that browser. In this program the check is against onehtml. `True` for `(display: grid)` says the parser accepts that declaration; it doesn’t mean the library performs grid layout or that a particular browser will render your grid as intended. Use the C# result to inspect the conditions onehtml evaluates, not as a browser compatibility result.

Support isn’t checked to the same depth for every property. As the [CSS values reference](https://recursiveai.org/onehtml/docs/css-values/#supports) explains, properties such as `color`, `display`, and `opacity` have dedicated value parsers. For some other known properties, only syntactic acceptability is checked. A syntactically acceptable declaration with `var()` can be accepted before the variable has been resolved for any element. In contrast, `color: not-a-color` reaches the typed color parser and is rejected.

## How do you use `not`, `and`, or `or` with CSS `@supports`?

Use `and` when all the conditions have to be true, `or` when any one can be true, and `not` to invert a condition. Wrap each property/value test in parentheses, as in the variations above. When you need to mix operators, group the conditions with parentheses instead of relying on precedence; see [MDN’s syntax examples](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@supports#syntax).

## Can `@supports` check a CSS selector?

`selector(...)` is also a feature test. When you parse a standalone condition, `SupportsOptions.SelectorSupported` lets you supply the check for the selector text. Without that callback, onehtml only checks whether the text looks like one complex selector, with no top-level comma. That isn’t a full selector check, and it doesn’t tell you whether the selector matches any element in a document.
