Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a media query with a smaller min-width overwrite a larger one?

I'm trying to understand why the min-width: 600 media query below would overwrite the min-width: 768media query.

I know that 600px comes later in the CSS file which could be a reason, but surely only one should only be applied if the screen size is either 600 or 768?

I'm looking into media query documentation now, but have yet been able to explain this.

enter image description here

Thanks for any help.

like image 623
Dano007 Avatar asked Feb 28 '15 14:02

Dano007


People also ask

Does width override min-width?

The min-width property in CSS is used to set the minimum width of a specified element. The min-width property always overrides the width property whether followed before or after width in your declaration.

Does Min-width override max-width?

Change the maximum width. max-width overrides width , but min-width overrides max-width .

What happens when the tags min-width value becomes greater than the width of the page?

The min-width property defines the minimum width of an element. If the content is smaller than the minimum width, the minimum width will be applied. If the content is larger than the minimum width, the min-width property has no effect.

How does min-width media query work?

The min-width and max-width are media features that correspond to a certain media type that has been specified in the media query. The min-width specifies the minimum screen width of a specific device, meanwhile, The max-width media feature states the maximum screen width of a specific device.


2 Answers

I know that 600px comes later in the CSS file which could be a reason

This is usually the reason, aside from unrelated authoring mistakes or, worse, browser bugs. Anything that is greater than or equal to 768px is, by necessity, also greater than or equal to 600px, so they both have to match.

See the following related questions for more information:

  • Why does the order of media queries matter in CSS?
  • What are the rules for CSS media query overlap?
  • CSS specificity, Media Queries and min-width

but surely only one should only be applied if the screen size is either 600 or 768?

That's not true; @media rules are completely independent of one another. It doesn't make sense for @media rules to be exclusive, in particular when you consider that media queries can consist of any combination of media features.

For example, what should happen in this case when the media is (width: 600px) and (height: 300px)? (The correct behavior is that every rule is applied, with the last one taking precedence, because there is no other way for the UA to account for both width and height when evaluating the queries.)

@media {
    body { background-color: white; }
}

@media (min-width: 600px) {
    body { background-color: red; }
}

@media (min-width: 300px) and (max-height: 300px) {
    body { background-color: yellow; }
}

@media (max-height: 600px) {
    body { background-color: blue; }
}
like image 180
BoltClock Avatar answered Oct 05 '22 09:10

BoltClock


Both of yours media query have no below limit. That mean both will be applied to from 0px to either 600px or 768px.

So when you are below 600px both will be load but only one will got applied. The order of Media query is the key. The one that after will override the rules before it.

If you want to specific it to be less confusing. Just put the limits/range for it like:

@media (min-width: 600px) and (max-width: 768px){
  ...
}
like image 22
Linh Pham Avatar answered Oct 05 '22 08:10

Linh Pham