javascriptjquerycsshtmlstyles

Define global CSS classes using JavaScript or jQuery?


Is there a way to set the CSS of global classes using JavaScript or jQuery? That is, append .my_class { foo:bar } to the <style/> tag of the page?


Solution

  • Pure javascript -

    var style=document.createElement('style');
    style.type='text/css';
    if(style.styleSheet){
        style.styleSheet.cssText='your css styles';
    }else{
        style.appendChild(document.createTextNode('your css styles'));
    }
    document.getElementsByTagName('head')[0].appendChild(style);