DOM Window.matchMedia() Method
The matchMedia() method checks a CSS media query from JavaScript, such as screen width or dark mode, and can notify you when the result changes.
Description
The matchMedia() method evaluates a CSS media query and returns a MediaQueryList. Its matches property tells you whether the query currently applies, and its change event fires when that changes. It's more efficient than checking innerWidth on every resize, and works with any media feature, including prefers-color-scheme and prefers-reduced-motion.
Queries can combine conditions, as in '(min-width: 768px) and (orientation: landscape)', just like in CSS. Keeping your breakpoints in CSS custom properties or a shared constants file helps keep JavaScript and CSS in agreement.
Media features about user preferences are especially useful: reduce or skip animations when prefers-reduced-motion: reduce matches, and switch themes in response to prefers-color-scheme. Listen for change so the page updates if the user changes a setting while it's open.
Syntax
matchMedia(mediaQuery)Parameters
- mediaQueryA CSS media query string, such as
"(max-width: 600px)".
Return Value
A MediaQueryList with a matches property and a change event.
Example
Browser Support
The following information will show you the current browser support for the Window.matchMedia() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





