Last active
April 9, 2025 08:01
-
-
Save miladd3/17ed397bddd3d0333035c5a5d6c35a8a to your computer and use it in GitHub Desktop.
Javascript px to Rem function
This file contains hidden or 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' | |
} |
Simplified this a bit and made TS happy:
const remCalc = (px: number | string, base: number = 16) => { const tempPx = `${px}`.replace('px', '') return (1 / base) * parseInt(tempPx) + 'rem' }
Great, sweet and simple, updated the code to this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simplified this a bit and made TS happy: