2

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! :)

1
  • 1
    CDATA sections in a stylesheet are typically not necessary. Commented Jul 3, 2012 at 19:09

1 Answer 1

4

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.

1
  • I assumed that because I had my SVG-styles in one file with my HTML-styles, that the reason it didn't work was because I had to include the css xml-style. This works just fine, thank you very much! :)
    – Macks
    Commented Jul 3, 2012 at 13:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.