Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

html - Is it possible to change svg (included as <img> element) styles from external css?

All the solutions I can find describe dirty hacks with replacing an <img> element with inline svg, or embedding css directly into the svg. Both doesn't suit me.

What I need: the ability to change styles (fill in particular) of the svg image included to the page as <img> element.

Example html:

<img src="https://dl.dropboxusercontent.com/u/4486681/rect.svg" class="recolor-me" />

CSS that I expected to work, but it doesn't

.recolor-me {
    fill: red;
}

Sandbox to play: http://jsfiddle.net/Q4Y6k/

So, is there any way to re-color the svg following the requirements from above (without using JS or inlining svg)?

PS: it's fine if the solution is chrome only

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, you cannot style an embedded resource with CSS code on the embedding page, even if the embedded resouce happens to be in an XML format (or HTML format – you cannot even style an HTML document embedded with iframe using CSS code in the embedding document).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...