Last active
June 3, 2024 09:05
-
-
Save kcak11/145ec4e271a9625b3d080a23c4df052d to your computer and use it in GitHub Desktop.
Dynamic CSS
A Pen by K.C.Ashish Kumar on CodePen.
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
| <div> | |
| Text with Dynamic CSS | |
| </div> |
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
| (function(w, d) { | |
| w.applyCSSRule = function(cssRuleText) { | |
| var ruleID = "k4nf0pmb665v079pfa495"; | |
| /*dcr --> dynamicCSSRule*/ | |
| var dcr = d.querySelector("style#" + ruleID); | |
| dcr && dcr.parentNode.removeChild(dcr); | |
| dcr = d.createElement("style"); | |
| dcr.id = ruleID; | |
| dcr.type = "text/css"; | |
| if (dcr.styleSheet) { | |
| dcr.styleSheet.cssText = cssRuleText; | |
| } else { | |
| dcr.appendChild(d.createTextNode(cssRuleText)); | |
| } | |
| d.querySelector("head").appendChild(dcr); | |
| }; | |
| })(window, document); | |
| /*------------------------------------------------*/ | |
| /*Use as below*/ | |
| var css ="\ | |
| body{\ | |
| background-color: rgb(255, 128, 0);\ | |
| }\ | |
| div{\ | |
| font-family: Verdana;\ | |
| color: #fff;\ | |
| }\ | |
| div:before{\ | |
| content:'Pseudo Text Before';\ | |
| color:#ffba75;\ | |
| font-size: 11px;\ | |
| }\ | |
| div:after{\ | |
| content:'Pseudo Text After';\ | |
| font-size: 11px;\ | |
| }"; | |
| applyCSSRule(css); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment