Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent CSS gradient banding?

I started using CSS gradients, rather than actual images, for two reasons: first, the CSS gradient definitely loads faster than an image, and second, they aren't supposed to show banding, like so many raster graphics. I started testing my site on various screens recently, and on larger ones (24+ inches), the CSS linear gradient which constitutes my site's background shows very visible banding. As a provisional fix, I've overlaid the gradient with a small, repeating, transparent PNG image of noise, which helps a little. Is there any other way to fix this banding issue?

like image 209
John Doe Avatar asked Jul 26 '12 09:07

John Doe


People also ask

How do you stop a linear gradient from repeating CSS?

A color-stop's <color> value, followed by one or two optional stop positions, (each being either a <percentage> or a <length> along the gradient's axis). A percentage of 0% , or a length of 0 , represents the start of the gradient; the value 100% is 100% of the image size, meaning the gradient will not repeat.

Why are my gradients banding in Photoshop?

This issue is called “color banding” and it happens when values within a gradient get pushed so much that there is no color/value in the file to actually represent the mathematical change you've applied with a tool in Photoshop. Basically, when this happens it means you are hitting the boundaries of your file.

How do you transition gradients CSS?

In CSS, you can't transition a background gradient. It jumps from one gradient to the other immediately, with no smooth transition between the two. He documents a clever tactic of positioning a pseudo element covering the element with a different background and transitioning the opacity of that pseudo element.


1 Answers

I know you won't like the sound of this, but the only real way right now to get a consistent cross-browser aesthetic in this case, is to use a repeating image.

If it's a simple linear gradient, then you only need it to be 1px wide and as high as the gradient, then make the background colour of the page as the final colour of the gradient so it runs smoothly. This will keep file size tiny.

If you want to reduce gradient bands in your image, use a PNG (not transparency) as I find these to be better suited than JPG's for this purpose.

In Adobe Fireworks, I would export this as a PNG-24.

Good luck.

http://codepen.io/anon/pen/JdEjWm

#gradient {   position: absolute;   width: 100%;   height: 100%;   background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(black), to(white));   background: -webkit-linear-gradient(top, black, white);   background: -moz-linear-gradient(top, black, white);   background: -ms-linear-gradient(top, black, white);   background: -o-linear-gradient(top, black, white);   background: linear-gradient(top, black, white); } 
like image 176
Michael Giovanni Pumo Avatar answered Sep 20 '22 15:09

Michael Giovanni Pumo