Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining CSS media queries within selectors

Are there any issues (performance is my primary concern) if instead of defining css selectors within media queries (example 1), you define media queries within css selectors (example 2).

Example 1 - css selectors within media queries

@media (min-width: 600px) {   .foo { ... }   .bar { ... }   .hello { ... }   .world{ ... } }  @media (min-width: 1000px) {   .foo { ... }   .bar { ... }   .hello { ... }   .world{ ... } }  @media (min-width: 1500px) {   .foo { ... }   .bar { ... }   .hello { ... }   .world{ ... } } 

Example 2 - media queries within css selectors

.foo {   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } }  .bar {   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } }  .hello {   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } }  .world{   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } } 

You may be wondering "why do this?". There are some limitations in LESS around extending classes across inside media queries, and also scoping variables.

like image 793
Andrew Avatar asked Mar 05 '15 06:03

Andrew


People also ask

Can you have nested media queries?

You can nest media queries in native CSS, as long as you're doing it from the root. It's funny to see in native CSS, but it works!

Where should I put media queries in CSS?

Important: Always put your media queries at the end of your CSS file.

Can you have multiple media queries in CSS?

You may use as many media queries as you would like in a CSS file. Note that you may use the and operator to require multiple queries to be true, but you have to use the comma (,) as the or operator to separate groups of multiple queries.

What does @media mean in CSS?

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.


2 Answers

Short answer, no. There are no performance issues in defining media queries within CSS selectors.

But let's dive in...

As described in Anselm Hannemann great article Web Performance: One or Thousands of Media Queries there is no performance loss from adding the media queries in the manner you are.

As long as the same set of media queries are being used in each selector there is no major performance hit other than your CSS file might be a bit larger.

.foo {   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } }  .bar {   @media (min-width: 600px) { ... }   @media (min-width: 1000px) { ... }   @media (min-width: 1500px) { ... } } 

However, it does matter how many different media queries you use. Different being different min-widths, max-widths and so on.

like image 137
Brett DeWoody Avatar answered Sep 25 '22 17:09

Brett DeWoody


There shouldn't be a performance difference looking at the way browsers handle media queries. Browser engines do serialize and strip out duplicated media-queries so they only need to evaluate each media query once. Also they cache the queries so that they can re-use it later on. It doesn’t matter if you use one big or multiple media-queries in your code assuming your values are mostly the same.


Some of the possibilities when there can be performance issues

  • You use multiple media queries with different values and the browser window is re-sized. As the browser window is re-sized, multiple media queries can be a big overhead on the cpu.
  • When the CSS selectors are too complex. A complex CSS selector is much performance hindering than multiple media queries. So having multiple media queries inside complex selectors can cause performance issues
like image 29
Aravind Bharathy Avatar answered Sep 24 '22 17:09

Aravind Bharathy