Last active
June 27, 2024 11:36
-
-
Save miladd3/17ed397bddd3d0333035c5a5d6c35a8a to your computer and use it in GitHub Desktop.
Javascript px to Rem function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts pixel size to rem and accepts the base as second argument. default base is 16px | |
* | |
* @param {number|string} px | |
* @param {number} base | |
* @return {string} | |
*/ | |
const remCalc = (px: number | string, base: number = 16) => { | |
const tempPx = `${px}`.replace('px', '') | |
return (1 / base) * parseInt(tempPx) + 'rem' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, sweet and simple, updated the code to this