0

I have a SVG image with some rect's that need to change colour when hovering over it. In jsfiddle the code works fine:

https://jsfiddle.net/o8ufaL0h/

But on my web page, I can't get hover or onmouseover to work on the single rects. However when I put a onmouseover on the full img element from within the html code, it does work, so I guess its not a z-index problem on the img element.

I really dont know why I can't target svg elements in my svg or css code.

<style>
    rect:hover
    {
        opacity: 0.5;
    }
</style>


<rect x="277" y="126" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="126" fill="#004D44" width="157" height="88"/>

<rect x="277" y="232" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="232" fill="#004D44" width="157" height="88"/>

<rect x="277" y="339" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="339" fill="#004D44" width="157" height="88"/>

</svg>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99

1 Answers1

0

try add attributes for each of rect tag(with javascript)

onmouseover="this.setAttribute('fill-opacity','.5')" 
onmouseout="this.removeAttribute('fill-opacity')"

or with css

<style>
rect:hover{
    fill-opacity:1
}
</style>