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 7041949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:06:32+00:00 2026-05-28T02:06:32+00:00

Of late, I’ve been designing sites that are more responsive and I’ve been using

  • 0

Of late, I’ve been designing sites that are more responsive and I’ve been using CSS media queries frequently. One pattern I noticed is that the order in which the media queries are defined actually matters. I didn’t test it in every single browser, but just on Chrome. Is there an explanation for this behaviour? Sometimes it gets frustrating when your site doesn’t work as it should and you are unsure if it’s the query or the order in which the query is written.

Here’s an example:

HTML

<body>
    <div class="one"><h1>Welcome to my website</h1></div>
    <div class="two"><a href="#">Contact us</a></div>
</body>

CSS:

body{
font-size:1em; /* 16px */
}

.two{margin-top:2em;}



/* Media Queries */

@media (max-width: 480px) {
    .body{font-size: 0.938em;}

}
/* iphone */
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
    body {font-size: 0.938em;}
}
/*if greater than 1280x800*/
@media (min-width: 1200px) {
       .two{margin-top:8em;}
            }
/*1024x600*/
@media (max-height: 600px) {
       .two{margin-top:4em;}
}
/*1920x1024*/
@media (min-height: 1020px) {
       .two{margin-top:9em;}
}
/*1366x768*/
@media (min-height: 750px) and (max-height: 770px) {
       .two{margin-top:7em;}
}

However, If I wrote the query for 1024×600 in the last, the browser would ignore it and apply the margin value specified in the starting of the CSS (margin-top:2em).

/* Media Queries - Re-arranged version */

@media (max-width: 480px) {
    .body{font-size: 0.938em;}
}
/* iphone */
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
    body {font-size: 0.938em;}
}
/*if greater than 1280x800*/
@media (min-width: 1200px) {
       .two{margin-top:8em;}
}
/*1920x1024*/
@media (min-height: 1020px) {
       .two{margin-top:9em;}
}
/*1366x768*/
@media (min-height: 750px) and (max-height: 770px) {
       .two{margin-top:7em;}
}
 /*1024x600*/
@media (max-height: 600px) {
       .two{margin-top:4em;}
}

If my understanding of media queries are correct, the order shouldn’t matter, but it seems it does. What could be the reason?

  • 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-05-28T02:06:33+00:00Added an answer on May 28, 2026 at 2:06 am

    That’s by design of CSS — Cascading Style Sheet.

    It means that, if you apply two rules that collide to the same elements, it will choose the last one that was declared, unless the first one has the !important marker or is more specific (e.g. html > body vs just body, the latter is less specific).

    So, given this CSS

    @media (max-width: 600px) {
      body {
        background: red;
      }
    }
    
    @media (max-width: 400px) {
      body {
        background: blue;
      }
    }
    

    if the browser window is 350 pixels wide, the background will be blue, while with this CSS

    @media (max-width: 400px) {
      body {
        background: blue;
      }
    }
    
    @media (max-width: 600px) {
      body {
        background: red;
      }
    }
    

    and the same window width, the background will be red. Both rules are indeed matched, but the second one it’s the one that is applied because is the last rule.

    Finally, with

    @media (max-width: 400px) {
      body {
        background: blue !important;
      }
    }
    
    @media (max-width: 600px) {
      body {
        background: red;
      }
    }
    

    or

    @media (max-width: 400px) {
      html > body {
        background: blue;
      }
    }
    
    @media (max-width: 600px) {
      body {
        background: red;
      }
    }
    

    the background will be blue (with a 350 pixels wide window).

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

Sidebar

Related Questions

It's too late to change the question, but more precise would have been to
I have a late requirement change that requires tab navigation. I have already using
Off late I have been dumping relatively large tables using SSMS. The usual way
I'm using late binding OLE automation to work with Excel. The problem is that
During late development of an application I've been git pushing to heroku such that
Off late I'd been hearing that applications written in different languages can call each
It's late on a Friday and I'm scratching my head on this one. I'm
Well it's late so I assume I'm just exhausted but I've been trying to
Late one afternoon, I did an export of my project repository and took home
It must be late... Given the following HTML, how would one select all of

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.