Support seems to be different across browsers..
Check the link
Firefox: Black with white text.
Opera, Chrome, IE9: Blue with black text.
Which is correct and how would I make it consistent?
The code
@media screen and (min-width: 480px) {
body{
background-color:#6aa6cc;
color:#000;
}
@media screen and (min-width: 768px) {
body{
background-color:#000;
color:#fff;
}
}
}
Interestingly enough it appears that nesting media queries within a conditional @import does seem to work.
e.g:
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Media test</title>
<link rel="stylesheet" type="text/css" href="importer.css" />
</head>
<body>
<h1>Why is this not consistent.</h1>
</body>
</html>
importer.css
@import url(media.css) screen and (min-width: 480px);
media.css
body {
background-color: #6aa6cc;
color: #000;
}
@media screen and (min-width:768px) {
body {
background-color: #000;
color: #fff;
}
}
For those simply looking for an answer to "Which browsers support nesting of
@mediarules?", the short answer is that all modern browsers, including Firefox, Safari, Chrome (and its derivatives), and Microsoft Edge, now support nesting of@mediaat-rules as described in CSS Conditional 3. The code in the question with the nested@mediaat-rules should now work correctly everywhere, with the exception of Internet Explorer (which is no longer being updated with new features, meaning no version of IE will ever support this feature).This feature did not exist in CSS2.1, since only media types existed at the time which you could simply group with a comma, which explains why support for this was very poor at the time this question was first asked.
What follows is an analysis of the original question with these historical limitations in mind.
There’s a bit of terminology confusion that needs clearing up in order for us to understand what exactly is happening.
The code you have refers to
@mediarules, and not so much media queries — the media query itself is the component that follows the@mediatoken, whereas the rule is the entire block of code consisting of@media, the media query, and the rules nested within its set of curly braces.This may cause confusion among many when it comes to using media queries in CSS, as well as your specific case where a
@mediarule in an imported stylesheet works correctly even when the@importis accompanied by another media query. Notice that media queries can occur in both@mediaand@importrules. They’re the same thing, but they’re being used to restrictively apply style rules in different ways.Now, the actual issue here is that nested
@mediarules are not valid in CSS2.1 because you’re not allowed to nest any at-rules within@mediarules. However, things seem quite different in CSS3. Namely, the Conditional Rules module states very clearly that@mediarules can be nested, even providing an example:Furthermore, it looks like Firefox is following this specification and processing the rules accordingly, whereas the other browsers are still treating it the CSS2.1 way.
The grammar in the Syntax module hasn’t been updated to reflect this yet, though; it still disallows nesting of at-rules within
@mediarules as with CSS2.1. This specification is slated for a rewrite anyway, so I guess this doesn’t matter.Basically, CSS3 allows it (pending rewriting the Syntax module), but not CSS2.1 (because it neither defines media queries nor allows nested
@mediarule blocks). And while at least one browser has begun supporting the new spec, I wouldn’t call other browsers buggy; instead, I’ll say that they simply haven’t caught up yet as they’re really conforming to an older, more stable spec.Lastly, the reason why your
@importworks is because@importis able to work conditionally with the help of a media query. However this has no relation to the@mediarule in your imported stylesheet. These are in fact two separate things, and are treated as such by all browsers.To make your code work consistently across browsers, you can either use your
@importstatement, or, since both of your rules usemin-width, simply remove the nesting of your@mediarules: