I am looking at this svg from wikipedia link – I have downloaded this to a simple html page locally. I noticed each area of the map had an id, so I thought I could use this to target the area and change the colour on hover. It sounded simple enough, but so far nothing.
First I tried to load the file as an object using <object type="image/svg+xml" data="map.svg"></object>
(which does display fine and does allow me to see the individual areas) but when I try to target this using the following CSS, nothing is happening:
#Queens path {
fill: yellow;
}
(I also tried path#Queens
as this is what is shown on dev tools when I target the area.
I’ve also tried brining the entire file into my html (which is messy) – but I am at a loss. Nothing is triggering a change on hover.
2
Answers
No need to use
<object>
, you can include the svg directly in the html file. Since it has inline styles for fill you’ll need to use!important
to override it on hover. Example (I’ve simplified the svg for characters limit):You cannot change the fill from the path without modifiying the svg
Why?
Because the path you are trying to target has his fill defined in his style
and according to the CSS Specificity
What if I still want to change it ?
You could either use
!important
(I recommend using it the least possible)or
You will have to extract the style from the style attribute
Example with smaller svg: