I wanted to accomplish the same thing (with variables and some processing logic in CSS) that pushed me to develop several stand-alone tools (one Angular and one V-JS):
Both of these toolkits allow you to do this in the STYLE tag or in an external * .css file:
/* <script src='external_logic.js'></script> <script> var mainColor = "#cccccc"; </script>*/ BODY { color: /*{{mainColor}}*/; }
And this is in your on-page style attributes:
<div style="color: {{mainColor}}" cjsss="#sourceCSS">blah</div>
(or for ngCss)
<div style="color: {{mainColor}}" ng-css="sourceCSS">blah</div>
NOTE. In ngCss you can also do $scope.mainColor instead of var mainColor
By default, Javascript runs in an isolated IFRAME, but in reality it is no different from how the browser handles your * .js files. Just remember to create your own CSS and host it on your own server, then XSS will not be a problem. But the sandbox provides much greater security and peace of mind.
CjsSS.js and ngCss are somewhere between other tools for performing similar tasks:
LESS , SASS, and Stylus are just preprocessors and require you to learn a new language and maim your CSS. They basically expanded CSS with new language features. All of them are also limited to plugins designed for each platform, while CjsSS.js and ngCss allow you to include any Javascript library through <script src='blah.js'></script> .
AbsurdJS saw the same problems with preprocessors and went in the opposite direction; Instead of extending CSS, AbsurdJS created a Javascript library to generate CSS without directly touching CSS.
CjsSS.js and ngCss took a middle position; you already know CSS, you already know Javascript, so just let them work together in a simple, intuitive way. CjsSS.js can be run on the server side (via Node.js) for CSS preprocessing, or both can be executed on the client side. You can also run in a hybrid way, when most variables are pre-processed and the rest are executed on the client side.
Campbeln
source share