I have been trying to figure out whether it's possible to nest CSS feature queries (also known as “CSS @supports”) and regular media queries, and what would be the correct way to do it.
Example A shows the feature query inside the media query.
Example B shows the media query inside the feature query.
Is it even possible to nest them at all? If so, is there a preferred nesting style? A or B?
.foo { background: red; } /* EXAMPLE A */ @media (min-width: 50em) { .foo { background: green; } @supports (flex-wrap: wrap) { .foo { background: blue; } } } /* EXAMPLE B */ @supports (flex-wrap: wrap) { .foo { background: green; } @media (min-width: 50em) { .foo { background: blue; } } }
You can nest media queries in native CSS, as long as you're doing it from the root. It's funny to see in native CSS, but it works!
Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS. There is probably a point where the logic of that gets out of hand, so careful.
@media is the actually media query. The word screen is adding the 'conditions' to the media query. So @media screen is telling the media query to apply (whatever other conditions) to screens. For example, @media screen and (max-width: 360px) will target only screens with a max-width of 360px.
Both examples are valid CSS according to section 3 of the spec, and for the time being they seem to be consistently supported by browsers that understand both @supports rules and nested @media rules (also new to CSS3).
Although both examples will only apply the background: blue declaration when both the @media and @supports conditions are met,
background: green or background: blue if and only if the (min-width: 50em) media query is met;@supports and supports flex-wrap: wrap.Since your two examples mean subtly different things, which one you choose will depend on your use case:
@supports or flex-wrap: wrap to see either declaration, and to instead always apply background: red, choose B.background: green at the specified viewport width, even if it will never apply background: blue due to not supporting @supports or flex-wrap: wrap, choose A.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With