interface

Mediaquerylistevent — CSS reference

A MediaQueryListEvent includes the media query and its updated matches state. It is delivered to listeners when the query crosses from matching to nonmatching or vice versa. The fixture listens for a real resize; a separate test drives a viewport transition.

Formal Syntax & Structure

css syntaxcss
list.addEventListener("change", event => event.matches);

Working Example

Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>MediaQueryListEvent</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 list=matchMedia("(min-width:600px)");window.lastMatch=list.matches;window.events=[];a.textContent=String(lastMatch);list.addEventListener("change",e=>{events.push({matches:e.matches,media:e.media});lastMatch=e.matches;a.textContent=String(e.matches)});</script></body></html>
CSS Reference