3

I'm trying to stack media queries in CSS, and can't seem to get it to work correctly. What i'm trying to achieve is having a single set of css rules for any of the media queries. I know normally I could use an and to have it apply to different rules, but in this case I'm trying to deal with vendor-specific prefixes so the browser won't satisfy all of the conditions.

Here's what I've got so far:

@media only screen and (min--moz-device-pixel-ratio: 2),
@media only screen and (-o-min-device-pixel-ratio: 2/1),
@media only screen and (-webkit-min-device-pixel-ratio: 2),
@media only screen and (min-device-pixel-ratio: 2) {
    /* My rules go here */
}

What is the correct way to stack rules like this? Also, if there is any further reading on this subject, I'd like to check it out.

1 Answer 1

6

The correct way is to have only one @media at the very beginning, followed by a comma-separated list of media queries:

@media only screen and (min--moz-device-pixel-ratio: 2),
       only screen and (-o-min-device-pixel-ratio: 2/1),
       only screen and (-webkit-min-device-pixel-ratio: 2),
       only screen and (min-device-pixel-ratio: 2) {
    /* Your rules go here */
}

@media is only permitted when declaring a distinct media rule, and is not used to separate multiple media queries in a single rule.

Sign up to request clarification or add additional context in comments.

2 Comments

Alright, thanks for the info - is there any documentation that I can read on this?
@Dan: Here's the Media Queries spec: w3.org/TR/css3-mediaqueries based on the CSS2 media types spec: w3.org/TR/CSS21/media.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.