Generate CSS from a JavaScript Object
This module does not convert property names to their dasherized counterparts, this is just a plain object to CSS stringification, though see property
and value
options below.
Install to-css
using npm:
npm install --save to-css
var toCss = require('to-css');
toCss({body: {'font-size': '10px'}}, {indent: ' '});
/**
* body {
* font-size: 10px;
* }
*/
Sometimes you want to have a CSS declaration with the same property specified multiple times with different values, for fallback values. You can use arrays for that, e.g:
var toCss = require('to-css');
toCss({body: {color: ['rgba(0,0,0,.5)', 'black']}}, {indent: ' '});
/**
* body {
* color: rgba(0,0,0,.5);
* color: black;
* }
*/
Defining multiple @font-face
's can be done using arrays like this:
var toCss = require('to-css');
toCss({
'@font-face': [
{'font-family': '"MyWebFont"', src: 'url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff")'},
{'font-family': 'MyOtherFont', src: 'url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff")'}
]
});
/**
* @font-face {
* font-family: "MyWebFont";
* src: url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff");
* }
* @font-face {
* font-family: "MyOtherFont";
* src: url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff");
* }
*/
Or like this:
var toCss = require('to-css');
toCss([
{
'@font-face': {
'font-family': '"MyWebFont"',
src: 'url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff")'
}
},
{
'@font-face': {
'font-family': 'MyOtherFont',
src: 'url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff")'
}
}
]);
/**
* @font-face {
* font-family: "MyWebFont";
* src: url("myfont.woff2") format("woff2"), url("myfont.woff") format("woff");
* }
* @font-face {
* font-family: "MyOtherFont";
* src: url("otherfont.woff2") format("woff2"), url("otherfont.woff") format("woff");
* }
*/
Name | Type | Description |
---|---|---|
object | `Object | Array` |
options | Object |
Options |
Returns: String
, the generated CSS string.
Type: String|Number
Default: ""
Works like JSON.stringify's space parameter. I.e. if it's a number it indicates the number of spaces to use as white space, and if it's a string the string itself is used as white space. When empty (or NULL
) no white space is used.
Type: Function
Default: NOOP
A transform function for property values, gets called for each CSS rule with value and property as params: options.value(value, property)
. Can return a String
or an array of strings!
Type: Function
Default: NOOP
A transform function for property names, gets called for each CSS rule with property and value as params: options.property(property, value)
. Can return a String
or an array of strings!
Type: Function
Default: NOOP
A transform function for selectors, gets called for each CSS declaration with selector and declaration object as params: options.selector(selector, declaration)
. Can return a String
or an array of strings!
MIT © Joakim Carlstein