
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
css-custom-properties
Advanced tools
This module provides utilities to work with CSS custom properties in Javascript.
Install the package with the following command
npm install --save css-custom-properties
Example:
// Import using ES5 syntax
var CssCustomProperties = require('css-custom-properties');
// Import using ES6 syntax
import CssCustomProperties from 'css-custom-properties';
// Set a CSS variable
CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
});
// Get a CSS variable's value
console.log(CssCustomProperties.get('my-var'));
// => '16px'
// Get all CSS variables
console.log(CssCustomProperties.getAll());
// => {'my-var': '16px', 'my-other-var': 0.5}
// Check if a CSS variable has been set
console.log(CssCustomProperties.has('my-var'));
// => true
// Remove a CSS variable
console.log(CssCustomProperties.unset('my-var'));
// => '16px'
// Check if a CSS variable has been set
console.log(CssCustomProperties.has('my-var'));
// => false
The package exposes the following methods.
This method sets CSS variables on a DOM element.
Arguments
:root
element.Returns
collection
.Example
var variables = CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
'--my-prefixed-var': 'red',
});
console.log(variables);
// => {'my-var': '16px', 'my-other-var': 0.5, 'my-prefixed-var': 'red'}
This method gets a CSS variable's value on a DOM element.
Arguments
:root
element.Returns
variable
's value, else undefined
.Example
var myVar = CssCustomProperties.get('my-var');
console.log(myVar);
// => '16px'
var myNonExistantVar = CssCustomProperties.get('my-non-existant-var');
console.log(myNonExistantVar);
// => undefined
This method gets all CSS variables on a DOM element.
Arguments
:root
element.Returns
Example
CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
});
CssCustomProperties.set({
'another-one': 0,
});
var allVars = CssCustomProperties.getAll();
console.log(allVars);
// => {'my-var': '16px', 'my-other-var': 0.5, 'another-one': 0}
This method gets all CSS variables on a DOM element. Like getAll()
but with prefixed variable names.
Arguments
:root
element.Returns
Example
CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
});
CssCustomProperties.set({
'another-one': 0,
});
var allVars = CssCustomProperties.getAll();
console.log(allVars);
// => {'--my-var': '16px', '--my-other-var': 0.5, '--another-one': 0}
This method checks if a CSS variable exists on a DOM element.
Arguments
:root
element.Returns
true
if CSS variable exists on element
, else false
.Example
var myVarExists = CssCustomProperties.has('my-var');
console.log(myVarExists);
// => true
var myNonExistantVarExists = CssCustomProperties.get('my-non-existant-var');
console.log(myNonExistantVarExists);
// => false
This method removes a CSS variable from a DOM element.
Arguments
:root
element.Returns
Example
var variables = CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
});
var removedVar = CssCustomProperties.unset('my-other-var');
// => 0.5
var allVars = CssCustomProperties.getAll();
console.log(allVars);
// => {'my-var': '16px'}
This method removes all CSS variable from a DOM element.
Arguments
:root
element.Returns
Example
var variables = CssCustomProperties.set({
'my-var': '16px',
'my-other-var': 0.5,
});
var removedVars = CssCustomProperties.unsetAll();
// => {'my-var': '16px', 'my-other-var': 0.5}
var allVars = CssCustomProperties.getAll();
console.log(allVars);
// => {}
Adds the "--" prefix if it doesn't already exists.
Arguments
Returns
Example
CssCustomProperties.prefix('var');
// => '--var'
CssCustomProperties.prefix(['var', '--other-var']);
//=> ['--var', '--other-var']
CssCustomProperties.prefix({
'--var': 10,
'other-var': 'green',
});
//=> {'--var': 10, '--other-var': 'green'}
Trim the "--" prefix, if it exists.
Arguments
Returns
Example
CssCustomProperties.unprefix('--var');
// => 'var'
CssCustomProperties.unprefix(['var', '--other-var']);
//=> ['var', 'other-var']
CssCustomProperties.unprefix({
'--var': 10,
'other-var': 'green',
});
//=> {'var': 10, 'other-var': 'green'}
FAQs
Work with CSS Custom Properties in Javascript
The npm package css-custom-properties receives a total of 7 weekly downloads. As such, css-custom-properties popularity was classified as not popular.
We found that css-custom-properties demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.