matchMedia
Safe to use with polyfillapimatchMedia provides a JavaScript API to see if a media query will succeed.
- Verdict
- Safe to use
- Category
- JavaScript APIs
- Browser support
- Can I Use
- Works in
- all modern browsers
Should you use matchMedia in 2026?
MatchMedia.js provides a shim for unsupported browsers. If you want a shim that is responsive to window resizing, you can use this fork, though it's limited to browsers that support CSS Transitions.
How to use it
const mq = matchMedia('(prefers-color-scheme: dark)');
apply(mq.matches);
mq.addEventListener('change', e => apply(e.matches));
Please