π CSS Media Queries Level 4 β Range Queries (Modern Syntax)
π What Are CSS Level 4 Range Queries?
CSS Media Queries Level 4 introduced a **new, clean, and mathematical way** to write responsive breakpoints. These are called range queries because they let you compare viewport sizes using mathematical operators like < , <= , > , >=.
>>βLevel 4 range queries make media queries readable β just like math.β
Note
π Good news: All modern browsers support Level 4 range syntax!
π― Why Use Range Queries?
- Cleaner, shorter, and more readable media queries
- No more writing two separate queries for min-width + max-width
- Acts like mathematical inequalities β easier to understand
- Works with width, height, aspect-ratio, resolution, etc.
π Old Way vs New Way
| Purpose | Old Syntax | New (Level 4) Syntax |
|---|---|---|
| Between 600px and 900px | @media (min-width: 600px) and (max-width: 900px) | @media (600px β€ width β€ 900px) |
| Above 1200px | @media (min-width: 1200px) | @media (width β₯ 1200px) |
| Below 500px | @media (max-width: 500px) | @media (width β€ 500px) |
Note
π‘ Range syntax is **mathematically correct**, readable, and avoids duplication.
π― Syntax Format
Range Query Syntax
@media (<value> <operator> <feature> <operator> <value>) { ... }Examples of operators:
- < β less than
- <= β less than or equal
- > β greater than
- >= β greater than or equal
π§ͺ Example 1 β Between Two Breakpoints
Between 600px and 1024px
@media (600px <= width <= 1024px) {
.container {
padding: 20px;
}
}Applies only when viewport width is **between 600 and 1024px**, inclusive.
π§ͺ Example 2 β Mobile Breakpoint
Below 480px
@media (width <= 480px) {
.nav {
flex-direction: column;
}
}π§ͺ Example 3 β Desktop Breakpoint
Above 1200px
@media (width >= 1200px) {
.layout {
max-width: 1100px;
}
}π§ͺ Example 4 β Aspect Ratio Range
Aspect Ratio Range
@media (3/4 <= aspect-ratio <= 16/9) {
.video {
border: 3px solid green;
}
}Works great for video layouts and responsive media.
π§ More Examples of Allowed Forms
Single Comparison
Single Comparison
@media (width > 768px) { ... }Reversed Order (also allowed)
Reversed Syntax
@media (768px < width) { ... }Height Queries
Height Range
@media (400px <= height <= 800px) {
.sidebar { display: block; }
}π± Real-World Example β Fully Responsive Layout
Full Range Query System
/* Mobile */
@media (width <= 600px) {
.grid { grid-template-columns: 1fr; }
}
/* Tablet */
@media (600px < width <= 1024px) {
.grid { grid-template-columns: repeat(2, 1fr); }
}
/* Desktop */
@media (width > 1024px) {
.grid { grid-template-columns: repeat(4, 1fr); }
}Clean, readable, scalable β no repeated min/max syntax.
β οΈ Common Mistakes
- Trying to mix old and new syntax incorrectly
- Assuming all browsers support it (older browsers do not)
- Using commas instead of logical AND ranges
Note
β All major modern browsers (Chrome, Firefox, Safari, Edge) support range queries now.
π₯ Summary
CSS Level 4 Range Queries modernize the way we write responsive breakpoints. They give you a mathematical, readable, and intuitive way to define screen ranges without writing multiple media conditions.
>>βRange queries turn media queries into simple math β clean, powerful, future-proof.β
Learn more βMDN Docs π