Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS media queries - Order matters?

Working a lot now with CSS media queries, I wondered in which order it's best to use them.

Method 1

@media only screen and (min-width: 800px) {
    #content { ... }
    #sidebar { ... }
}
@media only screen and (max-width: 799px) {
    #content { ... }
    #sidebar { ... }
}

Like this obviously the code is shorter, but with a lot of CSS you end up having the CSS of one container spread to multiple places in your stylesheet.


Method 2

@media only screen and (min-width: 800px) {
    #content { ... }
}
@media only screen and (max-width: 799px) {
    #content { ... }        
}
@media only screen and (min-width: 800px) {
    #sidebar { ... }
}
@media only screen and (max-width: 799px) {
    #sidebar { ... }
}

Like this if you specify the screen size (at which the CSS is active) for each container a new, the overview in my humble opinion is much better. But with a lot of CSS you will use the @media query dozens and dozens times.


Does the second method cause significantly longer load time or has any other disadvantages?




EDIT:

I might have been not clear enough. My question doesn't really concern the order or the queries as such or about overwriting CSS declarations. What I wonder about is rather the norms how other people include the media query "statments" into their css.

Lets say I have only one breaking point where I switch some CSS. So I have one media query for min:800px and a second for max:799px.

Should I use both query "statements"

@media only screen and (min-width: 800px) { ... }
@media only sreen and (max-width: 799px) { ... }

only once in my whole stylesheet and include ALL the CSS for ALL containers into the two media query "statments"? Or is it okay as well to use the media query "statments" mutiple times?

I mean instead of making two seperate areas in the stylesheet (one for CSS above and one for below 800px), if there are any concerns about the method of using the media query "statments" instead multiple times (for each part of the page again, like for Content, Widgets etc to make them responsive)?
I would just like to have the CSS for above and below 800px in two different parts of my stylesheet.

I know that ofc both methodes are working, I am jsut curious about the norms and if using the media query "statements" dozens or hundreds of times within a CSS sheet (instead of just twice in the case I jsut mentioned) will increase the loading times?

like image 325
ink Avatar asked Dec 19 '22 05:12

ink


2 Answers

My answer on how you should use media queries can be applied to your question:

Here is how you should use media queries:

Remember use the sizes you like/need. This below is just for demo purposes.

Non-Mobile First Method using max-width:

/*==========  Non-Mobile First Method  ==========*/

    @media only screen and (max-width: 960px) {
      /*your CSS Rules*/     
    }
    @media only screen and (max-width: 768px) {
      /*your CSS Rules*/     
    }
    @media only screen and (max-width: 640px) {
      /*your CSS Rules*/     
    }
    @media only screen and (max-width: 480px) {
      /*your CSS Rules*/     
    }       
    @media only screen and (max-width: 320px) {
      /*your CSS Rules*/ 
    }

Mobile First Method using min-width:

/*==========  Mobile First Method  ==========*/
 
    @media only screen and (min-width: 320px) {
      /*your CSS Rules*/     
    }
    @media only screen and (min-width: 480px) {
      /*your CSS Rules*/     
    }
    @media only screen and (min-width: 640px) {
      /*your CSS Rules*/     
    }
    @media only screen and (min-width: 768px) {
      /*your CSS Rules*/     
    }       
    @media only screen and (min-width: 960px) {
      /*your CSS Rules*/ 
    }

Here is a good tutorial from W3.org


Based on your edited question:

I guess this depends on each developer and how they need/think to develop his/her project.

Here is what I use to do ** (when not not using Pre-compliers)**:

I create a file styles.css which includes the general styles that will apply to the project like this:

/*==========  All Screens  ==========*/
    {
      /*General CSS Rules*/     
    }

Then having the media queries below, either using the non-mobile or mobile approach method explained above (in my case I usual use the non-mobile approach method).

But, depending on the projects you may need to have some other breaks besides the "standard" which can led you to use the rules in the way you mentioned.

Plus there are developers who prefer to separate into 2 files, the one with general styles CSS and other one with media queries styles.

Important: There is one difference from creating a file with general styles + 1 media queries (min-width:800px or max-width:799px), then only having a file with 2 media queries (min-width:800px/max-width:799px), which is when you have the general rules it will apply to ALL widths, therefore you just need to set the rules for 1 media queries.


Based on your last comment, the answer I could give you would be opinion-wised, so the best I can do for you is to give you a few articles so you can have your own opinion on this topic:

How many media queries is too many?

Web Performance: One or thousands of Media Queries?

Debunking Responsive CSS Performance Myths

like image 132
dippas Avatar answered Jan 03 '23 08:01

dippas


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

The second one will always display the content at 799px and whatever content has been styled as the style allocated for 799 rather than 800px if the device is 800px, in this case because it's 1px difference it doesn't make much different, but if you did it at around 200px different it would cause problems for your design.

Example:

if you have it this way:

@media (max-width: 800px) {
  body {
    background: red;
  }
}

@media (max-width: 799px) {
  body {
    background: green;
  }
}

The background would be green if the device is 799px in width or less.

if it was the other way round

@media (max-width: 799px) {
      body {
        background: red;
      }
    }

    @media (max-width: 800px) {
      body {
        background: green;
      }
    }

if the device width was less than 799px the background would be green because no !important keyword has been defined.

when the !important keyword has been defined, result for example one will be the same

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

    @media (max-width: 800px) {
      body {
        background: green;
      }
    }

It won't take the processor longer unless the two elements collide. You'll be fine to vary min-width and max-width.

like image 21
BIW Avatar answered Jan 03 '23 10:01

BIW