#515·snapdom

linearGradient stop-color with CSS var() produces black output in screenshot

Author: Yuan-ZWCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbug✅ solved

Describe it simply:

svg 中的渐变色使用 CSS var(),截图的结果呈现为黑色

Quick demo to reproduce

xml
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>SnapDOM CSS Variable</title>
    <style>
      :root {
        --circle-color: #e65332;
        --gradient-start: #f2a65a;
        --gradient-end: #3459c7;
        --div-background: #536c46;
      }

      #capture {
        width: 320px;
        padding: 16px;
        background: #f4eddf;
      }

      #capture svg {
        display: block;
        width: 100%;
      }

      #capture .variable-div {
        padding: 16px;
        background: var(--div-background);
        color: white;
      }

      #result img {
        max-width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="capture">
      <svg viewBox="0 0 240 120">
        <defs>
          <linearGradient id="circle-gradient">
            <stop offset="0%" stop-color="var(--gradient-start)" />
            <stop offset="100%" stop-color="var(--gradient-end)" />
          </linearGradient>
        </defs>
        <circle cx="60" cy="60" r="45" fill="var(--circle-color)" />
        <circle cx="180" cy="60" r="45" fill="url(#circle-gradient)" />
      </svg>
      <div class="variable-div">DIV background: var(--div-background)</div>
    </div>

    <button id="capture-button" type="button">Capture</button>
    <p>Result:</p>
    <div id="result"></div>

    <script src="https://unpkg.com/@zumer/snapdom@latest/dist/snapdom.js"></script>
    <script>
      document.querySelector("#capture-button").addEventListener("click", async () => {
        const image = await window.snapdom.toPng(document.querySelector("#capture"));
        document.querySelector("#result").replaceChildren(image);
      });
    </script>
  </body>
</html>
Image

如图所示,上方为DOM元素内容,下方为截图的结果

Anything else we should know?

chrome 153.0.8010.53 @zumer/[email protected]