Comments on: Inject New CSS Rules https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Sun, 23 Dec 2018 07:55:40 +0000 hourly 1 https://wordpress.org/?v=6.1.1 By: infonota https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1654245 Sun, 23 Dec 2018 07:55:40 +0000 http://css-tricks.com/?page_id=19644#comment-1654245 IMPORTANT Add !important to rule
injectStyles(‘a:hover { color: red !important; }’);

]]>
By: Tabish https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1633731 Wed, 23 May 2018 06:59:13 +0000 http://css-tricks.com/?page_id=19644#comment-1633731 everytime we click a new div is appended in the body tag, how to remove the existing div and then append?

]]>
By: steve Veerhoff https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1613734 Tue, 12 Dec 2017 18:56:27 +0000 http://css-tricks.com/?page_id=19644#comment-1613734 In reply to Patrick Neal.

Thanks for this post! In response to Patrick , we are using this function to set defined styles, but the color and background-color are determined by a form input stored in a database. For example, we have a field named sub_unit, and the user determines the name of the sub-unit, which becomes a class. When the form is filled out, font color and background color are chosen by the person filling out the form and applied to the newly defined class.

Since we have no control over what the sub-unit name is, this snippet allows us to dynamically set CSS when the form data is read.

]]>
By: sgnl https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1613237 Wed, 22 Nov 2017 03:12:37 +0000 http://css-tricks.com/?page_id=19644#comment-1613237 In reply to Patrick Neal.

I like what Patrick has to say, and I like what Chris has to say as well. I think a good compromise for the article would be to mention both patterns and use cases with examples for future readers who may or may not be experienced with CSS.

]]>
By: Faisal https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1597143 Wed, 23 Sep 2015 11:05:45 +0000 http://css-tricks.com/?page_id=19644#comment-1597143 I have tried to improve upon this post’s code by creating the a js method that would add a style tag dynamically and rule(s) to document body or supplied container element.

See the link for the working example:

]]>
By: Garlaro https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1585735 Tue, 07 Oct 2014 07:45:57 +0000 http://css-tricks.com/?page_id=19644#comment-1585735 Thanks, very helpful!
I needed a “no framework” function, so here it is (based on all the above):
http://stackoverflow.com/a/26230472/2752979
code and demo

]]>
By: Danny Povolotski https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1137726 Thu, 06 Feb 2014 11:37:58 +0000 http://css-tricks.com/?page_id=19644#comment-1137726 Agreed. I’m actually quite sure that jQuery popularized a pretty bad approach to CSS styling (inline styles). It’s slow and doesn’t make sense if you think about it (applying CSS to a class doesn’t persist to new elements of that class).

CSS injection really seems to be the way to go.

I wrote veinjs just for this purpose:
https://github.com/israelidanny/veinjs

]]>
By: Elise Chant https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-1137090 Thu, 06 Feb 2014 03:12:45 +0000 http://css-tricks.com/?page_id=19644#comment-1137090 It would be better to give the div an id so you can dispose of it easily and instantiate it only once.

injectStyles: function(id, rule) {
    var $div = "<div id='"+ id +"'><style>"+ rule + "</style></div>";

    if ($('body ' + id).length === 0) {
        $($div).appendTo('body');
    }
}
]]>
By: Dr. Clue https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-435868 Wed, 08 May 2013 05:57:40 +0000 http://css-tricks.com/?page_id=19644#comment-435868 This one is actually even a little more fun as you can create pretty much any CSS you could create in a stylesheet, using any of the selectors the browser supports, and they can be

document.styleSheets[0].insertRule(“body {background:red;}”, 0);
document.styleSheets[0].deleteRule(0)

The 0 is the position index. I actually have a class scribbled up for this stuff that does things like detect and map standards CSS to vendor prefixed , produces re-usable CSS for CSS3D stuff like carousels , cubes , pyramids (with textures),manipulates keyframe rules etc. It will even tell me all the classes that effect an element , elements or even elements matching a regular expression query. The only reason this other stuff is mentioned is simply to point out just how much can actually be done working at the rule level

]]>
By: TeMc https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-248379 Fri, 04 Jan 2013 01:34:21 +0000 http://css-tricks.com/?page_id=19644#comment-248379 The <div> and &shy; (as opposed to just <style>) are an awkward but necessary evil. However beware of html: instead of text(). It means it isn’t escaped. This can be problematic if you use special characters in strings that are expected to be unescaped in CSS but the element will consider HTML if not escaped:

pre[data-foo="css"] {
    content: "</style>"
}

Also, you might want to keep the dom cleaner by using this:

function injectStyles(rule) {
  // Works in IE6
  var div = document.createElement('div');
  div.innerHTML = '&shy;<style>' + rule + '</style>';
  document.body.appendChild(div.childNodes[1]);
}

or:

function injectStyles(rule) {
  // Works in IE6
  var div = $("<div />", {
    html: '&shy;<style>' + rule + '</style>'
  }).children().appendTo("body");    
}
]]>
By: Chris Coyier https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-246526 Wed, 02 Jan 2013 02:43:52 +0000 http://css-tricks.com/?page_id=19644#comment-246526 In reply to Rodney Rehm.

Yeah it is, but there’s never been a browser where it didn’t work no matter where you put it. I think think it’s like 0.0001% safer to use body instead of head. Come to think of it, I think the safest possible way is to target the first script tag it can find (there must be one somewhere, since this is JavaScript running) and insert the style element after it.

]]>
By: Rodney Rehm https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-246263 Tue, 01 Jan 2013 21:04:40 +0000 http://css-tricks.com/?page_id=19644#comment-246263 Isn’t a style element supposed to go in head, since it’s metadata content and all. That is, unless the scope attribute is present… See style element

]]>
By: Chris Coyier https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-246096 Tue, 01 Jan 2013 16:56:46 +0000 http://css-tricks.com/?page_id=19644#comment-246096 In reply to Patrick Neal.

Possibly. But you just can’t know every possible situation. Like I mentioned above, what if you need to programmatically calculate what the style will be?

]]>
By: Patrick Neal https://css-tricks.com/snippets/javascript/inject-new-css-rules/#comment-246093 Tue, 01 Jan 2013 16:54:27 +0000 http://css-tricks.com/?page_id=19644#comment-246093 Good post, but wouldn’t it be preferable to simply add a class that solely changes the hover state? For instance add a class of .hoverChange, and in the CSS just add a rule like .hoverChange:hover{ color: red; }?

]]>