Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Patterned gradients in modern browsers

I get more and more complaints regarding badly rendered gradients (CSS, Canvas, and SVG).

As it highly depends on the browser and OS. I can't propose a global MCVE. So I'll focus on this simple CSS gradient which is badly rendered on Chrome/Ubuntu (vertical stripes):

body {
    background: linear-gradient(90deg, #111, #444);
}

Of course I'd be interested in a solution for this specific case. But I'd value more a general solution to get smooth monotonous gradients. Canvas or image based solutions are not acceptable as my gradients are in great number, dynamic, and usually partially transparent.

like image 785
Denys Séguret Avatar asked Nov 16 '17 12:11

Denys Séguret


People also ask

Does linear gradient work on all browsers?

All desktop browsers including Internet Explorer 11 and Microsoft Edge provide browser support for Linear CSS Gradients, meaning these CSS Gradients offer excellent cross browser compatibility.

What are the three types of gradients?

CSS defines three types of gradients: Linear Gradients (goes down/up/left/right/diagonally) Radial Gradients (defined by their center) Conic Gradients (rotated around a center point)

What is gradient in Web?

What Is A Gradient? A gradient is a gradual transition from one color to another. It allows designers to create almost a new color. Gradients make objects stand out by adding a new dimension and realism to a design. Simply put, gradients add depth to an image.


2 Answers

In many cases it is not neccessarily the fault of the OS or Browser, it is more the problem, that a lot of people don't know, how gradients work, and why they are patterned.

This is highly a problem of the luminosity of each color. In your example it is easy to find the luminosity of the gradient, since its a grayscaled gradient. So take a look at the picture below, where I put the darkest and lightest color of the gradiend side by side. You will notice, that they have a slightly different luminosity. If you make the black a bit lighter (or the gray a bit darker) the pattern will start to disappear.

enter image description here

Now to make it clearer, lets start with this simple gradient. (Don't worry, I will get to the pattern problem) Looks pretty dirty, doesn't it? Especially that grey in the middle of the gradient.

enter image description here

If we take a look at the RGB Color wheel, we see the problem. The gradient goes right through the middle of the wheel, which is a really washed out spot. We can avoid this, if we just start to add another stop in our gradient, such as green. If you compare it with the wheel on the right side, you cann see, that this avoids the part in the middle. The gradient below is much more colorful and does not look washed out.

enter image description here enter image description here

But, now we have a pretty hard pattern in our gradient. This is, as already mentioned above, because we have a lot of different luminosities. So we don't actually want to go through the RGB spectrum with our gradients, we would more like to go through a color space, which is based on luminosity. And that is the HSL color space. Below you have a comparison of our initla red-to-cyan gradient between RGB and HSL, and you will notice, that there is much less of the banding in the HSL gradient. There is acutally a tool in the web, which I like to use for gradients. You can give a start and an end point (and steps between) and it generates a gradient in all the different color spaces, and mostly I pick the HSL one, because its the most natural one.

enter image description here

enter image description here

tl;dr The problem with these patterns is often a problem of the gradients luminosity, and if you try to change the color space, it will work out much better. Now I don't say, that ifferent Monitors, Browsers or Operating Systems don't have any influence on this, but the problem can be reduced with this trick pretty easily.

like image 68
Matthias Seifert Avatar answered Nov 15 '22 22:11

Matthias Seifert


The lines you are seeing is called banding. Chrome in Win10 dithers gradients, FF does not. This differs per browser.

Some informative links related to this question:

Without using an image as the gradient it is not possible to solve this as far as I know. Perhaps FireFox will dither gradients in a future version.

like image 35
Red Avatar answered Nov 15 '22 23:11

Red