Media queries with display-mode
Tags: css, mediaqueries, mediafeatures, displaymode
Published: 2022-01-15 09:52:26

---
title: Media queries with display-mode
published: true
date: 2022-01-12 16:35:38 UTC
tags: css,mediaqueries,mediafeatures,displaymode
canonical_url: https://adactio.com/journal/18762
---

It’s said that the best way to learn about something is to teach it. I certainly found that to be true when I was writing [the web.dev course on responsive design](https://web.dev/learn/design).

I felt fairly confident about some of the topics, but I felt somewhat out of my depth when it came to some of the newer modern additions to browsers. The last few modules in particular were unexplored areas for me, with topics like [screen configurations](https://web.dev/learn/design/screen-configurations/) and [media features](https://web.dev/learn/design/media-features/). I learned a lot about those topics by writing about them.

Best of all, I got to put my new-found knowledge to use! Here’s how…

[The Session](https://thesession.org/) is a progressive web app. If you add it to the home screen of your mobile device, then when you launch the site by tapping on its icon, it behaves just like a native app.

In [the web app manifest file for The Session](https://thesession.org/manifest.json), the `display-mode` property is set to “standalone.” That means it will launch without any browser chrome: no address bar and no back button. It’s up to me to provide the functionality that the browser usually takes care of.

So I added a back button in the navigation interface. It only appears on small screens.

Do you see the assumption I made?

I figured that the back button was most necessary in the situation where the site had been added to the home screen. That only happens on mobile devices, right?

Nope. If you’re using Chrome or Edge on a desktop device, you will be actively encourged to “install” The Session. If you do that, then just as on mobile, the site will behave like a standalone native app and launch without any browser chrome.

So desktop users who install the progressive web app don’t get any back button (because in my CSS I declare that the back button in the interface should only appear on small screens).

I was alerted to [this issue on The Session](https://thesession.org/discussions/46608#comment932251):

> It downloaded for me but there’s a bug, Jeremy - there doesn’t seem to be a way to go back.

Luckily, this happened as I was writing the module on [media features](https://web.dev/learn/design/media-features/). I knew exactly how to solve this problem because now I knew about the existence of [the `display-mode` media feature](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/display-mode). It allows you to write media queries that match the possible values of `display-mode` in a web app manifest:

`
.goback {
  display: none;
}
@media (display-mode: standalone) {
  .goback {
    display: inline;
  }
}
`

Now the back button shows up if you “install” The Session, regardless of whether that’s on mobile or desktop.

Previously I made the mistake of _inferring_ whether or not to show the back button based on screen size. But the `display-mode` media feature allowed me to test the _actual_ condition I cared about: is this user navigating in standalone mode?

If I hadn’t been writing about media features, I don’t think I would’ve been able to solve the problem. It’s a really good feeling when you’ve just learned something new, and then you immediately find _exactly_ the right use case for it!