What is css?
The 'css' npm package is a powerful library for parsing, manipulating, and generating CSS. It allows developers to programmatically work with CSS stylesheets, making it easier to handle dynamic styling in web applications.
What are css's main functionalities?
Parsing CSS
This feature allows you to parse a CSS string into a JavaScript object. The parsed result includes detailed information about styles, rules, and selectors, which can be manipulated or queried.
const css = require('css');
const stylesheet = css.parse('body { font-size: 12px; }');
console.log(stylesheet);
Stringifying CSS
This feature converts a JavaScript object representing a CSS stylesheet back into a CSS string. This is useful for generating CSS styles dynamically based on logic within your application.
const css = require('css');
const obj = {
type: 'stylesheet',
stylesheet: {
rules: [{
type: 'rule',
selectors: ['body'],
declarations: [{
type: 'declaration',
property: 'margin',
value: '0 auto'
}]
}]
}
};
const stringifiedCSS = css.stringify(obj);
console.log(stringifiedCSS);
Manipulating CSS
This feature allows you to manipulate an existing CSS stylesheet by adding, modifying, or removing rules and declarations. This example demonstrates adding a new declaration to an existing rule.
const css = require('css');
const stylesheet = css.parse('h1 { color: red; }');
stylesheet.stylesheet.rules[0].declarations.push({
type: 'declaration',
property: 'font-size',
value: '20px'
});
const newCSS = css.stringify(stylesheet);
console.log(newCSS);
Other packages similar to css
postcss
PostCSS is a tool for transforming CSS with JavaScript plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more. It is more plugin-driven compared to 'css', offering a broader range of transformations.
sass
Sass (Syntactically Awesome Style Sheets) is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). It provides more features like variables, nested rules, and mixins compared to the basic CSS manipulations offered by 'css'.
less
Less is a backward-compatible language extension for CSS. This is very similar to Sass but uses a different syntax. It provides variables, mixins, and functions which allows you to make CSS that is more maintainable, themable, and extendable than the 'css' package.
css
CSS parser / stringifier using css-parse and css-stringify.
Installation
$ npm install css
Example
js:
var css = require('css')
var obj = css.parse('tobi { name: "tobi" }')
css.stringify(obj);
object returned by .parse()
:
{
"stylesheet": {
"rules": [
{
"selector": "tobi",
"declarations": [
{
"property": "name",
"value": "tobi"
}
]
}
]
}
}
string returned by .stringify(ast)
:
tobi {
name: tobi;
}
string returned by .stringify(ast, { compress: true })
:
tobi{name:tobi}
License
(The MIT License)
Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.