信息发布→ 登录 注册 退出

Animate SVG Path Color During Animation

发布时间:2026-01-12

点击量:

svg path elements lose their `fill` color during smil animation if the animated `` lacks an explicit `fill` attribute — even when the original path had one. the fix is to ensure every animated `` element (including those created dynamically or used in `` targets) explicitly declares `fill`.

When animating SVG paths using SMIL (e.g., with ), browsers do not inherit or preserve styling attributes like fill from the base element onto the animated version — especially when the animated is defined separately (e.g., as a second without fill). In your code, the first correctly has fill="#e67700" and fades in via opacity animation, but the second — the one driving the d-attribute animation — has no fill declared at all, causing it to render in the browser’s default black.

The solution is simple but critical:
Add fill="#e67700" (or your intended color) to every element involved in the animation — including those that only exist to animate d, opacity, transform, or other attributes.

✅ Corrected Example Snippet


  
  
    
  

  
  
        d="M -3.11,-5.41 Q ... Z">
    
  

⚠️ Key Notes

  • SMIL does not cascade styles: fill is not inherited from parent or computed from CSS unless explicitly set on the .
  • Dynamic generation? If your app generates these elements programmatically (e.g., via JavaScript), always inject fill into the element’s attributes before appending it to the DOM.
  • CSS fallback (optional): You can also apply path { fill: #e67700; } globally in a
  • Opacity + fill interaction: When animating opacity, ensure fill is present before opacity reaches 1; otherwise, the path may flash black before coloring.

By consistently declaring fill on all animated elements, you guarantee consistent, predictable color behavior across all modern browsers supporting SMIL — no more unexpected black paths.

标签:# css  # javascript  # java  # svg  # cad  # app  # red  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!