JavaScript window.matchMedia() Method

You are Here:

JavaScript window.matchMedia() Method

The window.matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string.

Note: The returned MediaQueryList can be used for any of the following purpose

  • To determine if the Document matches the CSS media query.
  • To monitor a document to detect when it matches or stops matching the CSS media query.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Check Screen Type</button> <p id="point"></p> <script> var x = document.getElementById("point"); var size = "(min-width: 450px)"; function myFunction() { if(window.matchMedia(size).matches) x.innerHTML = "Big Screen"; else x.innerHTML = "Small Screen"; } </script> </body> </html>

Syntax

window.matchMedia(mediaQueryString)

Parameter Values

ValueTypeExplanation
ObjectRequiredAn object representing the CSS media query for which to return a new MediaQueryList object.

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author