This Function lets you add CSS rules and it puts the rules on your stylesheet, but it has an appending problem. First here is the code then explanation that follows.
var addCSSRule = function(sheet, selector, rules){
//Backward searching of the selector matching cssRules
var index=sheet.cssRules.length - 1;
for(var i=index; i>0; i--){
var current_style = sheet.cssRules[i];
if(current_style.selectorText === selector){
//Append the new rules to the current content of the cssRule;
rules=current_style.style.cssText + rules;
sheet.deleteRule(i);
index=i;
}
}
if(sheet.insertRule){
sheet.insertRule(selector + "{" + rules + "}", index);
}
else{
sheet.addRule(selector, rules, index);
}
return sheet.cssRules[index].css;
}
addCSSRule(myStyleSheet, "#container", "height: 300px;");
addCSSRule(myStyleSheet, "#container", "width: 300px;");
addCSSRule(myStyleSheet, "#container", "border: 2px solid blue;");
addCSSRule(myStyleSheet, "#container", "background-color: red;");
Well to be fair, I didn't come up with this. I found this on a website http://davidwalsh.name/add-rules-stylesheets. However there is a problem with it. The rules append like they should, except for the first rule, well sort of. It appends okay, that is to say, the rule is recognized, but whenever you inspect element, its not with all the other rules. See picture below. Basically the first rule isn't appending right, like all the others.

for(var i=index; i>0; i--)only runs whileiis greater than zero, whereas it should run as long asiis greater-equal zero.