Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are media queries counted in IE's CSS selectors limit?

Any idea how does IE treat media queries regarding this CSS selector limit?

Does it see it as a single CSS rule or it sees it as 1 rule (the @media declaration) + number of rules inside the @media rule?

This being for IE9 as from what i know IE9 is the only IE that supports media queries while also having this issue with 4095 selectors.

I'm trying to write a tool to split the CSS accordingly and I'm not sure how to count the rules, as in a @media rule will be counted as 1 or will be counted as 1 + nr of rules inside?

like image 556
daniels Avatar asked Jul 31 '14 06:07

daniels


People also ask

How many media queries should I use in CSS?

You can also have more than one breakpoint for CSS selectors. You might add on additional media queries to continue changing the h1 size as the viewport increases to 62em (992px) and 87em (1392px) wide. You can create as many breakpoints as you would like, and you can use any width that you would like.

How do you write a media query for maximum and minimum width?

Max-width and min-width can be used together to target a specific range of screen sizes. @media only screen and (max-width: 600px) and (min-width: 400px) {...} The query above will trigger only for screens that are 600-400px wide. This can be used to target specific devices with known widths.

Can you have more than one media query?

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. The not keyword can be used to alter the logic as well.

How does media query work CSS?

A media query computes to true when the media type (if specified) matches the device on which a document is being displayed and all media feature expressions compute as true. Queries involving unknown media types are always false.


1 Answers

It appears that media queries are not included in the selector limit. All rules within all media queries are counted though.

I wrote a test that performs a binary search to find the turning point where the last selector is ignored. It is available at https://robwu.nl/s/css-selector-limit-test.html. The binary search runs over the range 0 - 4200 and reports how often the input selector fits until the last selector is not applied any more. If the result is greater than 4096, the test case reports "infinity".

Results:

Turning point at 4095 for: #DUMMY{color:red;}
Turning point at 4095 for: @media screen(min-width:9px) { #DUMMY {color:red;} }
Turning point at 2047 for: @media screen(min-width:9px) { #DUMMY, #DUMMY {color:red;} }
Turning point at 1023 for: @media screen(min-width:9px) { #DUMMY {color:red;} #DUMMY, #DUMMY, #DUMMY {color:red;} }
Turning point at 1364 for: @media screen(min-width:9px) { #DUMMY {color:red;} } @media screen(max-width:9px) {#DUMMY, #DUMMY {color:red}}
Turning point at 1364 for: @media screen(min-width:9px) { #DUMMY {color:red;} } @media screen(max-width:9px) {#DUMMY {color:red;}} @media screen(max-width:9px){ #DUMMY {color:red;}}
Turning point at infinity for: @media screen (min-width:9px) { }
Turning point at infinity for: @media screen (min-width:9px) { } @media screen (min-width:9px) { } @media screen (min-width:9px) { }
Turning point at infinity for: @font-face { font-family: "blablablablabla"; } 

The last three tests show that media queries (and other at-rules such as @font-face) are not counted in the selector limit.

I have seen many "css rule" counter scripts here on Stack Oveflow (e.g. https://stackoverflow.com/a/20496041 and https://stackoverflow.com/a/12313690), but all of them are wrong, unfortunately. A media query appears as one entry in the cssRules/rules list. The right way to count the number of selectors in a stylesheet is to recursively process the style sheet to deal with (nested) @media at-rules.

like image 135
Rob W Avatar answered Sep 25 '22 16:09

Rob W