api-member

Window.matchmedia — CSS reference

matchMedia returns a MediaQueryList representing a query. Read its matches property for current state and use a change listener when the match status changes. It reports the same environment as a CSS media query, without directly styling an element.

Formal Syntax & Structure

css syntaxcss
window.matchMedia("(width >= 600px)")

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>Window.matchMedia</title><style>body{margin:0}#a{padding:8px;background:rgb(232,237,241);color:rgb(0,0,0);box-sizing:border-box}
</style><body><main id="a">Resize the viewport to explore this example.</main><script>const mq=matchMedia("(width >= 600px)");const update=()=>a.textContent=mq.matches?"Wide":"Narrow";update();mq.addEventListener("change",update);</script></body></html>
CSS Reference