Please stop using px for font-size.

94,180
0
Published 2024-07-22
🔥 Learn how to build a responsive website from a Figma design with HTML, SCSS, JS ➡️ coder-coder.com/responsive/

In this video, I'll show you why you should not use the px unit for font-size, width, and height of elements. The px unit is not affected by the browser's base font size, and so is not accessible for users who want to increase the font-size by using that browser setting. For accessibility, it's better to use rem or em units

0:00 - Intro
0:23 - The problem with px
3:28 - Don't use px for widths
5:32 - Don't use px in media queries
9:03 - Ways to convert from px to rems
12:20 - What about the 62.5% hack?

Resources mentioned in video:
🐙 GitHub repo: github.com/thecodercoder/stop-using-pixels
🖥️ MDN notes on pixels and font-size -- developer.mozilla.org/en-US/docs/Web/CSS/font-size…
↗️ WCAG Resize Text standards -- www.w3.org/WAI/WCAG22/quickref/#resize-text
✒️ Josh Comeau, "The Surprising Truth About Pixels and Accessibility" -- www.joshwcomeau.com/css/surprising-truth-about-pix…

____________________________

😎 Join the Coder Coder Club and get sneak peeks of videos: courses.coder-coder.com/p/club

👕 Get my hoodie here: coder-coder.com/merch

💻 Become a full-stack web dev with Zero to Mastery: academy.zerotomastery.io/a/aff_338z7xnj/external?a…

🎨 Get my VS Code theme: marketplace.visualstudio.com/items?itemName=CoderC…

🔽 FOLLOW CODER CODER
Twitter -- twitter.com/thecodercoder
Instagram -- www.instagram.com/thecodercoder


#css #javascript #html

All Comments (21)
  • @TheCoderCoder
    It is totally fine to use pixels for things like spacing: margin, padding, gap; as well as border and border-radius. In those cases it actually might be slightly better to use pixels, because having your spacing not scale up will leave more real estate on the page for the text, which can help with readability. But I think you can use either px or rems/ems for these cases and they are both fine solutions.
  • The main reason I started using em/rem is because it just looks more consistent since all of the font sizes are multiples of the base font size which makes the design of your site look more consistent. I had no idea about the accessibility benefits but I'm glad I do now! Great video :)
  • @dasten123
    For some reason, I have never even thought about using rem on media queries. But it makes total sense!
  • @TheCoderCoder
    🚨 I misspoke at 14:15 - 14:33 -- if you're using the 62.5% hack, I said that "everything on your website will be smaller," but what I meant was the 3rd party packages will be smaller on your website. And you may have to make their sizes LARGER (not smaller) to work on your website
  • @Crowmeir
    Oh my god, I was searching for how to use Rems and ems a few minutes ago and your video was on the suggested panel! I'm so happy I'm subscribed to this channel!
  • @john_elstar
    Happy 500k subscribers Coder Coder! <3 I'm so happy for you! :))
  • @jrom_ai
    Love this content 😍 Well done!
  • @najmantube
    I've been setting html {font-size: 10px} then body {font-size: 1.6rem} etc for a long time. After watching this, I set the browser font size to 'very large' and the font-size on my web page stayed the same! (as I'd predicted after watching your video). But, following up on an earlier comment on this thread, I went looking for a vscode extension and found one that works: "Convert px to rem" v0.0.10 - by gwanduke. So you could write your entire CSS file in px, then select all/some, then Ctrl+Shift+P, then type: convert px to rem of selections. PS. Excellent video, very informative!
  • @webdevsingh
    It's great to see videos on a tip like this! Thank you 😀
  • This really explained rem usage clearly and well... Thanks a lot 👍🏿😊
  • @zshn
    I really liked how you explained the merits. Very well done. On the topic of using var vs SCSS functions; I think the issue root vars solve is of distributed values being harder to modify / maintain / update. root variables help centralize the different values used across the system and SCSS functions are not a replacement to it. Even if you use a SCSS function to convert pixels to rem; you now have hundreds of places where magic numbers are used - 16, 32, 24, 1000, etc. As leads / reviewers; I'd like a more consistent model of defining & standardizing those values. So both root vars and SCSS functions can and should be used together. One (root vars) helps in make change configurable and centralized, the other (functions) makes it consistent and reliable.
  • @76248
    This is the best argument I have seen for useing rem over px. Great video
  • @hcmac100
    Thanks so much for your time and knowledge - total respect !
  • @AdeyCoder
    I like the content learned new. thank you & keep it up.
  • @cswalker21
    Thank you Coder Coder. CSS is my kryptonite so it's great to see videos with detailed explanations and examples. I'd had this vague understanding that I was supposed to be using ems instead of pixels but it's nice to have all the ins and outs. Outstanding video as usual.
  • @user-bf1pq5ht4y
    Great video! Your tips on responsive design and using rem units really helped me fix scaling issues. Thanks for the clear and practical advice!
  • @Kipex
    I've gone back and forth with this topic in the past, but I'm sticking to pixels these days. I do appreciate that your video covered the often forgotten media query aspect, which I think is important if you do end up using relative units. I disagree with calling font size based scaling a "modern approach", considering that's basically the original approach from before responsive design and variety of devices was even a concern. Zooming in its current implementation is a more recent addition and answer to user needs. It's more accessible to the user, has a default setting and easy on-the-fly adjustment. Zoom is consistent as everything scales equally. The WCAG criteria "1.4.4 Resize" that you referenced in the video, also supports zooming as a sufficient technique. In fact, if you were to take the rem approach and only apply it to font-sizes, you could easily end up failing that criteria due to clipped text in smaller areas. In this regard I think pixels are far simpler to work with, not to mention the work between designers and coders. I recognize that the font size setting still exists and that a select few may use it, but whichever your approach is, you have provided an accessible way to resize. Chances are many such users are already using the Scale setting in Windows for example as well. They have plenty of good options available :)
  • @GazeUponSwat789
    NIce!!! Thankyou!! You are the first Female coder on YouTube that I saw. I just Subcribed as well one again thankyou!!
  • @joseflinha
    1. Heading size in REM. Does having a full-screen title really improve accessibility? The size of the title itself is sufficient, there is no need to increase it further. So using REM matters. 2. Use REM to determine width. It makes sense in the demo, but if you're designing a page where you want 300 pixels, you really want 300 pixels there. The content part on the left may not fit what normally fits. So using REM matters. 3. Using EM in media queries also matters. If the desktop design is supposed to be two columns, why should it be affected by the font size? It works in some cases, but really everywhere? This way I might see the tablet version on my desktop with enlarged font. So use REM and EM, but wisely.