Fast and lightweight polyfill for min/max-width CSS3 Media Queries for IE 6-8 and more
—
The matchMedia polyfill provides window.matchMedia functionality for browsers that don't support it natively. This enables testing CSS media queries programmatically in JavaScript, essential for responsive JavaScript behavior.
Creates MediaQueryList objects for testing media queries in JavaScript.
/**
* Test a CSS media query in JavaScript
* Creates a temporary DOM element to test if the media query matches
* @param query - CSS media query string to test
* @returns MediaQueryList object with match result and query string
*/
declare function matchMedia(query: string): MediaQueryList;
interface MediaQueryList {
/** Whether the media query currently matches the viewport */
matches: boolean;
/** The original media query string that was tested */
media: string;
}Usage Examples:
// Test viewport width
var mql = matchMedia('(min-width: 768px)');
if (mql.matches) {
console.log('Viewport is at least 768px wide');
} else {
console.log('Viewport is less than 768px wide');
}
// Test different media types
var printQuery = matchMedia('print');
if (printQuery.matches) {
console.log('Print styles are active');
}
// Test complex queries
var complexQuery = matchMedia('screen and (min-width: 480px) and (max-width: 1024px)');
if (complexQuery.matches) {
console.log('Medium-sized screen detected');
}Result object returned by matchMedia calls.
interface MediaQueryList {
/**
* Boolean indicating whether the media query matches current conditions
* Updated each time matchMedia is called with the same query
*/
matches: boolean;
/**
* The original media query string passed to matchMedia
* Preserved exactly as provided
*/
media: string;
}The polyfill works by creating a temporary DOM structure to test media queries:
/* Example of injected test CSS */
@media (min-width: 768px) {
#mq-test-1 { width: 42px; }
}The polyfill checks if the test element (#mq-test-1) has a width of 42px to determine if the media query matches.
The matchMedia polyfill supports the same media query features as the main respond.js library:
// Simple width queries
matchMedia('(min-width: 600px)').matches;
matchMedia('(max-width: 1200px)').matches;
// Media type queries
matchMedia('screen').matches;
matchMedia('print').matches;
// Complex combined queries
matchMedia('screen and (min-width: 768px)').matches;
matchMedia('screen and (min-width: 480px) and (max-width: 1024px)').matches;
// Em-based queries
matchMedia('(min-width: 30em)').matches;
matchMedia('(max-width: 60em)').matches;The matchMedia polyfill is included in certain respond.js distributions:
The matchMedia polyfill can be used independently of respond.js for projects that only need programmatic media query testing without CSS polyfill functionality.
<!-- Standalone matchMedia polyfill -->
<script src="matchmedia.polyfill.js"></script>
<script>
// Now available in all browsers
if (matchMedia('(min-width: 768px)').matches) {
// JavaScript for larger screens
}
</script>// Check if matchMedia is already available
if (!window.matchMedia) {
// Load polyfill only if needed
var script = document.createElement('script');
script.src = 'matchmedia.polyfill.js';
document.head.appendChild(script);
}The polyfill creates fresh DOM elements for each test, so frequent matchMedia calls with the same query should store the result:
// Inefficient - creates new test elements each time
function checkViewport() {
return matchMedia('(min-width: 768px)').matches;
}
// Better - cache the MediaQueryList object
var desktopQuery = matchMedia('(min-width: 768px)');
function checkViewport() {
return desktopQuery.matches;
}The polyfill involves DOM manipulation, so it's best called:
// Good usage pattern
var queries = {
mobile: matchMedia('(max-width: 767px)'),
tablet: matchMedia('(min-width: 768px) and (max-width: 1023px)'),
desktop: matchMedia('(min-width: 1024px)')
};
function handleResize() {
if (queries.mobile.matches) {
// Mobile layout
} else if (queries.tablet.matches) {
// Tablet layout
} else if (queries.desktop.matches) {
// Desktop layout
}
}Install with Tessl CLI
npx tessl i tessl/npm-respond-js