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

推荐订阅源

Spread Privacy
Spread Privacy
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
H
Hacker News: Front Page
C
Cybersecurity and Infrastructure Security Agency CISA
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
AI
AI
G
GRAHAM CLULEY
IT之家
IT之家
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
D
Docker
Recent Announcements
Recent Announcements
O
OpenAI News
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
Troy Hunt's Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic

Adam Wathan's Blog

Composing the Uncomposable with CSS Variables Tailwind CSS: From Side-Project Byproduct to Multi-Million Dollar Business Persistent Layout Patterns in Next.js Going Full-Time on Tailwind CSS 2018 Year in Review The Trick to Understanding Scoped Slots in Vue.js Renderless Components in Vue.js CSS Utility Classes and "Separation of Concerns" The $61,392 Book Launch That Let Me Quit My Job
Don't Use Em for Media Queries
2017-11-30 · via Adam Wathan's Blog

Update: It looks like the bug in Safari has been fixed in Safari 15, so I wholeheartedly recommend using em for media queries now. Keeping the post up for historical reasons, but the conclusion is no longer valid.

In March 2016, Zell Liew published a really great in-depth post titled "PX, EM, or REM Media Queries?", where he shared detailed tests he had performed to figure out which unit was the best choice for media queries and ultimately recommended em units.

I've always used pixels for media queries, so although I was pretty convinced I should switch after reading Zell's post, I wanted to do a bit of testing on my own to make sure I really understood the problems with pixels before I renounced them forever.

The Problem

The problem Zell discovered in his tests is that when you zoom in on Safari, px media queries and em media queries trigger at different sizes.

Here I've got two containers set to change their background color at 400px and 25em respectively, with the zoom set to normal (100%):

In this example you can see that the backgrounds change at the same time, because 25em at the default font size of 16px matches the 400px breakpoint.

In this example, I'm using the same containers and breakpoints, but Safari is zoomed to 150%:

Link to Fiddle (don't forget to zoom!)

Here it's clear that Safari is triggering the pixel breakpoint much sooner than it triggers the em breakpoint.

The Mistake

In Zell's post, he concluded that Safari was not scaling the pixel breakpoint as the user zooms, and that when using pixels, Safari was triggering the breakpoint too soon.

But let's measure when the actual breakpoints are triggered:

Interesting! Safari is triggering the 400px breakpoint at 600px when zoomed to 150% (the correct behavior) but it's triggering the 25em breakpoint at 900px, which makes no sense at all.

So it turns out that yes, there is a bug in Safari, but it's not with how it handles px media queries, it's with how it handles em media queries.

When zoomed in, Safari triggers em breakpoints much later than it should.

Practical Consequences

Imagine a tiled layout where the number of columns changes based on the viewport width.

Here's what it might look like at around 960px wide:

Here's what happens when I zoom in to 150% in Safari using pixel media queries:

The layout drops to two columns, like you'd see at 640px viewport size.

Now look what happens when I zoom in to 150% in Safari using em media queries:

We're seeing the single column layout, even though the text is rendering at exactly the same size as it was when zoomed in using pixel media queries!

Again, this is because Safari is triggering the breakpoint far too late when using em units.

For good measure, here's what things look like at 100% and 150% using both px and em units in Safari, Chrome, and Firefox:

One of these things is not like the other 🤔

Just Use Pixels

Pixels are the only unit that behave consistently across all commonly used browsers.

"But what if the user has changed their browser's default font size?"

In this case, your breakpoints will trigger differently than you designed for, and yeah it does have the potential to make things uglier than you planned. Usually this manifests itself as shorter line lengths in multi-column layouts or elements wrapping that you didn't intend to wrap because the user is using a larger default font size.

Personally I try to just not worry about it too much. I maintain sites with millions of unique visitors each month and haven't once had someone report a layout issue because of this, so even though this certainly sucks, it seems like in practice it's not causing sites to become truly unusable.

If you really can't stand the idea of this type of issue being possible in your project, one option is to set an explicit root font size in pixels, but be warned that this will override the user's custom default font size, which is frustrating if someone has changed that because they find your typical website too hard to read. I think it's probably better for those people to experience a couple of minor layout issues than be forced to struggle to read the content on your site because the font size is too small for them.

With any luck we'll see this issue in Safari get resolved at some point, and we can revisit using em for media queries and hopefully avoid this unfortunate trade-off.