Last active
April 20, 2016 17:39
-
-
Save pachacamac/0d5be8b465c0ebc0fe51c13f8605f5f3 to your computer and use it in GitHub Desktop.
Simple Sass like variables in CSS through JavaScript. Obviously not a good idea for production but if you're just hacking a bit locally, why not?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style class='vcss'> | |
/* | |
$container-width: 1280px; | |
$main-font: 1rem 'Dosis', sans-serif; | |
$light: #fdfdfd; | |
$dark: #222; | |
*/ | |
@import url("http://fonts.googleapis.com/css?family=Dosis"); | |
body { background-color: $light; color: $dark; font: $main-font; } | |
.container { position: relative; text-align: left; width: $container-width; max-width: 90%; margin: 0 auto; } | |
</style> | |
<script> | |
(function(){ | |
var vars = {}, css = document.querySelectorAll('style.vcss'); | |
function vreplace(s){ for(var k in vars){ s=s.replace(new RegExp('\\$'+k+'(?!:)(?!-)\\b', 'gm'), vars[k])}; return s; } | |
for(var i=0; i<css.length; i++){ | |
css[i].textContent.replace(/^\s*\$(\w[\w\-\d]+):\s+(.*?);\s*(:?$|\/\/)/igm, function(m,k,v){ vars[k] = v; } ); | |
for(var k in vars){ vars[k] = vreplace(vars[k]); css[i].textContent = vreplace(css[i].textContent); } | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now also handles variables in variables, as long as they're defined in the right order. I.e.: