# css-mediaquery
[](#contributors-)
[](https://crates.io/crates/css-mediaquery)
[](https://docs.rs/css-mediaquery)
[](https://github.com/trananhtung/css-mediaquery/actions/workflows/ci.yml)
[](#license)
**Parse CSS media queries and match them against a context** — a headless
`matchMedia` for servers and tooling. A faithful Rust port of the
[`css-mediaquery`](https://www.npmjs.com/package/css-mediaquery) npm package
(used by `react-responsive`, styled-components SSR, and friends). Zero dependencies.
```rust
use css_mediaquery::{matches, Context};
let ctx = Context::new().media_type("screen").width(640.0);
assert!(matches("screen and (min-width: 600px)", &ctx));
assert!(!matches("screen and (min-width: 800px)", &ctx));
// units convert just like css-mediaquery: 30em = 480px, 2dppx = 192dpi
assert!(matches("(min-width: 30em)", &Context::new().width(481.0)));
assert!(matches("(min-resolution: 2dppx)", &Context::new().resolution(192.0)));
```
You can also inspect the parsed AST:
```rust
let q = css_mediaquery::parse("screen and (min-width: 600px)");
assert_eq!(q[0].media_type, "screen");
assert_eq!(q[0].expressions[0].feature, "width");
```
## Why css-mediaquery?
Server-side rendering, static analysis, and headless tooling all need to evaluate
`@media` queries without a browser. Rust had full CSS parsers but no small,
focused "does this query match this context?" — so this ports the canonical JS one,
verified against it across thousands of realistic (query, context) pairs.
```toml
[dependencies]
css-mediaquery = "0.1"
```
## API
| `parse(query)` | Parse into `Vec<Query>` (one per comma) |
| `matches(query, &context)` | Whether the query matches the context |
| `Query::matches(&context)` | Match a single parsed query |
| `Context::new()…` | Builder: `media_type`, `width`, `height`, `resolution`, `orientation`, `aspect_ratio`, `color`, … |
## Behavior
This mirrors css-mediaquery's semantics, including its quirks:
- Lengths convert to px (`em` = `rem` = `16px`, `in` = `96px`, `cm` = `96/2.54px`, …);
resolutions to dpi (`dppx` = `×96`, `dpcm` = `/2.54`); ratios to decimals.
- A feature matches **only when the context supplies a non-zero value** for it.
Orientation and aspect-ratio are *not* derived from width/height — set them
explicitly.
- The media type is matched, and `not` quits early when the type matches (so
`not all and (…)` is always false), mirroring the reference implementation.
- `,` is OR; within a query, all expressions must hold (AND).
On well-formed queries it agrees with css-mediaquery exactly. It intentionally
differs only on input that never appears in real stylesheets: numeric values use
Rust parsing rather than JavaScript's `Number`/`parseInt` coercions (no `Infinity`,
hexadecimal, or integers beyond `i64`), and malformed parentheses degrade gracefully
instead of throwing.
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/trananhtung"><img src="https://avatars.githubusercontent.com/u/30992229?v=4?s=100" width="100px;" alt="Tung Tran"/><br /><sub><b>Tung Tran</b></sub></a><br /><a href="https://github.com/trananhtung/css-mediaquery/commits?author=trananhtung" title="Code">💻</a> <a href="#maintenance-trananhtung" title="Maintenance">🚧</a></td>
</tr>
</tbody>
</table>
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.