Course contentsShow
← Web Development
Lesson 393 of 2,9827. Responsive DesignFree lesson

Combining Media Features with Logical Operators

Use 'and', 'or', and 'not' to create complex media queries targeting specific device ranges.

Combining Media Features with Logical Operators

You'll learn how to write sophisticated media queries that target precise device conditions using and, or (comma), and not operators.

The Core Idea

So far, you've written media queries with single conditions like @media (min-width: 768px). But what if you need styles for tablets in landscape mode only, or any device except small phones? Logical operators let you combine multiple media features to create precise targeting rules.

Think of it like building a fence with multiple gates: you can require visitors to pass through all gates (and), any gate (or), or block specific visitors entirely (not).

The Three Operators

and — All conditions must be true:

@media (min-width: 768px) and (max-width: 1024px) {
  /* Only tablets between 768px and 1024px */
}

Comma (,) — Acts as "or" — Any condition can be true:

@media (max-width: 480px), (orientation: portrait) {
  /* Small screens OR portrait mode */
}

not — Inverts the entire query:

@media not all and (min-width: 768px) {
  /* Everything EXCEPT devices 768px and wider */
}

Practical Examples

Target landscape tablets specifically:

@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
  /* Three conditions all must match */
}

Exclude very small devices:

@media not all and (max-width: 320px) {
  /* Apply to everything except devices 320px or smaller */
}

Key Takeaway: Logical operators (and, , for or, not) allow you to build complex, precise media queries by combining multiple device conditions, giving you fine-grained control over responsive breakpoints.