What are the rules for CSS media query overlap?
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).
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 @media
rules that match the browser or media.
How can I avoid media query overlap?
The only reliable way to create two mutually exclusive @media
blocks for any given media query is to use not
to negate it in one of the blocks. Unfortunately, this means repeating your media query once for each @media
block. So, instead of this for example:
@media (max-width: 49.9375em) {
body {
color: red;
}
}
@media (min-width: 50em) {
body {
font-size: larger;
}
}
You would have this:
/*
* Note: Media Queries 4 still requires 'not' to be followed by a
* media type (e.g. 'all' or 'screen') for reasons I cannot comprehend.
*/
@media not all and (min-width: 50em) {
body {
color: red;
}
}
@media (min-width: 50em) {
body {
font-size: larger;
}
}
Interactive jsFiddle demo
This is very effective at closing the gap with range media features like width
and height
since it essentially turns this into an either-or scenario. But, like your first two options, it isn't perfect: as mentioned, you have to repeat the same media query twice, and add not
to one of them. There is no if/else construct for @media
as described in Conditional Rules 3.
Although I mention this in my answer to your previous question:
From my experiments it would seem Safari on iOS rounds all fractional pixel values to ensure that either one of
max-width: 799px
andmin-width: 800px
will match, even if the viewport is really 799.5px (which apparently matches the former).
It should be noted, still, that I've noticed some quirks when it comes to rounding. That said, I haven't been able to find a fractional value that would evade both media queries and end up not receiving styles from either set of rules (which, by the way, is the worst that can happen, so don't worry about potentially creating a space-time rift). That must mean browsers — at least, Safari as I've tested — do a reasonable job of ensuring they satisfy media queries even if you have values that differ (by exactly 1 CSS pixel).
When it comes to units with larger gaps that can be observed on desktop browsers, though, like ems, there is a much larger margin of error. For example, one comment suggests using 49.99999em instead of something more arbitrary than 49.9375em, but apparently there is a difference, at least with a default font size of 16px.
I simplified your code, changed the media queries to use decimal values, and put the code in jsFiddle:
@media (max-width: 49.9375em) {
body {
color: red;
}
}
@media (min-width: 50em) {
body {
font-size: larger;
}
}
If you resize the Result pane to exactly 800 pixels (the text will update to guide you along), you actually end up with different results depending on whether @media (max-width: 49.9375em)
is used, or @media (max-width: 49.99999em)
is used (I was surprised by this too)...
Either way, you're right: option 2 has its drawbacks too. I'm not particularly fond of it, to be honest, because I wouldn't want to crack my head over device and user agent quirks which are out of my control. If you're like me, I suppose it would be better to go through the inconvenience of redeclaring your rules at the cost (?) of being more vigilant around your code, as that's at least still within your control as an author.
Why are my media queries stepping on each other?
I think you are having hard time with orientation
remove orientation
and all works fine.
Check that snippet.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
background-color: yellow;
}
@media only screen and (min-width: 1024px) and (max-width: 1366px) {
body {
background-color: blue;
}
}
@media only screen and (min-width: 810px) and (max-width: 1080px) {
body {
background-color: red;
}
}
@media only screen and (min-width: 768px) and (max-width: 1024px) {
body {
background-color: pink;
}
}
CSS: media queries to match width x and width = x. Weird behavior
However, I have notice that there is a certain width of the window where the styles from both media queries seem to be mixed together. (It definitely isn't the case that no query is applied.)
Correct; instead, both of them are being applied, and that's when the width of the viewport exactly equals 34 ems. What happens then is that the cascade applies as usual. See my answer to the following question for an explanation:
What are the rules for CSS media query overlap?
The easiest way to handle this for two ranges of screen sizes is to utilize the cascade, but differently from what you have: by writing your rules generally for one screen size, and only including a media query for the other screen size as shown in dwreck08's answer.
Otherwise, if you want exactly one rule to be applied at a time without relying on the cascade to override your styles, use either min-width
or max-width
only, and negate it in the other rule with the not
keyword. As an example, you can have either this:
@media screen and (max-width: 34em) {} /* Smaller screen sizes */
@media not screen and (max-width: 34em) {} /* Larger screen sizes */
Or this:
@media not screen and (min-width: 34em) {} /* Smaller screen sizes */
@media screen and (min-width: 34em) {} /* Larger screen sizes */
The ordering does not matter because not
makes these rules mutually exclusive — at least, in the browsers that understand the keyword, which as far as I know is all of them that support media queries anyway.
My answer to this question, which is a follow-up to the one linked above, offers another explanation:
How can I avoid media query overlap?
Can 2 CSS Media Query definition overlap
you can combine multiple media queries in a comma-separated list; if
any of the media queries in the list is true, the associated style
sheet is applied. This is the equivalent of a logical "or" operation.
https://developer.mozilla.org/en/CSS/Media_queries
So
This is the equivalent of a logical "or" operation
makes it look like only one match is applied.
http://reference.sitepoint.com/css/mediaqueries seems to confirm this as well.
If this is true (and it is untested by me), then I would create a common stylesheet to deliver all the common styles no matter what media query is matched and then have device independent stylesheets with additional styles for each targeted device.
Content overlapping on 480px media query
figured it out, since all i wanted to do was set a specific, unchanging height for the description cards, what I did was instead of adding a <div class="featureLocker">
inside the col-**-*
bootstrap divs, I added that class TO the col-**-*
bootstrap div, because, by only setting the height it wouldn't affect the horizontal responsiveness of those divs. This SEEMS TO BE the solution for this particular problem if someone is facing a similar issue. Thank you all for your help!
Related Topics
How to Change the Font-Size of a Select Option
Nesting Pseudo-Elements Inside Pseudo-Elements
Differencebetween :Before and ::Before
Huge Google Maps Controls (Possible Bug)
What Exactly Flex-Basis Property Sets
How to Apply a CSS Class to HTML.Actionlink in ASP.NET MVC
How to Make Select Element Be Transparent in Chrome
Custom Cursor Image Doesn't Work in All Ies
How to Use CSS to Position a Fixed Variable Height Header and a Scrollable Content Box
Why Don't CSS Resets Use '*' to Cover All Elements
Select Odd Even Child Excluding the Hidden Child
How Do Browsers Read and Interpret CSS
First-Child Full-Width in Flexbox
What Does '&.' in '&.Sub-Title' Indicates in SCSS
CSS Transform with Element Resizing