Maybe sometimes you should use pixels
The conventional wisdom is that you should not use pixel units, but maybe sometimes you should?
When I posted the early stages of my redesign at the end of February, someone on Mastodon said I should change the CSS of my main container to use em
units instead of px
because itās āmore accessible.ā
About that.
Iāve been doing this web development thing for a bit now, and sure, Iāve heard that line for years. Believed and used it, too.
The conventional wisdom is that you should convert all of your absolute units (like pixels) to relative units like em
or rem
so people who like to use a larger font size wonāt have their choices overridden. And weāve been doing that for years. Iāve even made a few SASS mixins in my time to automatically convert pixel values into rem
units, and have for many years diligently converted every px
in my codebase to rem
. Even the border widths.
1px
is 0.0625rem
, by the way.
But I hate putting tools out of use without a super good reason, and so Iāve been wondering ā is there a place for pixels?
I think there is. In fact, I think pixels work best under most circumstances.
Iāll spoil the ending for you: relative sizes are great for fonts and line-spacing, but for other elements like padding, margin, borders, and layout sizes, pixels may actually be preferable.
Thatās because browsers offer two different ways of scaling fonts.
A Tale of Two Zooms
Some time ago āĀ quite some time ago, in fact ā browsers standardized on āpage zoom.ā If you use zoom right now in Safari, Firefox, or Chromium browsers (Arc, Edge, Chrome, Opera) you will notice everything on this page gets bigger, despite the fact that Iāve used a mixture of px
and rem
. Zoom in far enough on the desktop and you will start to see the mobile layout of this site.
All of the browsers determined that zooming in and out was a critical enough accessibility issue that it couldnāt be left up to the entire Web to retrofit font sizes to px
or rem
. So: setting margins, padding, and font sizes in pixels doesnāt disable zoom anymore, and it hasnāt in a long time.
On the other hand, browsers let you set the default base font size. āNormalā here is 16px
(with some exceptions).

Here in the browser appearance settings in Chrome you can set the base font size independently of page zoom.
Chrome has their font size settings buried pretty deep in the settings. Safari binds text-only size increases to āā„ +
/ āā„ -
. Firefox has a āzoom text onlyā toggle in the zoom menu. If youāre so inclined you can try those out now here on this page. What you should see is that changing the font size alone makes the text on this page bigger or smaller, but the layout doesnāt change otherwise.
This text, on the other hand, ignores the font size changes because Iāve set the font size in pixels. So it remains true that setting the font size in pixels overrides the userās expressed preferences. The standard page zoom, on the other hand, wonāt make this paragraph look any different from the others.
Update March 16, 2024: Nicolas Hoizey pointed out that Firefox zooms text even if the size is set in pixels. This is also the case in Safari. So to see the behavior I described above, you have to be using Chrome (or its derivatives). Interestingly, the only thing this effectively changes is that if Chrome adopts this strategy, too, even sizing fonts in rem
or em
will become an academic / purity exercise.

Hereās what it looks like on my computer if I set the font size setting to āvery largeā in Chrome.
The font sizes set in rem
or em
scale, but the paragraph set in px
stays the same
regardless.
This behavior is possible here because Iāve set margins, padding, and responsive container sizes controlling layout to rely on pixel values, while keeping font size and line heights relative.
If, however, you follow the conventional wisdom (āpx
units are evilā), the end result is that changing the base font size alone works exactly the same way as page zoom already works.
You are re-implementing page zoom, which is pointless. But you are also removing the ability for users to change font size independently.
Where does this leave us? Well.
- Sizing everything using
px
satisfies WCAG 2.1 1.4.4 through technique G142 as long as people use one of the very common browsers with āpage zoom.ā People who want to resize text independently are just out of luck. - Sizing everything using
rem
satisfies the same requirement through technique C28 and technique G146, but since thatās already met by browser page zoom, resizing text independently is impossible. - Mixing
px
andrems
deliberately satisfies accessibility requirements through technique G179, as long as access to all of the content and functionality remains available. This approach does not interfere with the native browser support offered through page zoom. So itās the only choice that supports both page and text-only zoom on Chrome.
The third option is the most difficult. If you go this route, you have decide what should scale with font size and what shouldnāt. Thatās many new judgement calls and a lot more testing. Maybe you donāt want or canāt support that. But it does preserve your visitorsā ability to scale text independently of your layout.
It also demonstrates why following conventional wisdom banning specific practices without regard for context has unintended consequences and leads to pointless complexity.
That, at least, is my thinking for this project. Am I wrong? Perhaps I am wrong. Maybe conventional wisdom is conventional for good reason, and my attempt to support text-only preferences is the real waste of time and resources. Iād love to hear your opinions on the matter, so let me know on Mastodon.
Add a comment