unit-to-px

Convert any CSS unit (pt, em, rem, pc, in, mm, cm, vw...) to px, in browser

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unit-to-px
1701.0.56 years ago7 years agoMinified + gzip package size for unit-to-px in KB

Readme

Unit To Px
Convert any CSS unit (pt, em, rem, pc, in, mm, cm, vw...) to px, in browser
Build Status npm Standard - JavaScript Style Guide
Install

NPM

npm install -S unit-to-px

UNPKG

http://unpkg.com/unit-to-px
Usage
Only one top level API: fn (cssLength: string) -> px: Number

  1. Caculate px per unit

const toPx = require('unit-to-px')

console.log(toPx('mm'))  //3.779375
console.log(toPx('rem')) //16
console.log(toPx('em'))  //14
console.log(toPx('in'))  //96
console.log(toPx('vh'))  //2.48
console.log(toPx('??'))  //0 -> unknown unit always be 0!

  1. Convert px of length

const toPx = require('unit-to-px')

console.log(toPx('210mm'))  //793.6687499999999
console.log(toPx('- 210 m m'))  //throw TypeError: Error parsing length