Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active October 24, 2016 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burdiuz/0f2e28342bfcf5c39567b1f210c82d58 to your computer and use it in GitHub Desktop.
Save burdiuz/0f2e28342bfcf5c39567b1f210c82d58 to your computer and use it in GitHub Desktop.
Simple class that uses getComputedStyle() to convert any CSS value unit into PX or integers/numbers.

CSSValue

Installation

via npm

npm install gist:0f2e28342bfcf5c39567b1f210c82d58 --save

via git

git clone https://gist.github.com/burdiuz/0f2e28342bfcf5c39567b1f210c82d58 CSSValue
class CSSValue {
static toPx(value) {
CSSValue._element.style.fontSize = value;
return window.getComputedStyle(CSSValue._element).fontSize;
}
static toInt(value) {
return parseInt(CSSValue.toPx(value));
}
static toNumber(value) {
return parseFloat(CSSValue.toPx(value));
}
}
CSSValue._element = (() => {
const el = document.createElement('span');
el.style.display = 'none';
document.body.appendChild(el);
return el;
})();
{
"name": "CSSValue",
"version": "0.0.1",
"main": "CSSValue.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment