Favorites

DOM Window.matchMedia() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Window.matchMedia()
DOM Window MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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

const narrow = matchMedia('(max-width: 600px)');
console.log('narrow:', narrow.matches);
narrow.addEventListener('change', e => console.log('now narrow?', e.matches));

console.log('dark mode:', matchMedia('(prefers-color-scheme: dark)').matches);
console.log('reduce motion:', matchMedia('(prefers-reduced-motion: reduce)').matches);

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee