A CSS feature query checks whether a declaration is supported. This tests onehtml’s parser, not a browser or rendering engine.
dotnet add package RecursiveAI.OneHtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
The snippet prints the condition result, then the result of checking the color value:
True False
The full program
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);
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.
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.
var test = SupportsCondition.Parse( "not (color: not-a-color)"); Console.WriteLine(test.Evaluate());
Related recipes
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 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 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.
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.