{"id":1381,"date":"2014-05-28T15:28:09","date_gmt":"2014-05-28T13:28:09","guid":{"rendered":"http:\/\/blog.kodono.info\/wordpress\/?p=1381"},"modified":"2017-01-02T12:46:03","modified_gmt":"2017-01-02T11:46:03","slug":"dynamically-inject-a-css-code-into-the-page-javascript","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2014\/05\/28\/dynamically-inject-a-css-code-into-the-page-javascript\/","title":{"rendered":"Dynamically inject a CSS code into the page [JavaScript]"},"content":{"rendered":"<p>Mozilla created a function that permits to inject some CSS into the document: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleSheet.insertRule\">addStylesheetRules()<\/a><\/p>\n<p>But if you use jQuery you may want to use this function:<\/p>\n<pre class=\"brush:javascript\">\r\nfunction injectCSS(rule) {\r\n  $(\"head\").append('&lt;style>' + rule + '&lt;\/style>')\r\n}\r\n\r\n\/\/ example\r\ninjectCSS(\"div { padding:5px }\")\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mozilla created a function that permits to inject some CSS into the document: addStylesheetRules() But if you use jQuery you may want to use this function: function injectCSS(rule) { $(&#8220;head&#8221;).append(&#8216;&lt;style>&#8217; + rule + &#8216;&lt;\/style>&#8217;) } \/\/ example injectCSS(&#8220;div { padding:5px }&#8221;)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[15,13,33,26],"tags":[153,30,24,152,158,157],"class_list":["post-1381","post","type-post","status-publish","format-standard","hentry","category-astuce","category-niveau-intermediaire","category-programmation","category-web-design","tag-astuce","tag-css","tag-javascript","tag-niveau-intermediaire","tag-programmation","tag-web-design"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1381"}],"version-history":[{"count":4,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1381\/revisions"}],"predecessor-version":[{"id":1756,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1381\/revisions\/1756"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}