π Stop using pixels in CSS

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Should we also be using rems for margins, borders, padding, and basically everything else as well? Or just for font size?
I use it for font-size/margin
But for borders/border-radius and media-queries I still use pixels.
I'm actually not 100% sure on the "best" answer, but this is my gut feeling and it works well.
Chris Bongers I was thinking the same thing. Border radius would probably give some wacky results if using rems. And media queries probably wouldn't work very well either. Thanks for the response :)
Great article... π
I would also suggest everyone to check Responsive Font Size (a.k.a. RFS). It's a unit resizing engine which resizes fonts (and more) based on viewport width. Bootstrap also provides an option to enable it.
Awesome, that's some cool stuff!
I don't understand. If rem is directly proportional to px, what advantage does it possibly hold? I've been using px only for borders and %, vw & vh for the rest.
Good question Prateek Aher it comes down to accessibility.
So if you set your browser default to be 20px because of your bad eyes or whatever reason. rem is a way better solution, then us defining what would be best for you.
For borders and media queries however it is still a valid point of using px.
Also can't live without vw and vh :D
Yeah, it's the zooming (manually or by default) which wouldn't change using px values.
if rem is directly proportional to px, what advantage does it possibly hold
The answer is that it isn't directly proportional. It depends on the font size. The formulas and converter work under the assumption of default font size.
It's like those measuring cups that have kg and liters, do you have one in your kitchen? They only measure liters, so the kg is a misleading lie. But because most kitchen-liquids have density close to that of water, it's a sort-of-useful trick anyway.
Same with rem and px: most users will be using 16px = 1rem so it's a good starting point, but the whole reason rem is better is because sometimes 1rem != 16px.
Mark Exactly is based on the root font-size (HTML) so hence I included that in my calculator.
You must keep in mind screenreaders and some mobile devices have a bigger font-size by default.
People could also use accessibility tools that don't necessary use the zoom function.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I may be the worst advocate for actually doing this, but let me try and be a better person from now on.
And please, you too!
Let's all get away from pixels
But, don't take just my word for it:
From back in the days, we started using pixels for web development, and for a long time this was good enough.
So what makes them so bad you ask?
Well, we can choose between em and rem for font-size.
CSS3 came with rem which is a relative size to the root html element and is supported by 98,22% of the browsers!
So the cool part about this is people can have their default browser setting set to something else, then the defined 16px's, and we will just scale for them.
Side note: REM is also a cool band!
To calculate the pixels to rem we have to understand that the default font size for an HTML document is 16 pixels...
We as developers, should never update that. It's something the user can do.
So talking that in account it means that 1rem = 16px, and 2rem = 32px.
To make it super easy. I made this calculator for you guys:
Let us please all be aware this is the future πΈ.
The cursor is pretty well supported! The function has been around, some mobile browsers don't go about it, but you must see this as an "extra".

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter