Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change html svg cricle's fill with css when clicked on

Tags:

html

css

svg

I am using the following svg drawing as a button:

<svg id="button-more-people" style="width: 60px; height: 60px;">
    <circle cx="30" cy="30" r="30" fill="#F44336" id="circle-button-more-people"/>
    <text fill="#ffffff" font-size="45" x="11" y="44" font-family="Verdana">+</text>
</svg>

When the mouse hovers over the circle, I scale the button up through css:

#button {
    transition: 0.3s ease;
}

#button:hover {
    transform: scale(1.2);   
}

What I can't seem to achieve is to change the buttons color when its clicked on. I have tried the following, to no avail:

#button:active ~ circle {
    fill: #D50000;
}

I would prefer if there was a javascript-less solution to this I could use.

Thanks for any help in advance!

like image 874
zomnombom Avatar asked Sep 15 '17 23:09

zomnombom


People also ask

How do I change the styling of an SVG element?

SVG elements such as paths, circles, rectangles etc. can be targeted by CSS selectors and have the styling modified using standard SVG attributes as CSS properties. For example: circle { stroke-width: 20; stroke: #f00; fill: #ff0; }. This overrides any attributes defined within the SVG because the CSS has a higher specificity.

Is it possible to make an SVG element clickable using CSS?

But you DID say “clickable using CSS” specifically. That’s a little trickier. Usually we have :focus in CSS, but I don’t think there is any tried-and-true way to make an SVG element itself focusable. There was talk (long ago) of a focusable attribute, but I think that’s out.

How do I use an SVG file in HTML?

When used within an HTML <img> tag or CSS background-url, SVGs act identically to bitmaps: The browser will disable any scripts, links, and other interactive features embedded into the SVG file. You can manipulate that SVG using CSS in an identical way to other images using transform, filters, etc.

Why is my SVG icon not showing up in IE?

It fails in IE. CSS styling only applies to the <svg> element containing the <use>. The fill here makes every element of the icon the same color. To solve these issues, the SVG sprite can be embedded within page HTML then hidden using display: none or similar techniques.


1 Answers

The problem is in the selector used for the fill:

#button:active ~ circle {

This is the General Sibling Combinator which in your case matches <circle> tags that are siblings and after the #button, however in your markup <circle> is a child of #button

You can solve this by either using a descendant combinator:

#button:active circle {

Or a child combinator:

#button:active > circle {

Example:

#button-more-people {
  transition: 0.3s ease;
}

#button-more-people:hover {
  transform: scale(1.2);
}

#button-more-people:active circle {
  fill: #D50000;
}
<svg id="button-more-people" style="width: 60px; height: 60px;">
    <circle cx="30" cy="30" r="30" fill="#F44336" id="circle-button-more-people"/>
    <text fill="#ffffff" font-size="45" x="11" y="44" font-family="Verdana">+</text>
</svg>
like image 110
Isac Avatar answered Sep 20 '22 17:09

Isac