Your daily frequent11y newsletter, brought to you by @ChrisBAshton:
The Surprising Truth About Pixels and Accessibility
Josh Comeau dispels the myths around using px
, rem
and em
in your designs. He introduces these with no assumed knowledge, gradually explaining when to use each one, using inline interactive examples to aid learning. It’s really worth checking out.
My takeaways:
- A reminder that
px
doesn’t actually map to hardware pixels, but it’s the most concrete measurement we have. - There are two main ways a user can make the text on their screen bigger: “zooming” (
⌘
++
on MacOS,ctrl
++
on Windows/Linux), or “font scaling” (changing the browser’s default font size). - When deciding whether to use pixels or rems, ask “Should this value scale up as the user increases their browser’s default font size?” If yes, use
rem
, if no, usepx
. So you’d userem
for text, but you might usepx
for borders or padding. - Josh recommends using
rem
overem
due toem
‘s compounding issues (making it hard to predict what size an element actually ends up being). An exception is for the margins of headings and paragraphs, for which Josh usesem
.
It gets more complicated: should you use px
or rem
in your media queries? The answer is rem
. If a user has increased their default font size, the available space is reduced, so they’re more likely to need your ‘mobile’ view. Using a rem-based media query such as @media (min-width: 50rem)
means a user who increases their default text size will need a bigger screen to get the non-mobile styles. (If a user sets their default text size to 32px, this is “double the standard text size” and means that “50rem will now be equal to 1600px instead of 800px”).
Josh goes into more examples. Be careful when using rem
to define button sizes, as declaring something like width: 15rem
could mean the button becomes too large for its container. You can combine this with max-width: 100%
to prevent the overspill. Similarly, use min-height
as opposed to height
.
Josh ends the article by discussing the numerous ways you can manage your rem
size to px
conversions, e.g. through defining a 62.5% base font size, using calc
, or leveraging CSS variables.
Prefer longer newsletters? You can subscribe to week11y, fortnight11y or even month11y updates! Every newsletter gets the same content; it is your choice to have short, regular emails or longer, less frequent ones. Curated with ♥ by developer @ChrisBAshton.