Write Your Media Queries in Pixels, Not EMs

Edit on 09-04-2022: This article is quite old and I have since realized that the conclusion I draw in the article was wrong. The upsides of using EMs as the unit in your media queries, especially in terms of accessibility, far outweigh the one downside I described in the post. So please use relative units for all media queries going forward!

In all of my latest projects, I was using em units for writing media queries, just like so:

@media (min-width: 30em) {
	/* CSS for viewports >= 480px */

I had switched to em units some time ago because writing pixel-based media queries seemed to be the inferior solution. At first, because browsers used to handle zooming really bad, like Lyza Gardner had shown in 2012 in her article The EMs have it: Proportional Media Queries FTW!. And although the zooming behavior has since been made consistent, another article made a strong case against using pixels for media queries: Zell Liew showed in a couple of Codepen experiments that although most browsers handled pixel-based media consistently, Safari was the party pooper that still didn’t handle pixel-based media queries properly.

But yesterday, I stumbled upon an article by Alastair Campbell‏ in which he states that the advice to not use pixels in media queries should be considered a myth. I got curious: Maybe the Safari bug from back then had been resolved in the meantime and using pixel-based media queries was safe now?

So I looked at Zell Liew's article again and tried to find the Codepen he had used. I could not find it but found one further down in the comments. It was by a user named Ola who repeatedly pointed out that Safari was indeed handling media queries in the wrong way – but contrary to what the article suggested, not the ones that used pixels were problematic, but the em and rem media queries.

So I looked into it and it indeed seems like he is right. Safari handles pixel-based media queries correctly when the user zooms. If, for example, the zoom level is at 125 %, a min-width:400px media query correctly “fires” at a width of 500 device pixels.

You can try it out yourself. Open this Codepen in Safari and change the zoom level to 125 %. Then change the width of the browser window until the media queries kick in. And while the pixel-based behaves correctly, the em-based media query is triggered at a width of 625 device pixels. Obviously, Safari multiplies the font-size as well as the value in the media query by 1.25:

16px * 1.25 * 25 * 1.25 = 625px

Alternatively, look at this test page with Safari and zoom in/out (and compare it to any other browser): http://output.jsbin.com/ubuvay/4. There's also a related Webkit bug report (41063).

So until this issue gets resolved, it seems like we have to change the recommendation regarding media queries once more: For the most consistent cross-browser behavior, write your media queries in pixels, not ems.

What do you think? Am I missing something here, maybe? And how do you write your media queries? If you write an answer and link back to this article, you can add it via the form below and it will appear in the Webmentions section. But you can also write via Twitter or email, of course. I'd be very happy to hear your opinion.


10 Webmentions


Media Queries, Frameworks & An Eye for Design

Media queries have been around for almost 10 years and now that I think about it, that’s a real long time, especially on the web. I still remember the first responsive site I’ve built in 2010 like it was last year. It was seven years that said website had been launched and was to be redesigned. It for sure served well, but I’ll keep that for another story. Back in 2010, we used to write our CSS from max-...