CSS Media Query Not Working Fix
In this tutorial, you'll learn about CSS Media Query Not Working Fix. We cover key concepts, practical examples, and best practices.
The Problem
You write a @media rule but the styles do not apply when the viewport matches the condition. The browser ignores your query entirely or applies the wrong rule.
Quick Fix
Step 1: Verify the viewport meta tag
Without the viewport meta tag, mobile browsers use a simulated viewport width:
<!-- Wrong — missing viewport meta tag -->
<head>
<title>Page</title>
</head>
<!-- Right -->
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
Expected output: Media queries now respond to the actual device width.
Step 2: Check media query syntax
Parentheses are required around each condition:
/* Wrong — missing parentheses */
@media screen and max-width: 768px {
.container { width: 100%; }
}
/* Right */
@media screen and (max-width: 768px) {
.container { width: 100%; }
}
Expected output: The rule applies at viewport widths of 768px and below.
Step 3: Understand min-width vs max-width
min-width means "at least this width". max-width means "at most this width":
/* Wrong — confusing min and max */
@media (max-width: 768px) {
/* Intended for mobile > 768px, but this is mobile < 768px */
}
/* Right — mobile-first uses min-width */
@media (min-width: 768px) {
/* Tablet and above */
}
Expected output: Styles apply at the intended breakpoint.
Step 4: Maintain correct cascade order
Place wider breakpoints after narrower ones:
/* Wrong — narrower query overrides wider one */
@media (min-width: 768px) { /* tablet */ }
@media (min-width: 1024px) { /* desktop */ }
/* tablet query must be above desktop */
With mobile-first, go from smallest to largest:
/* Default: mobile styles */
.container { padding: 10px; }
@media (min-width: 768px) { .container { padding: 20px; } }
@media (min-width: 1024px) { .container { padding: 30px; } }
Expected output: Larger viewports correctly inherit breakpoint styles.
Step 5: Use not, and, and comma correctly
/* Wrong — mixing logic operators */
@media screen and (max-width: 768px), print { }
/* Right */
@media screen and (max-width: 768px) { }
@media print { }
Expected output: Each media query applies to the correct media type.
Prevention
- Always include the viewport meta tag in HTML head
- Use min-width for mobile-first development
- Order breakpoints from smallest to largest
- Test with browser DevTools device mode
Common Mistakes with media query not working
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro