Search code examples
svggradientmarker

How to apply a gradient to a marker?


In SVG, how to apply the gradient applied to a line to its marker-end ?

<?xml version='1.0' encoding='UTF-8' standalone="yes"?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     width="820px"
     height="500px"
     viewBox="0 0 820 500"
     version="1.1" >
  <style>
    .axis3 {
         stroke-width: 40px;
         marker-end:url(#arrow);
         stroke: url('#gradient_3');
         fill: url('#gradient_3');
         height: 30px;
    }
    .axis4 {
         stroke-width: 40px;
         marker-end:url(#arrow);
         stroke: url('#gradient_4');
         fill: url('#gradient_4'); /* corrected */
         height: 30px;
    }
  </style>
  <defs>
    <marker
       id="arrow"
       markerWidth="20"
       markerHeight="40"
       refX="0"
       refY="20"
       orient="auto"
       markerUnits="userSpaceOnUse"
       style="fill:inherit;">
      <path style="stroke:none;fill:inherit;overflow:visible;" d="M0 0 L20 20 L0 40 Z" />
    </marker>

    <linearGradient id="gradient_3" x1="0%" y1="0%" x2="100%" y2="0%" gradientUnits="userSpaceOnUse" >
      <stop offset="0%"  stop-color="yellow" />
      <stop offset="20%" stop-color="red" />
    </linearGradient>
    <linearGradient id="gradient_4" x1="0%" y1="0%" x2="100%" y2="0%" gradientUnits="userSpaceOnUse" >
      <stop offset="0%"  stop-color="blue" />
      <stop offset="20%" stop-color="green" />
    </linearGradient>
  </defs>

  <line class="axis3" x1="50" x2="400" y1="50" y2="50" />
  <line class="axis4" x1="50" x2="400" y1="100" y2="100" />
</svg>

With the code above, marker is always black.

As there are several elements line with different gradients, gradient can not be applied directly on the path. I tried to add style="fill:inherit" - with no success.


Solution

  • Answer to "Make marker-end same color as path?" mentions that there is no inheritance of colors from related path. Situation has not evolve since this answer.