Recursive AI

Evaluate CSS media queries in C#

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

A media query tests conditions such as viewport width. Supply the width yourself to check a breakpoint without a browser.

Terminal
dotnet add package RecursiveAI.OneHtml

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

C#
using RecursiveAI.OneHtml.Css;
 
var query = MediaQueryList.Parse(
    "(400px < width <= 800px)");
var media = new MediaFeatures();
Console.WriteLine(query.Matches(
    media with { Width = 390 }));
Console.WriteLine(query.Matches(
    media with { Width = 800 }));

Unzip the example, then dotnet run.

The snippet prints the matches for 390 and 800 CSS pixels, in that order:

Text
False
True

The full program

The download labels each result with the width it tested.

C#
using RecursiveAI.OneHtml.Css;
 
var query = MediaQueryList.Parse(
    "(400px < width <= 800px)");
foreach (int width in new[] { 390, 800 })
{
    var media = new MediaFeatures
    {
        Width = width
    };
    var match = query.Matches(media);
    Console.WriteLine($"{width}: {match}");
}
Text
390: False
800: True

An invalid query evaluates as not all, so a False result can also mean that the input didn’t parse as a valid query.

Variations

Use min-width and max-width. Replace the query declaration in the snippet at the top. Both ends are inclusive with this spelling; the snippet still prints False and True.

C#
var query = MediaQueryList.Parse(
    "(min-width: 400px) and " +
    "(max-width: 800px)");

Check print media. After the using line, replace the example body with this. It prints True.

C#
var query = MediaQueryList.Parse("print");
Console.WriteLine(query.Matches(
    new MediaFeatures { Type = MediaType.Print }));

The short answer

Pass the text after @media and before the opening brace to MediaQueryList.Parse. Then call Matches for each set of MediaFeatures you want to test. The parsed query is reusable: the example changes the width, not the query string. It doesn’t need an HTML document or read the size of the machine’s screen.

How do you set CSS media query breakpoints?

Use width for the viewport width in CSS pixels. In (400px < width <= 800px), the left comparison excludes exactly 400 and the right includes exactly 800. That makes the query a match at 800 but not at 390 or 400. This is useful when two adjacent ranges meet at the same breakpoint: make one side exclusive if you don’t want both to match at the boundary.

(min-width: 400px) and (max-width: 800px) includes both endpoints, so it is not exactly equivalent to the range above. The Media Queries range syntax lets you see which boundaries are included without translating min- and max- in your head. If you’re checking a boundary for a regression, include the boundary itself in your cases, not just a small and a large width.

How do media queries check more than viewport width?

Width is only one of the inputs. The CSS values reference lists features such as aspect ratio, pointer and hover capabilities, and the preference for reduced motion. For example, the print variation changes MediaFeatures.Type explicitly. A fresh MediaFeatures otherwise represents a 1280 × 720 light-scheme screen. Setting Width alone doesn’t turn that default context into a phone: if the query also checks height, hover, or pointer type, set those values for the case you mean to test.

An unknown feature doesn’t match, even under not: negating an unknown condition still leaves it unknown. That’s different from testing a known condition and getting False. This method only checks the media condition; to see which declaration wins for an element, compute styles using a StyleContext with the same media features. A container query is a different job: onehtml parses @container as a rule but doesn’t evaluate it, because that needs layout.

All onehtml recipes