Radial SVG gradient is not animating on hover

The radial gradient is supposed to grow from 10% to 100% on hover, but it’s just not doing anything. Can’t understand what I’m doing wrong.

<svg id="svgDoc" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" shape-rendering="geometricPrecision" text-rendering="geometricPrecision">

<style><![CDATA[
#svgDoc {
    pointer-events: all
}

#svgDoc:hover #svgGrad {
    animation: svgGrad_f_p 3000ms linear 1 normal forwards
}

@keyframes svgGrad_f_p {
    0% {
        offset: 10%
    } 
    100% {
        offset: 100%
    }
}
]]>
</style>

<defs>
<radialGradient id="svgGrad-fill" cx="0" cy="0" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox" gradientTransform="translate(0.5 0.5)">
<stop id="svgGrad-fill-0" offset="0%" stop-color="#ff0000"/>
<stop id="svgGrad-fill-1" offset="10%" stop-color="#000"/>
</radialGradient>
</defs>

<rect id="svgGrad" width="500" height="500" rx="0" ry="0" fill="url(#svgGrad-fill)"/>
</svg>

3

You can’t manipulate the stop attribute via CSS as it “collides” with the CSS offset-path related property of the same name.

Workaround 1: SMIL animations triggered by begin attribute

A workaround might be to animate the gradient via SMIL animation like so:

<h3>Hover me</h3>
<svg id="svgDoc" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
  <defs>
    <radialGradient id="svgGrad-fill" cx="0" cy="0" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox" gradientTransform="translate(0.5 0.5)">
      <stop id="svgGrad-fill-0" offset="0%" stop-color="#ff0000" />
      <stop id="svgGrad-fill-1" offset="10%" stop-color="#000" >
           <animate attributeName="offset" fill="freeze" values="0.1;1" dur="1s" repeatCount="1" begin="svgGrad.mouseover"  /> 
           <animate attributeName="offset" fill="freeze" values="1;0.1" dur="1s" repeatCount="1" begin="svgGrad.mouseout"  /> 
      </stop>
    </radialGradient>
  </defs>

  <rect id="svgGrad" width="500" height="500" rx="0" ry="0" fill="url(#svgGrad-fill)" />
</svg>

Fortunately, SVG’s <animate> element allows you to
add events to start (or stop) playback. See “mdn docs: begin”

<animate attributeName="offset" fill="freeze" values="1;0.1" dur="1s" repeatCount="1" begin="svgGrad.mouseout"  /> 

Adding transition easing

We need to calcMode="spline" and keySplines attributes to define a custom easing – similar to CSS easing:

  • Ease-in: keySplines="0.42 0 1 1"
  • Ease-in-out: keySplines="0.42 0 0.58 1"
  • Ease: keySplines="0.25 0.1 0.25 1"

Like the keyTimes attribute we can specify multiple values as an array separated by semicolons.
In your case we only have 2 animation states (begin:0.1/10% end: 1/100%) as descibed by the keyTimes attribute. Therefore we only need a single bézier value. Otherwise we need to repeat the keySplines according to the keyTimes where keySplines=keyTimes .length-1.

Here’s an example comparing the easing results

body{
font-size:3vmin;
display:grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
gap:1em;
}
<div class="col">
<h3>linear</h3>
<svg viewBox="0 0 500 500">
  <defs>
    <radialGradient id="svgGrad-fill" cx="50%" cy="50%" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox">
      <stop offset="0%" stop-color="#ff0000" />
      <stop offset="10%" stop-color="#000">
        
       <animate attributeName="offset" fill="freeze" values="0.1;1;0.1" keyTimes="0;0.5;1"  dur="1s" repeatCount="indefinite" begin="0" />

        <!--
        <animate attributeName="offset" fill="freeze" values="0.1;1" keyTimes="0;1" dur="1s" repeatCount="1" begin="svgGrad.mouseover" />
        <animate attributeName="offset" fill="freeze" values="1;0.1" keyTimes="0;1" dur="1s" repeatCount="1" begin="svgGrad.mouseout" />
-->
      </stop>
    </radialGradient>
  </defs>

  <rect id="svgGrad" width="500" height="500" rx="0" ry="0" fill="url(#svgGrad-fill)" />
</svg>
</div>


<div class="col">
<h3>Ease-in-out</h3>
<svg viewBox="0 0 500 500">
  <defs>
    <radialGradient id="svgGradFillEaseInOut" cx="50%" cy="50%" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox">
      <stop offset="0%" stop-color="#ff0000" />
      <stop offset="10%" stop-color="#000">
        
        <animate attributeName="offset" fill="freeze" values="0.1;1;0.1" keyTimes="0;0.5;1" calcMode="spline" keySplines="0.42 0 0.58 1; 0.42 0 0.58 1" dur="1s" repeatCount="indefinite" begin="0" />
        <!--
        <animate attributeName="offset" fill="freeze" values="0.1;1" keyTimes="0;1" calcMode="spline" keySplines="0.42 0 0.58 1" dur="1s" repeatCount="1" begin="svgGradEaseInOut.mouseover" />
        <animate attributeName="offset" fill="freeze" values="1;0.1" keyTimes="0;1" calcMode="spline" keySplines="0.42 0 0.58 1" dur="1s" repeatCount="1" begin="svgGradEaseInOut.mouseout" />
-->
      </stop>
    </radialGradient>
  </defs>
  <rect id="svgGradEaseInOut" width="500" height="500" rx="0" ry="0" fill="url(#svgGradFillEaseInOut)" />
</svg>
</div>


<div class="col">
<h3>Ease-in</h3>
<svg viewBox="0 0 500 500">
  <defs>
    <radialGradient id="svgGradFillEaseIn" cx="50%" cy="50%" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox">
      <stop offset="0%" stop-color="#ff0000" />
      <stop offset="10%" stop-color="#000">
        <animate attributeName="offset" fill="freeze" values="0.1;1;0.1" keyTimes="0;0.5;1" calcMode="spline" keySplines="0.42 0 1 1; 0.42 0 1 1" dur="1s" repeatCount="indefinite" begin="0" />

        <!--
        <animate attributeName="offset" fill="freeze" values="0.1;1" keyTimes="0;1" calcMode="spline" keySplines="0.42 0 1 1" dur="1s" repeatCount="1" begin="svgGradEaseIn.mouseover" />
        <animate attributeName="offset" fill="freeze" values="1;0.1" keyTimes="0;1" calcMode="spline" keySplines="0.42 0 1 1" dur="1s" repeatCount="1" begin="svgGradEaseIn.mouseout" />
-->
      </stop>
    </radialGradient>
  </defs>
  <rect id="svgGradEaseIn" width="500" height="500" rx="0" ry="0" fill="url(#svgGradFillEaseIn)" />
</svg>
</div>

<div class="col">
<h3>Ease</h3>
<svg viewBox="0 0 500 500">
  <defs>
    <radialGradient id="svgGradFillEase" cx="50%" cy="50%" r="0.5" spreadMethod="pad" gradientUnits="objectBoundingBox">
      <stop offset="0%" stop-color="#ff0000" />
      <stop offset="10%" stop-color="#000">
        
        <animate attributeName="offset" fill="freeze" values="0.1;1;0.1" keyTimes="0;0.5;1" calcMode="spline" keySplines="0.25 0.1 0.25 1; 0.25 0.1 0.25 1" dur="1s" repeatCount="indefinite" begin="0" />

        
        <!--
        <animate attributeName="offset" fill="freeze" values="0.1;1" keyTimes="0;1" calcMode="spline" keySplines="0.25 0.1 0.25 1" dur="1s" repeatCount="1" begin="svgGradEase.mouseover" />
        <animate attributeName="offset" fill="freeze" values="1;0.1" keyTimes="0;1" calcMode="spline" keySplines="0.25 0.1 0.25 1" dur="1s" repeatCount="1" begin="svgGradEase.mouseout" />
-->
      </stop>
    </radialGradient>
  </defs>
  <rect id="svgGradEase" width="500" height="500" rx="0" ry="0" fill="url(#svgGradFillEase)" />
</svg>
</div>

Workaround 2: CSS custom @property

By defining custom properties for start and end offsets we can also achieve a smooth gradient transition.

@property --offset1 {
  syntax: "<percentage>";
  inherits: false;
  initial-value: 0%;
}


@property --offset2 {
  syntax: "<percentage>";
  inherits: false;
  initial-value: 10%;
}


.gradient{
  background: radial-gradient( #FF0000 var(--offset1), #000 var(--offset2));
  transition:1s --offset1, 1s --offset2;
}

.gradient:hover{
  --offset1: 5%;
  --offset2:100%;
}
<h3>Gradient CSS</h3>
<svg id="svgDoc2" class="gradient" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500">
</svg>

This won’t work with CSS variables as we can’t transition the background property itself.

In this approach we’re applying a CSS gradient to the outermost (parent) SVG element – accepting CSS gradient styles just like a HTML element (unlike inner SVG elements).

However, the SMIL approach probably still offers the best cross-browser compatibility.

See also:

  • “mdn docs: @property”
  • “css-tricks: Using @property for CSS Custom Properties”
  • “CSS-tricks: A Guide to SVG Animations (SMIL)”
  • Temani Afif’s answer:”How to animate a radial-gradient using CSS?”

8

To animate the radial gradient in your SVG you need to ensure that the properties being animated are compatible with CSS animations. In SVG you cannot directly animate the offset property of gradient stops using CSS. Instead, you need to use SMIL (Synchronized Multimedia Integration Language) for animating $VG properties.

Here’s how you can modify your SVG to animate the radial gradient using SMIL:

<svg id="svgDoc" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 500 500" shape-rendering="geometricPrecision" text-rendering="geometricPrecision">

<defs>
  <radialGradient id="svgGrad-fill" cx="50%" cy="50%" r="50%" spreadMethod="pad" gradientUnits="objectBoundingBox">
    <stop id="svgGrad-fill-0" offset="0%" stop-color="#ff0000">
      <animate attributeName="offset" from="0%" to="10%" dur="3s" begin="svgDoc:hover" repeatCount="indefinite" />
    </stop>
    <stop id="svgGrad-fill-1" offset="10%" stop-color="#000">
      <animate attributeName="offset" from="10%" to="100%" dur="3s" begin="svgDoc:hover" repeatCount="indefinite" />
    </stop>
  </radialGradient>
</defs>

<rect id="svgGrad" width="500" height="500" rx="0" ry="0" fill="url(#svgGrad-fill)"/>
</svg>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật