Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS type casting? How to add "px" and "rem" using calc()?

Tags:

css

Assume I have variables with the following values: 32px and 1rem. I need to combine the two values and set, say, font-size in rem units. How is that done?

This does not work: font-size: calc(32px + 1rem);

In the scenario given, I actually want to set the font-size to 3rem. In my mind, the first step would be for me to convert 32px to its rem equivalent and then add the 1rem that I have (so, something like calc(32px / 16) + 1rem)). That doesn't work, either.

Is there a way to convert the 32px to its rem equivalent?

like image 288
StackOverflowNewbie Avatar asked Nov 15 '25 11:11

StackOverflowNewbie


1 Answers

For converting with calc simply multiply your conversion formula by 1 of the desired unit. Since you want to convert a pixel value to rem, you can do this: calc(32 / 16 * 1rem)

like image 90
Steve Avatar answered Nov 17 '25 09:11

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!