convert-css-length
Convert between css lengths e.g. em->px or px->rem
Last updated 6 years ago by kylemathews .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install convert-css-length 
SYNC missed versions from official npm registry.

convert-css-length

Convert between css lengths e.g. em->px or px->rem

Conversions between em, ex, rem, px are supported. PRs welcome if you need support for more esoteric length units.

[Note: algorithm was originally ported from Compass] (https://github.com/Compass/compass/blob/master/core/stylesheets/compass/typography/_units.scss)

Install

npm install convert-css-length

Usage

import convertLength from 'convert-css-length';

// Set the baseFontSize for your project. Defaults to 16px (also the
// browser default).
var convert = convertLength('21px');

// Convert rem to px.
convert('1rem', 'px');
// ---> 21px

// Convert px to em.
convert('30px', 'em');
// ---> 1.42857em

// Convert em to pixels using fromContext.
// em(s) are relative to the font-size at the same element. If you're setting an em on a element whose font-size
// is different than the base font size, you'll need to pass that font-size as the third parameter.
// Or just use rem instead which sizes everything relative to the base node.
convert('1em', 'px', '14px')
// ---> 14px

Current Tags

  • 2.0.1                                ...           latest (5 years ago)

5 Versions

  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Total 5
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (7)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE