Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. CSS
  3. Guides
  4. Media queries
  5. Testing

Testing media queries programmatically

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

TheDOM provides features that can test the results of amedia query programmatically, via theMediaQueryList interface and its methods and properties. Once you've created aMediaQueryList object, you can check the result of thequery or receive notifications when the result changes.

Creating a media query list

Before you can evaluate the results of a media query, you need to create theMediaQueryList object representing the query. To do this, use thewindow.matchMedia method.

For example, to set up a query list that determines if the device is in landscape or portraitorientation:

js
const mediaQueryList = window.matchMedia("(orientation: portrait)");

Checking the result of a query

Once you've created your media query list, you can check the result of the query by looking at the value of itsmatches property:

js
if (mediaQueryList.matches) {  /* The viewport is currently in portrait orientation */} else {  /* The viewport is not currently in portrait orientation, therefore landscape */}

Receiving query notifications

If you need to be aware of changes to the evaluated result of the query on an ongoing basis, it's more efficient to register alistener than to poll the query's result. To do this, call theaddEventListener() method on theMediaQueryList object, with a callback function to invoke when the media query status changes (e.g., the media query test goes fromtrue tofalse):

js
// Create the query list.const mediaQueryList = window.matchMedia("(orientation: portrait)");// Define a callback function for the event listener.function handleOrientationChange(mql) {  // …}// Run the orientation change handler once.handleOrientationChange(mediaQueryList);// Add the callback function as a listener to the query list.mediaQueryList.addEventListener("change", handleOrientationChange);

This code creates the orientation-testing media query list and adds an event listener to it. After defining the listener, we also call the listener directly. This makes our listener perform adjustments based on the current device orientation; otherwise, our code might assume the device is in portrait mode at startup, even if it's actually in landscape mode.

ThehandleOrientationChange() function would look at the result of the query and handle whatever we need to do on an orientation change:

js
function handleOrientationChange(evt) {  if (evt.matches) {    /* The viewport is currently in portrait orientation */  } else {    /* The viewport is currently in landscape orientation */  }}

Above, we define the parameter asevt — an event object of typeMediaQueryListEvent that also includes themedia andmatches properties, so you can query these features of theMediaQueryList by directly accessing it, or accessing the event object.

Ending query notifications

To stop receiving notifications about changes to the value of your media query, callremoveEventListener() on theMediaQueryList, passing it the name of the previously-defined callback function:

js
mediaQueryList.removeEventListener("change", handleOrientationChange);

Specifications

Specification
CSSOM View Module
# the-mediaquerylist-interface

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp