What are the rules for CSS media query overlap?
Solution 1:
What are the rules for CSS media query overlap?
Cascade.
@media
rules are transparent to the cascade, so when two or more @media
rules match at the same time, the browser should apply the styles in all the rules that match, and resolve the cascade accordingly.1
What will happen, across all supporting browsers, at exactly 20em, and 45em?
At exactly 20em wide, your first and second media query will both match. Browsers will apply styles in both @media
rules and cascade accordingly, so if there are any conflicting rules that need to be overridden, the last-declared one wins (accounting for specific selectors, !important
, etc). Likewise for the second and third media query when the viewport is exactly 45em wide.
Considering your example code, with some actual style rules added:
@media (max-width: 20em) {
.sidebar { display: none; }
}
@media (min-width: 20em) and (max-width: 45em) {
.sidebar { display: block; float: left; }
}
When the browser viewport is exactly 20em wide, both of these media queries will return true. By the cascade, display: block
overrides display: none
and float: left
will apply on any element with the class .sidebar
.
You can think of it as applying rules as if the media queries weren't there to begin with:
.sidebar { display: none; }
.sidebar { display: block; float: left; }
Another example of how the cascade takes place when a browser matches two or more media queries can be found in this other answer.
Be warned, though, that if you have declarations that don't overlap in both @media
rules, then all of those rules will apply. What happens here is a union of the declarations in both @media
rules, not just the latter completely overruling the former... which brings us to your earlier question:
How do we space out media queries accurately to avoid overlap?
If you wish to avoid overlap, you simply need to write media queries that are mutually exclusive.
Remember that the min-
and max-
prefixes mean "minimum inclusive" and "maximum inclusive"; this means (min-width: 20em)
and (max-width: 20em)
will both match a viewport that is exactly 20em wide.
It looks like you already have an example, which brings us to your last question:
I've seen people use: things like 799px and then 800px, but what about a screen width of 799.5 px? (Obviously not on a regular display, but a retina one?)
This I'm not entirely sure; all pixel values in CSS are logical pixels, and I've been hard pressed to find a browser that would report a fractional pixel value for a viewport width. I've tried experimenting with some iframes but haven't been able to come up with anything.
From my experiments it would seem Safari on iOS rounds all fractional pixel values to ensure that either one of max-width: 799px
and min-width: 800px
will match, even if the viewport is really 799.5px (which apparently matches the former).
1Although none of this is explicitly stated in either the Conditional Rules module or the Cascade module (the latter of which is currently slated for a rewrite), the cascade is implied to take place normally, since the spec simply says to apply styles in any and all @media
rules that match the browser or media.
Solution 2:
I have tried as recommended here:
@media screen and (max-width: calc(48em - 1px)) {
/*mobile styles*/
}
@media screen and (min-width: 48em) {
/*desktop styles*/
}
but found that this was not a good idea because it does not work on Chrome right now either on my Ubuntu desktop or on my Android phone. (as explained here: calc() not working within media queries) But, I found a better way...
@media screen and (max-width: 47.9999em) {
/*mobile styles*/
}
@media screen and (min-width: 48em) {
/*desktop styles*/
}
and bam!
Solution 3:
calc()
can be use to work around this (min-width: 50em and max-width: calc(50em - 1px)
will be correctly stacked), but poor browser support and i would not recommending it.
@media (min-width: 20em) and (max-width: calc(45em - 1px)) {
/* slightly wider viewport */
}
Infos:
- https://developer.mozilla.org/en-US/docs/CSS/calc
- http://caniuse.com/calc
- http://www.w3.org/TR/css3-values/#calc
Some others mentioned, not using the em
unit would help in your queries stacking.