A media query combines a media type, like screen or print, with one or more feature conditions in parentheses, like (max-width: 600px), and the enclosed CSS rules only take effect when every specified condition currently evaluates to true — multiple conditions can be combined with and, and multiple separate queries combined with a comma act as a logical or, matching if any one of them is true. The mobile-first approach to responsive design writes base styles for the smallest screens outside any media query, then uses min-width queries to progressively add or override styles for larger screens, rather than the reverse, desktop-first approach using max-width queries to scale down.
1Understanding Media Queries
A media query combines a media type, like screen or print, with one or more feature conditions in parentheses, like (max-width: 600px), and the enclosed CSS rules only take effect when every specified condition currently evaluates to true — multiple conditions can be combined with and, and multiple separate queries combined with a comma act as a logical or, matching if any one of them is true. The mobile-first approach to responsive design writes base styles for the smallest screens outside any media query, then uses min-width queries to progressively add or override styles for larger screens, rather than the reverse, desktop-first approach using max-width queries to scale down.
Prefer a mobile-first approach, base styles unwrapped for small screens, then min-width media queries progressively enhancing for larger ones, over a desktop-first approach using max-width queries to scale down — mobile-first generally produces simpler, more maintainable CSS as a project grows.
body {
font-size: 14px;
}
@media (min-width: 768px) {
body {
font-size: 16px;
}
}2Practical Example
Here is a real-world application of Media Queries showing how it is used in production CSS code.
@media (min-width: 600px) and (max-width: 900px) {
.sidebar {
display: none;
}
}3Best Practices
Follow these guidelines when working with Media Queries:
1. Use min-width media queries in a mobile-first approach, writing base styles for small screens first and adding enhancements for larger ones
2. Combine multiple conditions with and within a single media query when several conditions must simultaneously be true, like a specific width range
3. Test actual responsive breakpoints against real content and design needs, rather than blindly copying common device-specific pixel width breakpoints from elsewhere
Tip: Prefer a mobile-first approach, base styles unwrapped for small screens, then min-width media queries progressively enhancing for larger ones, over a desktop-first approach using max-width queries to scale down — mobile-first generally produces simpler, more maintainable CSS as a project grows.
body {
font-size: 14px;
}
@media (min-width: 768px) {
body {
font-size: 16px;
}
}