惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
S
Secure Thoughts
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
SecWiki News
SecWiki News
B
Blog
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
LINUX DO - 热门话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
F
Fortinet All Blogs
W
WeLiveSecurity
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
S
Schneier on Security
爱范儿
爱范儿
V
V2EX - 技术
L
LINUX DO - 最新话题
月光博客
月光博客
博客园 - 【当耐特】
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
U
Unit 42
Schneier on Security
Schneier on Security
E
Exploit-DB.com RSS Feed
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
博客园 - 叶小钗
T
Troy Hunt's Blog
大猫的无限游戏
大猫的无限游戏
AI
AI
Security Latest
Security Latest

Matthias Ott

Hello Again, World This, Still Not for Everyone The Shape of Friction WeissKlang L1 – Punching Above Its Weight Continvoucly Morged Value Webspace Invaders To Affinity and Beyond The Mystery of Storytelling Amateurs! Echoes of Connection Linear() Is Not (That) Linear View Transitions: The Smooth Parts Adding AVIF and WebP Support to My Craft CMS Site Challenge Acoustic Room Treatment and Building Sound Panels, Part 1: Planning Play On Overshoot The HTML Output Element Listening Closely Compressed Fluid Typography The Lifeblood of the Web What Could Go Wrong? That’s My Rank Making Space CSS :is() :where() the Magic Happens Visual Regression Testing for External URLs With Playwright Jane Goodall’s Famous Last Words European Tech Alternatives 🇪🇺 Independent Type Foundry Advent Calendar – Day 24: NaN Independent Type Foundry Advent Calendar – Day 23: Typotheque Independent Type Foundry Advent Calendar – Day 22: 205TF Independent Type Foundry Advent Calendar – Day 21: HvD Fonts Independent Type Foundry Advent Calendar – Day 20: Frere-Jones Type Independent Type Foundry Advent Calendar – Day 19: Fontwerk Independent Type Foundry Advent Calendar – Day 18: Vectro Independent Type Foundry Advent Calendar – Day 17: Studio René Bieder Independent Type Foundry Advent Calendar – Day 16: R-Typography Independent Type Foundry Advent Calendar – Day 15: David Jonathan Ross Independent Type Foundry Advent Calendar – Day 14: Interval Type Independent Type Foundry Advent Calendar – Day 13: Newglyph Independent Type Foundry Advent Calendar – Day 12: Swiss Typefaces Independent Type Foundry Advent Calendar – Day 11: Sharp Type Independent Type Foundry Advent Calendar – Day 10: Colophon Foundry Independent Type Foundry Advent Calendar – Day 9: Commercial Type Independent Type Foundry Advent Calendar – Day 8: Letters from Sweden Independent Type Foundry Advent Calendar – Day 7: Lineto Independent Type Foundry Advent Calendar – Day 6: Ohno Type Company Independent Type Foundry Advent Calendar – Day 5: Milieu Grotesque Independent Type Foundry Advent Calendar – Day 4: TypeMates Independent Type Foundry Advent Calendar – Day 3: Klim Type Foundry Independent Type Foundry Advent Calendar – Day 2: Dinamo Independent Type Foundry Advent Calendar – Day 1: Grilli Type The Independent Type Foundry Advent Calendar 2022 A Conversation With ChatGPT ChatGPT, please explain websites in the words of William Shakespeare Transient Frameworks Leaving Twitter Behind Converting Your Twitter Archive to Markdown The Wrong Question It Wasn’t Written Syndicating Posts from Your Personal Website to Twitter and Mastodon Suspension None of Your Business Doing Our Part Patch That Package Brain Dump Generating Accessibility Test Results for a Whole Website With Evaluatory The CSS Cascade, a Deep Dive Updates About Updates How to Delete Your Commit History in Git Unblocking Your Writing Blocks, Part 2: I’m Not an Expert nor a “Thought Leader” Connections No Wrong Notes Better Options Design Debt Finite and Infinite Games Don’t Assume, Validate. Necessity Is the Ultimate Teacher One Egg Go Deep There Is No Secret Code Balancing Risk Blue Eyes, Brown Eyes The Shortcut Boomerang My RSS Feed Collection of Personal Websites Frequency The Illusion of Control The Decisions Journey Write It Down Nownownow Into the Personal-Website-Verse Considering the Opposite What is it for? Unlimited Bowling. Never done. We Are Team Internet. We Need to Save #NetNeutrality. Progressive Search Data loss (also) by JavaScript Books I Will Definitely Maybe Read in 2017 Starting to Write Notes
Write Your Media Queries in Pixels, Not EMs
Matthias Ott · 2017-11-17 · via Matthias Ott
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

6 Likes

ⓘ Webmentions are a way to notify other websites when you link to them, and to receive notifications when others link to you. Learn more about Webmentions.