I plan to animate SVG with CSS, which is loaded inside an object element, therefore exists in its own content document, therefore the linked CSS of my HTML document does not apply there.
Luckily SVG has its own way to include CSS. You just need to put a line like this before your root <svg>
element.
<?xml-stylesheet type="text/css" href="style.css" ?>
If I include this line manually and load the SVG with pure HTML inside an <object>
element, then my CSS rules will be realized on the SVG.
However, I tried to automate it on the client side by loading the SVG as a text, insert the stylesheet link, create a blob from the text, finally add the URL of the blob as the data attribute of a <object>
element. This displayed the image and presented the same DOM structure in the inspector as the pure HTML solution, but the CSS rules were not realized.
Even in the most simple case, when I load an SVG with JS, which already contains the stylsheet link, the rules won't be applied. Which is presented in my self-contained example on glitch.
What is the reason behind this? Is there a way to circumvent this limitation, without inserting the SVG directly inside the HTML?