M
M
Maxim2020-11-14 03:14:49
Google Chrome
Maxim, 2020-11-14 03:14:49

Why doesn't gradient apply in chrome svg?

There are several svg images, you need to apply a gradient to them. In FireFox, everything works fine and images are filled with a gradient, but in Chrome, some svg are partially or not applied at all. Here's what happens (the same gradient is applied to all svgs):

5faf26c99d73a548315777.png

Same thing in FireFox:

5faf26d3b3440095756210.png

https://codepen.io/mavsan/pen/GRqPPop?editors=1000

Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RAX7, 2020-11-14
@mavsan

It looks like chrome calculates the dimensions of the gradient based on the dimensions of the svg where the gradient was defined, not the dimensions where it was used. The default svg is 300x150 and the value of the gradient attribute is y1="100%"calculated as y1="150px". Next, this gradient is applied to svg with viewBox="0 0 10187 8873", as a result, the gradient has a height of 150 units, and the height of the svg itself is 8873 units, which is why the icon has id='a'a thin blue strip ~1.7% high (150/8873 * 100 = 1.69052%) . For clarity, you can add the spreadMethod="repeat" attribute to the gradient, which will cause some icons to become striped.
How to fix?The obvious way is not to use the value of the x1, x2, y1, y2 attributes as a percentage. You need to fit all the icons to the same viewBox size, for example 256x256 and set attributes for the gradienty1="256" x2="0"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question