---
title: "Evaluate CSS media queries in C#"
url: https://recursiveai.org/onehtml/cookbook/evaluate-css-media-queries/
description: "Evaluate a CSS media query in C# without opening a browser. Parse a viewport-width range once and check whether it matches two widths you supply."
version: "1.6.0"
---

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

[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/evaluate-css-media-queries/evaluate-css-media-queries.zip)

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

```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 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](https://recursiveai.org/onehtml/cookbook/evaluate-css-media-queries/evaluate-css-media-queries.zip), 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.

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

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

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

## Related recipes

- [Check a CSS `@supports` condition](https://recursiveai.org/onehtml/cookbook/check-css-supports-condition/)
- [Compute an element’s CSS styles](https://recursiveai.org/onehtml/cookbook/computed-style-csharp/)

## 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](https://www.w3.org/TR/mediaqueries-4/#mq-ranges) 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](https://recursiveai.org/onehtml/docs/css-values/#media-queries) 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.
