So I'm learning SVG animation.
Basically all I'm trying to do is change the color of a circle when it's hovered over.
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 437.1 294.4" style="enable-background:new 0 0 437.1 294.4;" xml:space="preserve">
<style type="text/css">
.st0:hover {
fill: red;
}
</style>
<g id="Circle">
<path class="st0" d="M291.3,147.4c0,77-62.4,139.4-139.4,139.4S12.5,224.4,12.5,147.4C12.6,70.4,75,8,151.9,8
C228.9,8,291.3,70.4,291.3,147.4"/>
</g>
</svg>
This works exactly as expected when the svg code is inside the html file.
However, when I put it inside an svg file and call it in using the img tag the hover effect no longer works.
<img class="logo" src="url/logo.svg">
Is there a way to do this without embedding the svg code inside the html?
Thanks!
I am writing this answer as a sort of applied version of Sean's answer. The following snippet shows varying forms of how you can utilize
<svg>
and what their capabilities are in each context.The main takeaway is that depending on the context, the
<svg>
may or may not receive style rules from the top level document and may or may not receive interactive events from the top level document (:hover
).I suggest you view the snippet below to see the applications in action:
<img>
:hover
background-image
<iframe>
inline
<svg>
Can't be done with the
<img>
tag. See: Styling And Animating SVGs With CSS. Near the bottom of the page of this article there's a table with the pros and cons of each SVG embedding technique (ie, img, object, etc.). I have reproduced the table here:*Only if inside
<svg>