I have a div named 'dynamic' with text inside, and I am applying styling to it using javascript.
var styleElement = document.createElement("style");
styleElement.appendChild(document.createTextNode("#dynamic { color: red; }"));
document.getElementsByTagName("head")[0].appendChild(styleElement);
It is functioning properly. Similarly, I am attempting to add some CSS for the scrollbar, which can be achieved with the following CSS code:
div ::-webkit-scrollbar {
-webkit-appearance: none;
width: 7px;
}
div ::-webkit-scrollbar-thumb {
border-radius: 4px;
background-color: rgba(0,0,0,.5);
-webkit-box-shadow: 0 0 1px rgba(255,255,255,.5);
}
Now, I have added it through javascript like this:
var styleElement = document.createElement("style");
styleElement.appendChild(document.createTextNode("
div ::-webkit-scrollbar{
-webkit-appearance: none;
width: 7px;
}
div ::-webkit-scrollbar-thumb{
border-radius: 4px;
background-color: rgba(0,0,0,.5);
-webkit-box-shadow: 0 0 1px rgba(255,255,255,.5);
}
"));
document.getElementsByTagName("head")[0].appendChild(styleElement);
Unfortunately, this is not producing the expected result.
My query is:
In the first scenario, adding color to the "dynamic" div using javascript worked fine. However, in the second scenario, while attempting to add CSS for the scrollbar using javascript, it is not working. If the first scenario worked, then the second scenario should work as well.
So, did I write it incorrectly or is there another way to add CSS for webkit-scrollbar using javascript?
Here is the code in jsFiddle