Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CSS gradient look smooth?

I have a black background and want to add a block inside with simple CSS gradient from transparent to 0.7 white:

linear-gradient(to right, 
    hsla(0, 0%, 100%, 0), 
    hsla(0, 0%, 100%, 0.76) 14%, 
    hsla(0, 0%, 100%, 0.76)
)

But this looks bad:

enter image description here

The only way I found is to add additional color stops, manually.

background: linear-gradient(
    to right,
    hsla(0, 0%, 100%, 0),
    hsla(0, 0%, 100%, 0.05) 2%,
    hsla(0, 0%, 100%, 0.09) 3%,
    hsla(0, 0%, 100%, 0.2) 5%,
    hsla(0, 0%, 100%, 0.57) 11.5%,
    hsla(0, 0%, 100%, 0.69) 14%,
    hsla(0, 0%, 100%, 0.75) 16.5%,
    hsla(0, 0%, 100%, 0.76) 17.5%,
    hsla(0, 0%, 100%, 0.77)
);

And it looks much better:

The comparsion demonstration

The comparsion demonstration on CodePen

Is there an easier way to make CSS gradient smooth on color stops?

like image 324
Даниил Пронин Avatar asked Dec 28 '16 08:12

Даниил Пронин


People also ask

Can you do gradients in CSS?

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)

Can you animate a CSS gradient?

Thanks to the new @property defined in the CSS Properties and Values API Level 1 specification we can now have transition with custom properties (aka CSS variables).

What are the three main types of gradients in CSS?

There are 3 different CSS gradients: linear, conic, and radial. Each gradient uses a CSS function to pass in multiple color arguments. The colors can be in the format of hex, hsla, rgba or named colors. In addition, you can pass in direction and angles to specify the shape and transition of the gradient.

How many types of gradient are there in CSS?

You can choose between three types of gradients: linear (created with the linear-gradient() function), radial (created with the radial-gradient() function), and conic (created with the conic-gradient() function).


1 Answers

One day, I hope, we've got this:

linear-gradient(
  to top,
  hsla(330, 100%, 45%, 0),
  cubic-bezier(0.45, 0, 0.5, 0.5),
  hsla(330, 100%, 45%, 1)
);

Bot for now, we have this:

  • PostCSS plugin with 2 options: https://github.com/larsenwork/postcss-easing-gradients
  • An app allowng you to choose an easing function: https://larsenwork.com/easing-gradients/
like image 145
Даниил Пронин Avatar answered Oct 07 '22 01:10

Даниил Пронин