惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
O
OpenAI News
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
云风的 BLOG
云风的 BLOG
罗磊的独立博客
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
D
DataBreaches.Net
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
雷峰网
雷峰网
V
Visual Studio Blog
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
博客园 - 【当耐特】
G
Google Developers Blog
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Martin Fowler
Martin Fowler
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
AI
AI
Google Online Security Blog
Google Online Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
PCI Perspectives
PCI Perspectives
爱范儿
爱范儿
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Media Queries Range Syntax
Ahmad Shadeed · 2026-05-04 · via Ahmad Shadeed

Introduction

Media queries are the backbone of responsive design. We use them to control how a design should change based on the viewport size. But the min-width and max-width syntax can be confusing, and in some cases, cause layout bugs that take time to spot.

This article aims to convince you to use range queries, starting today.

In the following demo, we have a typical navigation pattern. On mobile, only the logo and the menu toggle are visible. At larger sizes, the navigation is visible and the menu toggle is hidden.

Take a look.

And here are the media queries used to achieve this.

/* Hide the navigation on small sizes */
.nav {
  @media (max-width: 300px) {
    display: none;
  }
}

/* Hide the toggle on large sizes */
.toggle {
  @media (min-width: 300px) {
    display: none;
  }
}

The problem happens when both media queries meet at the same breakpoint value (300px). Both elements will be hidden at the same time.

max-width is equivalent to <=

min-width is equivalent to >=

In the demo below, increase the container width to 300px and notice how both the menu and the button are hidden at the same time.

This is the confusion that happens when using min-width and max-width media queries with the same breakpoint value.

When you are too focused on your work, you might overlook this until you find yourself 45 minutes deep in debugging.

I have been there, and I know how frustrating it can be. To fix the problem, we need two different breakpoint values:

  • max-width: 299px: this means less than or equal to 299px
  • min-width: 300px: this means greater than or equal to 300px
/* Hide the navigation on small sizes */
.nav {
  @media (max-width: 299px) {
    display: none;
  }
}

/* Hide the toggle on large sizes */
.toggle {
  @media (min-width: 300px) {
    display: none;
  }
}

Now when the viewport width is 300px, the navigation will be visible, and the toggle will be hidden.

How far can this solution take us? For a single use case like this, it’s okay. However, when we have multiple breakpoints, things start to get complicated.

Media query ranges solve this easily without having to manually offset breakpoints. We can use comparison operators to achieve the same result in a more readable way.

/* Less than or equal to 300px */
.nav {
  @media (width <= 300px) {
    display: none;
  }
}

/* Greater than 300px */
.toggle {
  @media (width > 300px) {
    display: none;
  }
}

This is clearer because you can read the operators and understand the logic rather than trying to guess the min- or max- syntax.

Here is the same demo, but with range media queries.

This is part of the Media Queries Level 4 module.

Readability

They are easier to read and understand. When looking at the CSS, you can read what it means without guessing. This will lead to easier debugging.

Well supported

If you are using container queries in production, then you should also use media query ranges.

Browser support has been great since March 2023, according to Baseline.

Media query range syntax

Baseline Widely available

Supported in Chrome: yes. Supported in Edge: yes. Supported in Firefox: yes. Supported in Safari: yes.

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023

Media query range syntax on Web Platform Status

It makes ranges easier

If you want to limit a specific part of the design between two breakpoints, it becomes easier to write.

/* Before */
.section {
  @media (min-width: 300px)  and (max-width: 500px) {
    /* styles for the card */
  }
}

/* After */
.section {
  @media (300px <= width <= 500px) {
    /* styles for the card */
  }
}

In the following demo, see how the min/max prefix values translate to the range syntax.

@media () and ()

@media (300px <= width <= 500px)

The range syntax can also be used for container queries. For the page header at the top of the page, I used container queries with the range syntax.

Just swap @media with @container.

h1 {
  @container (width >= 300px) {
    color: #fff;
    background-color: var(--brand-1);
  }

  @container (width >= 500px) {
    background-color: hsl(from var(--brand-1) calc(h + 100) s l);
  }
}

Here is a video of it:

Further reading

You can learn more

Building layouts can be a challenging task, specially if you don’t know the core mental model of CSS layouts. You don’t have to worry about that anymore. I released an interactive CSS layout course, and I called it, The Layout Maestro.

A screenshot of the Layout Maestro course landing page

Check it out here.

Conclusion

Media query ranges are clearer, easier to debug, and well-supported. Give them a try in a project, I’m sure you will find it useful.