Macks
Macks

Reputation: 1776

HTML5 Inline SVG with external CSS

external CSS and inline SVG in a HTML5-document are really giving me a hard time.

I'm currently developing under the latest version of Chrome for Mac and wanted to get my SVG, which is styled with an external stylesheet, working for Firefox.

For Chrome, having my SVG-CSS inside the stylesheet for my webpage works fine. I just style all the html-elements and then the SVG-stuff. My stylesheet then looks something like this:

body {
  //blalba
}

<![CDATA[

line {
 //blabla
}

circle {
  //blabla
}

]]>

(works fine under chrome)

Now, if I go that route, all my SVG-elements in Firefox have a black fill, so this doesn't seem to work. Inline-CSS works fine inside the SVG, but I can't get external stylesheets to work within the SVG.

Here's the code I'm fiddling with:

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="styles/style.css" ?>
<svg width="827" height="185" viewBox="0 0 827 185" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect class="background" x="0" y="10" width="825" height="125" />
</svg>

I thought this was the correct way to do it, but it works neither in Chrome nor in Firefox. My stylesheet looks something like this:

<![CDATA[ // <- if tried leaving this out, no change

line {
 //blabla
}

circle {
  //blabla
}

]]> // <- if tried leaving this out, no change

I've also double-checked the path to my stylesheet. What am I doing wrong?

Thank you for your help! :)

Upvotes: 2

Views: 3179

Answers (1)

robertc
robertc

Reputation: 75777

If the SVG is inline in HTML, why not just use the link element like you would for any other stylesheet?

<link rel="stylesheet" href="styles/style.css">

Here's an example.

Upvotes: 4

Related Questions