I have a div called 'dynamic' with some text in it and I am adding style to it using javascript
var styleElement = document.createElement("style");
styleElement.appendChild(document.createTextNode("#dynamic { color: red; }"));
document.getElementsByTagName("head")[0].appendChild(styleElement);
It works fine. Same way, I am trying to add some css for scrollbar and we can achieve it by the css code below:
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);
}
So, now I added it using 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);
But this is not working as expected.
My question is:
For first scenario, it was working fine (adding color to "dynamic" div using javascript), But for second scenario, if I add css for scrollbar using javascript, its not working. If first scenario works, then the second scenario should also work.
So, whether I written wrongly or is there any other way to add css for webkit-scrollbar using javascript?
Here is the code jsFiddle