I’m trying to create a heart-shaped button with this code:
MENU
.heart {
--c: red;
width: 200px;
aspect-ratio: 1;
background:
radial-gradient(at 70% 31%,var(--c) 29%,#0000 30%),
radial-gradient(at 30% 31%,var(--c) 29%,#0000 30%),
linear-gradient(var(--c) 0 0) bottom/100% 50% no-repeat;
clip-path: polygon(-43% 0,50% 91%, 143% 0);
transition: all .5s;
}
.heart:hover {
--c: blue;
width: 200px;
aspect-ratio: 1;
background:
radial-gradient(at 70% 31%,var(--c) 29%,#0000 30%),
radial-gradient(at 30% 31%,var(--c) 29%,#0000 30%),
linear-gradient(var(--c) 0 0) bottom/100% 50% no-repeat;
clip-path: polygon(-43% 0,50% 91%, 143% 0);
}
<div class="heart">
<button id="widget-toggle" class="widget-toggle-open drawer-toggle widget-toggle-style-default" data-toggle-target="#widget-drawer" data-toggle-body-class="showing-widget-drawer" aria-expanded="false" data-set-focus=".widget-toggle-close">
<span class="widget-toggle-label">MENU</span>
<span class="widget-toggle-icon">
</span>
</button>
</div>
But the no matter how I try it, the transition doesn’t work. Is there a way to make this and have the color change on hover have a duration?
Old posts said this may not have been possible. I did look around and didn’t see any recent Question/Answers that said this may have changed, So I’m hopeful that maybe there’s been some modernization?
2
Answers
You can use the @property css at-rule for that, but firefox has only partial support.
https://caniuse.com/?search=@property
Specs: https://developer.mozilla.org/en-US/docs/Web/CSS/@property
No, you cannot transition a background.
The simplest solution is to use an SVG element in your HTML. This is what SVG is good at: vector graphics. You then apply styles to that element using CSS.
But if you are stuck with HTML from somewhere else and therefore want to do it all in CSS, you could achieve it using two pseudo-elements and animating the
opacity
of one of them.