I try to add a CSS style sheet to an SVG. So what I have is the following svg:
<svg>
<defs id="mydefs">
</defs>
<rect class="myclass" x="10" y="10" width="20" height="20"/>
</svg>
And I would like to add a style sheet to this like:
<svg>
<defs id="mydefs">
<style type="text/css">
.myclass {
fill: red;
}
</style>
</defs>
<rect class="myclass" x="10" y="10" width="20" height="20"/>
</svg>
I tried
var defs = document.getElementById('mydefs');
var style = document.createElementNS("http://www.w3.org/2000/svg", 'style');
style.type = 'text/css';
style.insertRule('.myclass { fill: red; }', style.cssRules.length);
defs.appendChild(style);
But the insertRule (and cssRules) seems not to be supported. Here is the (not working) codepen: