Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:09:57+00:00 2026-06-15T07:09:57+00:00

How do we space out media queries accurately to avoid overlap? For example, if

  • 0

How do we space out media queries accurately to avoid overlap?

For example, if we consider the code:

@media (max-width: 20em) {
    /* for narrow viewport */
}

@media (min-width: 20em) and (max-width: 45em) {
    /* slightly wider viewport */
}

@media (min-width: 45em) {
    /* everything else */
}

What will happen, across all supporting browsers, at exactly 20em, and 45em?

I’ve seen people use: things like 799px and then 800px, but what about a screen width of 799.5 px? (Obviously not on a regular display, but a retina one?)

I’m most curious about the answer here considering the spec.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:09:58+00:00Added an answer on June 15, 2026 at 7:09 am

    What are the rules for CSS media query overlap?

    Cascade.

    @media rules are transparent to the cascade, so when two or more @media rules match at the same time, the browser should apply the styles in all the rules that match, and resolve the cascade accordingly.1

    What will happen, across all supporting browsers, at exactly 20em, and 45em?

    At exactly 20em wide, your first and second media query will both match. Browsers will apply styles in both @media rules and cascade accordingly, so if there are any conflicting rules that need to be overridden, the last-declared one wins (accounting for specific selectors, !important, etc). Likewise for the second and third media query when the viewport is exactly 45em wide.

    Considering your example code, with some actual style rules added:

    @media (max-width: 20em) {
        .sidebar { display: none; }
    }
    
    @media (min-width: 20em) and (max-width: 45em) {
        .sidebar { display: block; float: left; }
    }
    

    When the browser viewport is exactly 20em wide, both of these media queries will return true. By the cascade, display: block overrides display: none and float: left will apply on any element with the class .sidebar.

    You can think of it as applying rules as if the media queries weren’t there to begin with:

    .sidebar { display: none; }
    .sidebar { display: block; float: left; }
    

    Another example of how the cascade takes place when a browser matches two or more media queries can be found in this other answer.

    Be warned, though, that if you have declarations that don’t overlap in both @media rules, then all of those rules will apply. What happens here is a union of the declarations in both @media rules, not just the latter completely overruling the former… which brings us to your earlier question:

    How do we space out media queries accurately to avoid overlap?

    If you wish to avoid overlap, you simply need to write media queries that are mutually exclusive.

    Remember that the min- and max- prefixes mean “minimum inclusive” and “maximum inclusive”; this means (min-width: 20em) and (max-width: 20em) will both match a viewport that is exactly 20em wide.

    It looks like you already have an example, which brings us to your last question:

    I’ve seen people use: things like 799px and then 800px, but what about a screen width of 799.5 px? (Obviously not on a regular display, but a retina one?)

    This I’m not entirely sure; all pixel values in CSS are logical pixels, and I’ve been hard pressed to find a browser that would report a fractional pixel value for a viewport width. I’ve tried experimenting with some iframes but haven’t been able to come up with anything.

    From my experiments it would seem Safari on iOS rounds all fractional pixel values to ensure that either one of max-width: 799px and min-width: 800px will match, even if the viewport is really 799.5px (which apparently matches the former).


    1 Although none of this is explicitly stated in either the Conditional Rules module or the Cascade module (the latter of which is currently slated for a rewrite), the cascade is implied to take place normally, since the spec simply says to apply styles in any and all @media rules that match the browser or media.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stumped about a stack overflow error--out of stack space (application error code: 12246)--that
<html> <head> <meta name=viewport content=width=device-width/> <title>HAHAHA</title> <STYLE TYPE=text/css media=screen> * { padding: 0; margin:
I'm using the below code to try and automatically space out my cell height
I am using text-align:justify to evenly space out inline-block elements. I am using jQuery
I'm trying to draw an ellipsoid in 3d space out of individual blocks. I
My vserver is running out of space. I noticed the traffic stats of apache
As I am running out of my space on GitHub on my free account
Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
Possible Duplicate: White space at bottom of anchor tag Check out this sample page
I figured out that of course . and SPACE aren't allowed. Are there other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.