How do we space out media queries accurately to avoid overlap?
For example, if we consider the code:
@media (max-width: 20em) {
/* for narrow viewport */
}
@media (min-width: 20em) and (max-width: 45em) {
/* slightly wider viewport */
}
@media (min-width: 45em) {
/* everything else */
}
What will happen, across all supporting browsers, at exactly 20em, and 45em?
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?)
I’m most curious about the answer here considering the spec.
Cascade.
@mediarules are transparent to the cascade, so when two or more@mediarules match at the same time, the browser should apply the styles in all the rules that match, and resolve the cascade accordingly.1At exactly 20em wide, your first and second media query will both match. Browsers will apply styles in both
@mediarules 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:
When the browser viewport is exactly 20em wide, both of these media queries will return true. By the cascade,
display: blockoverridesdisplay: noneandfloat: leftwill 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:
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
@mediarules, then all of those rules will apply. What happens here is a union of the declarations in both@mediarules, not just the latter completely overruling the former… which brings us to your earlier question:If you wish to avoid overlap, you simply need to write media queries that are mutually exclusive.
Remember that the
min-andmax-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:
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: 799pxandmin-width: 800pxwill match, even if the viewport is really 799.5px (which apparently matches the former).1 Although 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
@mediarules that match the browser or media.