Media queries are some of the best features cascading style sheets have to offer. They let us alter the styles applied to our documents based on the device viewing them, and the context theyโre viewed in. And every so often, we get new ones.
The Media Query Level 5 spec is being drafted as we speak, and it includes some really nice ones. Some of them are still being figured out, but several of them are available in some browsers.
This article will take you through some of the most interesting new media queries in this new specification, and show you how to use them.
This feature isnโt available in any browsers at the time of writing, but it definitely sounds like a future favorite. With the light-level media query, you can tune your styles based on whether your user is viewing your web app outside in daylight, or perhaps checking in before going to bed. This is great news for anyone who has ever tried to read their phone in the park, or check out a website at night!
There are three available values โ dim, normal (the default), and washed. Hereโs an example where we change some CSS custom properties:
@media (light-level: dim) { :root { --text-color: white; --background-color: black; } }
Read more at MDN.
Before the time of dark mode, a lot of people turned on the โinvert colorsโ to get that โdark modeโ feel. It looked pretty neat, but it also screwed with images, text shadows, and the way fonts were rendered (white on black has more perceived contrast than black on white for some reason).
The inverted-colors media query lets you adapt around those quirks! Although currently supported in Safari (and Safari on iOS), I sure hope this lands as well.
This is a boolean option, with two values none and inverted. But you can just skip the value altogether and write something like this:
@media (inverted-colors) { img { filter: invert(1); } * { box-shadow: none !important; text-shadow: none !important; } }
Neat, huh? Sounds like something that could be added to your base CSS sheet at some point right now!
The fifth level of CSS media queries also has a huge focus on personalization. It introduces no fewer than five distinct media queries that lets you tweak your website to whatever the user prefers!
You might actually have heard about the prefers-color-scheme media query already. This one lets you react to whether the user has turned on so-called โdark modeโ on their device. In other words โ adding โdark modeโ to your app is now a few lines of code!
This feature is already widely supported in browsers, and has three possible values โ light, dark, and no-preference. Hereโs an example where we change the background color of the site based on preference:
@media (prefers-color-scheme: dark) { body { background: #1e1e1e; color: white; } }
One of the things I love with these new media queries, is their focus on accessibility features. The prefers-contrast media query lets you cater to users who prefer high contrast content compared to your original design.
Thereโs two values here โ no-preference and high. Thereโs some discussion in the draft document about splitting high into increased and extremely-high, but nothing is certain yet. Thatโs probably the reason why thereโs no support for this one yet. But when they decide, youโll know what to write!
@media (prefers-contrast) { :root { --text-color: black; } }
Some users arenโt fans of animations and transitions. To some unlucky few, these kinds of movements can make them physically sick! Thatโs why most devices now support a way to tune down the transitions you find in most modern UIs.
With the prefers-reduced-motion media query, you can respect that wish as well. Use it to reduce those โbouncyโ animations, fading images and โfunโ transitions a bit. Note that you donโt necessarily have to remove all movement, but reduce it.
Browser support for this one is pretty good already, and is a โbooleanโ value too โ no-preference or reduce. Hereโs an example:
@media (prefers-reduced-motion) { * { transition-duration: 0.05s; } }
Some operating systems offer an option to reduce the amount of translucent layering effects used by the system. Although not supported by any browsers yet, the prefers-reduced-transparency media query is aiming to help you cater to those users.
This is another boolean value โ no-preference and reduce, so when it does get shipped in some browsers, you can write code like this:
@media (prefers-reduced-transparency) { .floating-box { opacity: 1; } }
Now this one is pretty exciting. If youโre running low on cellular data, or if youโre traveling internationally, it sucks to hit an image-heavy site. Well, not anymore, thanks to the fabulous prefers-reduced-data media query!
Thereโs no support for this yet, but itโs one of the most practical applications of media queries Iโve seen. With this, you could skip that huge header image or high-res profile images when theyโre not needed. I sure hope it gets implemented soon.
The value is boolean as well, so no-preference and reduce are the values. Hereโs an example:
@media (prefers-reduced-data) { .hero-image { background-image: none; background-color: salmon; } }
Read more about it in the spec (thereโs no MDN page for it yet!).
The last media query introduced in the level 5 spec is definitely the most powerful one โ scriptable, customizable media queries.
The main purpose seems to be avoiding repetition by creating a custom media query that maps to longer media queries.
Theyโre specified with the new @custom-media keyword, and it can look like this:
@custom-media --medium-devices (min-width: 50rem); @custom-media --large-landscape (min-width: 70rem) and (orientation: landscape); @media (--medium-devices) { .container { max-width: 40rem; } }
The CSS Working Group is also planning on a way to make these values scriptable, which is going to really bring super powers to media queries. Think how you can write styles when you can have @media (โlogged-in) or @media(โno-connection) media queries?
There are no browsers out there implementing this yet, but you can use a PostCSS plugin to use at least the @custom-media part of the spec.
Media queries have been one of the most useful features CSS ships with, but this fifth level of the specification sure seems to bring incredible strides. Please note that many of these media queries are not implemented yet, and are still considered an editorโs draft. That means youโll probably end up changing your implementation once or twice in the coming years, or perhaps scrapping it altogether.
On the plus side, media queries gracefully degrade into nothing, which means you can try adding them today, without breaking things for users not running the latest developer preview of your browser of choice.
As web frontends get increasingly complex, resource-greedy features demand more and more from the browser. If youโre interested in monitoring and tracking client-side CPU usage, memory usage, and more for all of your users in production, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording everything that happens in your web app, mobile app, or website. Instead of guessing why problems happen, you can aggregate and report on key frontend performance metrics, replay user sessions along with application state, log network requests, and automatically surface all errors.
Modernize how you debug web and mobile apps โ start monitoring for free.
Would you be interested in joining LogRocket's developer community?
Join LogRocketโs Content Advisory Board. Youโll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowBuild scalable admin dashboards with Filament and Laravel using Form Builder, Notifications, and Actions for clean, interactive panels.
Break down the parts of a URL and explore APIs for working with them in JavaScript, parsing them, building query strings, checking their validity, etc.
In this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]
5 Replies to "New media queries you need to know"
These sound good but how good will they actually be in practice? For example, I don’t have any problems with animations (things can fade in or slide in and it does not bother me), but I absolutely hate repeating animated GIFs or animations. I have to attempt to scroll them off the screen or cover them up with my hand to be able to read the surrounding content. Setting prefers-reduced-motion is an all or nothing proposition. I have to lose all animation to turn off the things that actually bother me. And how does one set these preferences? How many users know that they have to go into their OS settings and turn off all animations (at least on Windows) to set prefers-reduced-motion? I guess we will see once they go into common use.
I hear what you’re saying, I can definitely see the light-level and prefers-reduced-data media queries as being very cool features in the long-run
You really missed forced-colors here. It’s the “extrem” Dark mode for people the eye sight issues.
Hello, Kristofer Selbekk
My name is Richie Pu, a Web UI developer from Tencent Game Platform and Publish Design Center (short for P&P Design) .
we volunteer to run a site in Wechat which other designers in China can subscribe it to get the latest news or articles about the Game Design.
When we read the article “New media queries you need to know” , we think it should also be read by Chinese Designer & Web Developer. However we have to translate the English version to Chinese firstly by ourselves. This why we send you an E-mail to ask your permission of the translation right here.
Any question can reply to me. Hope to hear your voice.
Thank you!
Hi Richie, LogRocket editor here. Thanks for the love. For now, our policy is that we do not approve translations on third-party sites.