Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

styling SVG groups using CSS classes

Tags:

css

svg

I would like to highlight SVG groups using CSS. I have tried the following code

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <style type="text/css">
    * {stroke: black; fill: white}
    .A:hover {fill: orange}
    .B:hover {fill: blue}
  </style>
  <g class="A">
    <circle cx="100" cy="100" r="50" />
    <circle cx="250" cy="100" r="50" />
  </g>
  <circle class="B" cx="400" cy="100" r="50" />
</svg>

but the first two circles do not turn orange upon hover (in Safari and Opera). What am I doing wrong?

like image 652
u003f Avatar asked Oct 19 '25 03:10

u003f


1 Answers

Fill will get set on the hovered <g>, but the other selector overrides the fill on the circles.

Replace:

.A:hover {fill: orange}

With:

.A:hover * {fill: orange}

See fiddle.

like image 183
Erik Dahlström Avatar answered Oct 21 '25 19:10

Erik Dahlström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!