Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts style object into the corresponding CSS `style` attribute value
/**
* Takes a style object and returns the corresponding
* attribute value. Converts camel case property names
* to proper CSS selector names.
* @param {Object} obj Map of CSS properties to values.
* @return {string} The style attribute value.
*/
function toStyleAttribute = function(obj) {
return Object.keys(obj).map(function(key) {
// Camel case property names to CSS selector names.
return (key.replace(/([A-Z])/g, '-$1').toLowerCase()) +
':' + obj[key];
}).join(';');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment