Note that there are some explanatory texts on larger screens.

plurals
  1. POInline SVG embedded CSS interpreted at page-level?
    primarykey
    data
    text
    <p>I've run across the issue that given an HTML page with several SVG files embedded inline in the page, the CSS in the SVG is interpreted at page-level, not inside the SVG object itself. For example:</p> <pre><code>&lt;body&gt; &lt;svg id="svg-one" style="width:150px; height:150px;"&gt; &lt;style type="text/css"&gt;rect { fill:green; }&lt;/style&gt; &lt;rect x="5" y="5" height="100" width="100" /&gt; &lt;/svg&gt; &lt;svg id="svg-two" style="width:150px; height:150px;"&gt; &lt;style type="text/css"&gt;rect { fill:red; }&lt;/style&gt; &lt;rect x="5" y="5" height="100" width="100" /&gt; &lt;/svg&gt; &lt;/body&gt; </code></pre> <p>Both rectangles end up red; the CSS in the second overrides the CSS in the first. </p> <p>My goal is for each SVG file to be able to be downloaded on its own and have its own styles inside it to display properly, independent of the others and to be displayed with the others. Given that the <code>id</code> of the <code>&lt;svg&gt;</code> element may change, the best option I thought of is to wrap the whole SVG contents in a group and modify the CSS appropriately:</p> <pre><code>&lt;body&gt; &lt;svg id="svg-one" style="width:150px; height:150px;"&gt; &lt;style type="text/css"&gt;#ultimate-green-ness rect { fill:green; }&lt;/style&gt; &lt;g id="ultimate-green-ness"&gt;&lt;rect x="5" y="5" height="100" width="100" /&gt;&lt;/g&gt; &lt;/svg&gt; &lt;svg id="svg-two" style="width:150px; height:150px;"&gt; &lt;style type="text/css"&gt;#ultimate-red-ness rect { fill:red; }&lt;/style&gt; &lt;g id="ultimate-red-ness"&gt;&lt;rect x="5" y="5" height="100" width="100" /&gt;&lt;/g&gt; &lt;/svg&gt; &lt;/body&gt; </code></pre> <p>This would allow the downloaded version of the SVG to exactly match the online embedded one, with the downside of there being an group added to every SVG.</p> <p>Is there a better way to isolate the embedded CSS of inline SVGs?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload