40

Can we use the ">" or "<" symbols(Greater than and Less than ) in media queries? for example I would like to hide a dive for all monitors less than 768px. can I say some thing like this:

@media screen and (min-width<=768px) {}

4 Answers 4

58

Media queries don't make use of those symbols. Instead, they use the min- and max- prefixes. This is covered in the spec:

  • Most media features accept optional ‘min-’ or ‘max-’ prefixes to express "greater or equal to" and "smaller or equal to" constraints. This syntax is used to avoid "<" and ">" characters which may conflict with HTML and XML. Those media features that accept prefixes will most often be used with prefixes, but can also be used alone.

So, instead of something like (width <= 768px), you would say (max-width: 768px) instead:

@media screen and (max-width: 768px) {}
5
  • 8
    The current draft does in fact allow ranges of this type. See drafts.csswg.org/mediaqueries/#mq-syntax
    – tremby
    Commented Sep 15, 2017 at 22:02
  • Media queries can use ordinary mathematical comparison operators in Chrome 104: developer.chrome.com/blog/new-in-chrome-104/#mq-math Commented Aug 2, 2022 at 18:59
  • 1
    @Adam Taylor: Yep. Turns out MQ3's concerns regarding HTML/XML conflicts were rendered moot by the living HTML checker going so far as to validate the media queries in the media="" attribute for you. (cont'd)
    – BoltClock
    Commented Aug 2, 2022 at 19:24
  • 1
    Compare valid media query result and invalid media query result.
    – BoltClock
    Commented Aug 2, 2022 at 19:24
  • 1
    @Adam Taylor: Don't know why I said HTML/XML, I'm sure you do still have to worry about encoding < and > within attribute values in XML mode (application/xhtml+xml, and any other XML-based language that would implement media queries).
    – BoltClock
    Commented Aug 3, 2022 at 4:16
17

Check out the Sass lib include-media, which (despite being for Sass) explains how calls like @include media('<=768px') maps to plain CSS @media queries. In particular, see this section of the docs.

TLDR, what you learn from there is:

To do the equivalent of something like media('<=768px') (less than or equal to 768) in CSS, you need to write

@media (max-width: 768px) {}

and to do the equivalent of media('<768px') (less than 768) in CSS, you need to write

@media (max-width: 767px) {}

Notice how I subtracted 1 from 768, so that the max width is less than 768 (because we wanted to emulate the < less-than behavior which doesn't actually exist in CSS).

So to emulate something like media('>768px', '<=1024') in CSS, we would write:

@media (min-width: 769px) and (max-width: 1024px) {}

and media('>=768px', '<1024') would be

@media (min-width: 768px) and (max-width: 1023px) {}
2
  • HOWEVER: If all you ever do in your media queries is override something set by a previous query then it's better to have no overlap and put @media (min-width: 768px) and (max-width: 1024px). Your next query would be @media (min-width: 1024px) and (max-width: 1280px) and will override everything as soon as it hits 1024. In that case if you're at 1024 both rules technically match but the last rule wins. It's very possible to hit a non integer width (1279.5) when your zoom level isn't at 100% so neither matches - and if your query includes display: none you're at risk of everything vanishing! Commented Jul 8, 2021 at 20:04
  • 1
    You may run into cases (and sorry I don't have an example) where you really don't want even a one pixel overlap - I suppose it would be if you're setting something in the first query you're not explicitly overriding in the second. In that case subtract .01 or something very small to give you @media (min-width: 768px) and (max-width: 1023.99px). I hate doing it but that's what I've had to do occasionally. Don't do this as a matter of routine unless you find you need to because it's SO ugly! Commented Jul 8, 2021 at 20:05
7

CSS Media Queries Level 4 specification comes with ">" or "<" symbols support in media queries.

So instead of:

@media screen and (max-width: 768px) { /* … */ }

You can now write:

@media screen and (width >= 768px) { /* … */ }

Check browser support here

Read more here.

3
  • 1
    I think for all new css designers this would be the preferred why to go. Why make things super complicated with max-width: and min-width: where everyone with basic math schooling knows that < and > mean.
    – theking2
    Commented Dec 29, 2022 at 9:44
  • Browser support link no longer works Commented May 22, 2023 at 17:53
  • As of today, Nov 21, 2023, this should be the accepted answer. Supported by all the major browsers since over a decade ago. Browser support link has been updated and support is broad, especially in the latest versions of the browsers.
    – Sideways S
    Commented Nov 21, 2023 at 19:20
6
@media screen and (max-width: 768px) { ... }
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.