Skip to content

Instantly share code, notes, and snippets.

@roshanca
Created November 23, 2020 13:13
Show Gist options
  • Save roshanca/5c58acd444f59d2203e902a4754cbce6 to your computer and use it in GitHub Desktop.
Save roshanca/5c58acd444f59d2203e902a4754cbce6 to your computer and use it in GitHub Desktop.
Set CSS pseudo-element styles via JavaScript #js #css #pseudo
// reference: https://stackoverflow.com/questions/4481485/changing-css-pseudo-element-styles-via-javascript/12207551#answer-8051488
var addRule = (function (style) {
var sheet = document.head.appendChild(style).sheet;
return function (selector, css) {
var propText = typeof css === "string" ? css : Object.keys(css).map(function (p) {
return p + ":" + (p === "content" ? "'" + css[p] + "'" : css[p]);
}).join(";");
sheet.insertRule(selector + "{" + propText + "}", sheet.cssRules.length);
};
})(document.createElement("style"));
addRule("p:before", {
display: "block",
width: "100px",
height: "100px",
background: "red",
"border-radius": "50%",
content: "''"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment