matchMedia

A browser function that checks whether the page currently matches a CSS media query, such as a dark colour scheme or a narrow screen, and reports changes.

In more detail

It returns an object whose matches property is the current answer and which fires a change event. It lets scripts follow the same breakpoints as the CSS.

Example

In React, subscribe to it with useSyncExternalStore rather than copying it into state.

Related terms

Taught in

References