skip to Main Content

I’m trying to add the gradient on text for my svg image.

Here’s my svg image:

svg img

i’ve uploaded here in png format
That’s the way I’m trying to do that:

export const HawllIcon: React.FC<IconSvgProps> = ({
  size = 100,
  width,
  height,
  ...props
}: IconSvgProps) => (
  <svg
    height={size || height}
    preserveAspectRatio="xMidYMid meet"
    version="1.0"
    viewBox="0 0 800 500"
    width={size || width}
    {...props}
    xmlns="http://www.w3.org/2000/svg"
  >
    <defs>
      <linearGradient id="MyGradient" x1="0%" x2="100%" y1="0%" y2="100%">
        <stop offset="5%" stopColor="#ff9459" />
        <stop offset="95%" stopColor="#006fee" />
      </linearGradient>
    </defs>
    <g
      fill="url(#MyGradient)"
      stroke="none"
      transform="translate(0,500) scale(0.1,-0.1)"
    >
      <path
        d="M140 2490 l0 -700 195 0 195 0 0 275 0 275 275 0 275 0 0 -275 0
  -275 195 0 195 0 0 700 0 700 -195 0 -195 0 0 -260 0 -260 -275 0 -275 0 0
260 0 260 -195 0 -195 0 0 -700z"
      />
      <path
        d="M2181 3148 c-10 -24 -141 -320 -291 -658 -150 -338 -281 -634 -291
  -658 l-18 -42 201 2 201 3 203 505 203 505 16 -40 c9 -22 101 -249 204 -505
l187 -465 203 -3 c191 -2 203 -1 197 15 -4 10 -143 325 -309 700 l-302 683
-193 0 -193 0 -18 -42z"
      />
      <path
        d="M3133 3183 c3 -5 73 -219 157 -478 83 -258 155 -473 158 -477 4 -4
54 119 111 273 l104 281 -64 204 -63 204 -204 0 c-112 0 -201 -3 -199 -7z"
      />
      <path
        d="M3883 2490 l-231 -700 176 0 177 0 139 425 c77 234 143 421 146 415
4 -6 31 -89 61 -185 29 -96 57 -171 60 -167 3 4 48 121 98 261 l92 254 -62
198 -62 199 -181 0 -181 -1 -232 -699z"
      />
      <path
        d="M5055 3178 c-3 -7 -70 -215 -150 -463 -80 -247 -180 -553 -221 -680
  -40 -126 -74 -233 -74 -237 0 -4 82 -8 183 -8 l183 0 222 685 c122 376 225
691 228 700 5 13 -16 15 -180 15 -140 0 -188 -3 -191 -12z"
      />
      <path
        d="M5580 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
545 -195 0 -195 0 0 -700z"
      />
      <path
        d="M6800 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
545 -195 0 -195 0 0 -700z"
      />
    </g>
  </svg>
);

But I’m only achieving this:

enter image description here

And I’m trying to achieve that:

enter image description here

What do I need to do to get things done?

I’ve tried to:

  • change fill to almost every variation of this gradient,
  • change the gradientUnits both ways: objectBoundingBox & userSpaceOnUse and none of them does the thing correctly.

2

Answers


  1. You can use the letters in a mask, and mask off a rectangle that has the gradient fill.

    body {
      background: black;
    }
    <svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 800 500"
      xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="lg1">
          <stop offset="5%" stop-color="#ff9459" />
          <stop offset="95%" stop-color="#006fee" />
        </linearGradient>
        <mask id="m1">
          <g fill="white" stroke="none"
            transform="translate(0,500) scale(0.1,-0.1)">
          <path
            d="M140 2490 l0 -700 195 0 195 0 0 275 0 275 275 0 275 0 0 -275 0
      -275 195 0 195 0 0 700 0 700 -195 0 -195 0 0 -260 0 -260 -275 0 -275 0 0
    260 0 260 -195 0 -195 0 0 -700z"
          />
          <path
            d="M2181 3148 c-10 -24 -141 -320 -291 -658 -150 -338 -281 -634 -291
      -658 l-18 -42 201 2 201 3 203 505 203 505 16 -40 c9 -22 101 -249 204 -505
    l187 -465 203 -3 c191 -2 203 -1 197 15 -4 10 -143 325 -309 700 l-302 683
    -193 0 -193 0 -18 -42z"
          />
          <path
            d="M3133 3183 c3 -5 73 -219 157 -478 83 -258 155 -473 158 -477 4 -4
    54 119 111 273 l104 281 -64 204 -63 204 -204 0 c-112 0 -201 -3 -199 -7z"
          />
          <path
            d="M3883 2490 l-231 -700 176 0 177 0 139 425 c77 234 143 421 146 415
    4 -6 31 -89 61 -185 29 -96 57 -171 60 -167 3 4 48 121 98 261 l92 254 -62
    198 -62 199 -181 0 -181 -1 -232 -699z"
          />
          <path
            d="M5055 3178 c-3 -7 -70 -215 -150 -463 -80 -247 -180 -553 -221 -680
      -40 -126 -74 -233 -74 -237 0 -4 82 -8 183 -8 l183 0 222 685 c122 376 225
    691 228 700 5 13 -16 15 -180 15 -140 0 -188 -3 -191 -12z"
          />
          <path
            d="M5580 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z"
          />
          <path
            d="M6800 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z"
          />
        </g>
        </mask>
      </defs>
      <rect width="800" height="500" fill="url(#lg1)" mask="url(#m1)"/>
    </svg>
    Login or Signup to reply.
  2. gradientUnits="userSpaceOnUse"

    In fact you can achieve a continuous gradient across all <path> elements by applying gradientUnits="userSpaceOnUse". In your case you also need to scale the gradient via gradientTransform="scale(10)" to adjust the group transformation/scaling (currently 0.1/10%):

    <svg viewBox="0 0 800 500" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <linearGradient id="MyGradient" gradientUnits="userSpaceOnUse" gradientTransform="scale(10)">
          <stop offset="5%" stop-color="#ff9459" />
          <stop offset="100%" stop-color="#006fee" />
          </linearGradient>
        </defs>
        <g
          fill="url(#MyGradient)"
          stroke="none"
          transform="translate(0,500) scale(0.1,-0.1)"
        >
    <path
            d="M140 2490 l0 -700 195 0 195 0 0 275 0 275 275 0 275 0 0 -275 0
      -275 195 0 195 0 0 700 0 700 -195 0 -195 0 0 -260 0 -260 -275 0 -275 0 0
    260 0 260 -195 0 -195 0 0 -700z"
          />
          <path
            d="M2181 3148 c-10 -24 -141 -320 -291 -658 -150 -338 -281 -634 -291
      -658 l-18 -42 201 2 201 3 203 505 203 505 16 -40 c9 -22 101 -249 204 -505
    l187 -465 203 -3 c191 -2 203 -1 197 15 -4 10 -143 325 -309 700 l-302 683
    -193 0 -193 0 -18 -42z"
          />
          <path
            d="M3133 3183 c3 -5 73 -219 157 -478 83 -258 155 -473 158 -477 4 -4
    54 119 111 273 l104 281 -64 204 -63 204 -204 0 c-112 0 -201 -3 -199 -7z"
          />
          <path
            d="M3883 2490 l-231 -700 176 0 177 0 139 425 c77 234 143 421 146 415
    4 -6 31 -89 61 -185 29 -96 57 -171 60 -167 3 4 48 121 98 261 l92 254 -62
    198 -62 199 -181 0 -181 -1 -232 -699z"
          />
          <path
            d="M5055 3178 c-3 -7 -70 -215 -150 -463 -80 -247 -180 -553 -221 -680
      -40 -126 -74 -233 -74 -237 0 -4 82 -8 183 -8 l183 0 222 685 c122 376 225
    691 228 700 5 13 -16 15 -180 15 -140 0 -188 -3 -191 -12z"
          />
          <path
            d="M5580 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z"
          />
          <path
            d="M6800 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z"
          />
        </g>
     
      </svg>

    Combine paths to a single compound path.

    You can also combine all paths by concatenating all <path> elements’ d path data attribute values:

    <svg viewBox="0 0 800 500" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <linearGradient id="MyGradient">
          <stop offset="5%" stop-color="#ff9459" />
          <stop offset="95%" stop-color="#006fee" />
          </linearGradient>
        </defs>
        <g
          fill="url(#MyGradient)"
          stroke="none"
          transform="translate(0,500) scale(0.1,-0.1)"
        >
          <path
            d="M140 2490 l0 -700 195 0 195 0 0 275 0 275 275 0 275 0 0 -275 0
      -275 195 0 195 0 0 700 0 700 -195 0 -195 0 0 -260 0 -260 -275 0 -275 0 0
    260 0 260 -195 0 -195 0 0 -700z
               M2181 3148 c-10 -24 -141 -320 -291 -658 -150 -338 -281 -634 -291
      -658 l-18 -42 201 2 201 3 203 505 203 505 16 -40 c9 -22 101 -249 204 -505
    l187 -465 203 -3 c191 -2 203 -1 197 15 -4 10 -143 325 -309 700 l-302 683
    -193 0 -193 0 -18 -42z
               M3133 3183 c3 -5 73 -219 157 -478 83 -258 155 -473 158 -477 4 -4
    54 119 111 273 l104 281 -64 204 -63 204 -204 0 c-112 0 -201 -3 -199 -7z
               
               M3883 2490 l-231 -700 176 0 177 0 139 425 c77 234 143 421 146 415
    4 -6 31 -89 61 -185 29 -96 57 -171 60 -167 3 4 48 121 98 261 l92 254 -62
    198 -62 199 -181 0 -181 -1 -232 -699z
               
               M5055 3178 c-3 -7 -70 -215 -150 -463 -80 -247 -180 -553 -221 -680
      -40 -126 -74 -233 -74 -237 0 -4 82 -8 183 -8 l183 0 222 685 c122 376 225
    691 228 700 5 13 -16 15 -180 15 -140 0 -188 -3 -191 -12z
               M5580 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z
               M6800 2490 l0 -700 530 0 530 0 0 155 0 155 -335 0 -335 0 0 545 0
    545 -195 0 -195 0 0 -700z"
          />
        </g>
     
      </svg>

    You can further optimise this <> by converting the path data into relative commands and scaling the commands. (e.g. with svg-path-editor)
    This way you can avoid the `transform’ group altogether. Another advantage is that the svg becomes much more compact in file size.

    <svg viewBox="0 0 800 500" xmlns="http://www.w3.org/2000/svg">
        <defs>
          <linearGradient id="MyGradient">
          <stop offset="5%" stop-color="#ff9459" />
          <stop offset="95%" stop-color="#006fee" />
          </linearGradient>
        </defs>
          <path fill="url(#MyGradient)"
            d="M14 249v-70h20 19v28 27h28 27v-27-28h20 19v70 70h-19-20v-26-26h-27-28v26 26h-19-20v-70zm204 66c-1-3-14-32-29-66-15-34-28-63-29-66l-2-4 20 0 20 1 21 50 20 51 2-4c0-3 10-25 20-51l19-46 20-1c19 0 20 0 20 2-1 1-15 32-31 70l-30 68h-20-19l-2-4zm95 3c1 0 8-22 16-47 8-26 16-48 16-48 0-1 5 12 11 27l10 28-6 21-6 20h-21c-11 0-20 0-20-1zm75-69-23-70h18 18l13 43c8 23 15 42 15 41 0-1 3-9 6-18 3-10 6-18 6-17 0 0 5 12 10 26l9 25-6 20-6 20h-18l-18 0-24-70zm118 69c-1-1-7-22-15-46-8-25-18-56-23-68-4-13-7-24-7-24 0-1 8-1 18-1h19l22 69c12 37 22 69 23 70 0 1-2 1-18 1-14 0-19 0-19-1zm52-69v-70h53 53v16 15h-33-34v55 54h-19-20v-70zm122 0v-70h53 53v16 15h-33-34v55 54h-19-20v-70z"
          /> 
      </svg>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search