Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
The Media Query API lets you check device features in JavaScript - screen size, dark mode, touch support, and more! Just like CSS media queries, but in JS.
Resize window to see detection in action!
<div style="max-width: 600px; margin: 0 auto; font-family: sans-serif;">
<h2 style="color: #0d9488; margin-bottom: 20px;">📱 Media Query Demo</h2>
<!-- Screen Size Detection -->
<div style="background: #ccfbf1; padding: 20px; border-radius: 8px; margin-bottom: 15px; border: 2px solid #14b8a6;">
<h3 style="color: #115e59; margin-top: 0;">📐 Screen Size</h3>
<div id="screenInfo" style="font-size: 18px; font-weight: 600; color: #0f766e;"></div>
</div>
<!-- Device Features -->
<div style="background: #e0f2fe; padding: 20px; border-radius: 8px; margin-bottom: 15px; border: 2px solid #0ea5e9;">
<h3 style="color: #0c4a6e; margin-top: 0;">🎨 Features Detected</h3>
<div id="features" style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; font-size: 14px;"></div>
</div>
<!-- Live Updates -->
<div style="background: #fef3c7; padding: 15px; border-radius: 6px; border-left: 4px solid #f59e0b;">
<strong style="color: #92400e;">💡 Try This:</strong>
<span style="color: #78350f;"> Resize your browser window to see values update in real-time!</span>
</div>
</div>Loading preview...
// Check if mobile
const isMobile = window.matchMedia('(max-width: 768px)').matches;
if (isMobile) {
console.log('User is on mobile');
// Load mobile-specific features
loadMobileMenu();
} else {
console.log('User is on desktop');
// Load desktop features
loadDesktopMenu();
}
// Listen for changes (window resize)
const mediaQuery = window.matchMedia('(max-width: 768px)');
mediaQuery.addEventListener('change', (e) => {
if (e.matches) {
console.log('Switched to mobile');
loadMobileMenu();
} else {
console.log('Switched to desktop');
loadDesktopMenu();
}
});
// 🎯 Responsive behavior in JavaScript!// Check if user prefers dark mode
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (prefersDark) {
console.log('User prefers dark mode');
document.body.classList.add('dark-theme');
} else {
console.log('User prefers light mode');
document.body.classList.add('light-theme');
}
// Listen for changes (system theme switch)
const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');
darkModeQuery.addEventListener('change', (e) => {
if (e.matches) {
console.log('Switched to dark mode');
document.body.classList.remove('light-theme');
document.body.classList.add('dark-theme');
} else {
console.log('Switched to light mode');
document.body.classList.remove('dark-theme');
document.body.classList.add('light-theme');
}
});
// 🎯 Respect system preferences!Load different JS features for mobile/desktop
Detect and respond to system theme
Respect reduced motion preferences
Track device types and preferences
const isMobile = window.matchMedia('(max-width: 768px)').matches;
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;const mediaQuery = window.matchMedia('(max-width: 768px)');
mediaQuery.addEventListener('change', (e) => {
if (e.matches) {
console.log('Mobile view');
}
});🎯 Use Any CSS Query: Same queries as CSS media queries
Check media queries
Returns MediaQueryList
Boolean property
True if query matches
Listen for changes
Resize, theme switch
Use CSS media queries
In JavaScript