Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth CSS gradients

I'm learning how to use CSS gradients.

My problem is with top to bottom gradients. You can just see the "stops" in the color changing.

enter image description here

This is my CSS code

#header { 
   width:1000px; 
   height:250px; 
   background:-moz-linear-gradient(top, #BF7A30 30%, #EDD599); 
   background:-webkit-linear-gradient(top, #BF7A30 30%, #EDD599); 
}

Is there a way to smooth out the stops in top to bottom gradients? (this, to my eye, isn't very visible in left to right or right to left gradients)

like image 995
Isuru Avatar asked Oct 31 '12 04:10

Isuru


People also ask

How do you make a radial gradient in CSS?

To create a radial gradient that repeats so as to fill its container, use the repeating-radial-gradient() function instead. Because <gradient> s belong to the <image> data type, they can only be used where <image> s can be used.

What are the three main types of gradients 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).

What is linear gradient CSS?

The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image> .

How do you set a transition gradient in CSS?

CSS has two types of gradients: Linear gradient: It goes down/up/left/right/diagonally and makes smooth transitions of colors. To make a linear transition you first have to choose two color stops. Color stops are the color among which you want to make the transitions.


2 Answers

The main cause of this bending effect is actually the linear blending of colors, which is not as harmonious to the human eye.

Andreas Larsen has written a pretty elaborate article on css-tricks.com (2017). https://css-tricks.com/easing-linear-gradients/

It describes a concept of non-linear gradients by defining multiple color stops approximating a clothoid curve.

Would result in something like this (.gradient-clothoid):

.gradient-wrp {
  display: flex;
}

.header {
  width: 100%;
  height: 250px;
  flex: 0 0 none;
}

.gradient-linear {
  background-image: linear-gradient(#bf7a30 30%, #edd599);
}

.gradient-smooth {
  background-image: linear-gradient(#bf7a30 25%, 75%, #edd599);
}

.gradient-clothoid {
  background-image: linear-gradient(
    rgba(191, 122, 48, 1) 0%,
    rgba(191, 122, 48, 0.3) 50%,
    rgba(191, 122, 48, 0.15) 65%,
    rgba(191, 122, 48, 0.075) 75.5%,
    rgba(191, 122, 48, 0.037) 82.85%,
    rgba(191, 122, 48, 0.019) 88%,
    rgba(191, 122, 48, 0) 100%
  );
}
<div class="gradient-wrp">
  <div class="header gradient-linear"></div>
  <div class="header gradient-smooth"></div>
  <div class="header gradient-clothoid"></div>
</div>

This concept is also known as "scrim".

IMHO not so well suited for "starting" color stops like the original example:

  • the top 30% of gradient should have 100% color intensity. Probably to ensure better text readability for a heading
  • the remaining 70% should have a smooth color transition.

I actually prefer Amelia Bellamy-Royds’ proposal (article down below in the comments) using a (well supported) gradient smoothing by adding stop without color definition like so:

.gradient-smooth{
   background-image:linear-gradient(#BF7A30 25%, 75%, #EDD599); 
}

This will smooth the gradient between 25% and 75% to the bottom spline based and not linear.

.gradient-linear{
   background-image:linear-gradient(#BF7A30 30%, #EDD599); 
 }
like image 164
herrstrietzel Avatar answered Oct 17 '22 23:10

herrstrietzel


As @nighthawk2534 mentioned, adding more colors to the gradient is the way to go. Obviously, those have to be "right" colors.

I don't know color theory enough, but stumbled upon a great tool for that: https://mycolor.space/gradient

For your example it gives:

background-image: linear-gradient(to right top, #bf7a30, #ca9148, #d5a861, #e1bf7d, #edd599);

Which looks like that: image

(I know this thread is very old, but still may be helpful)

like image 1
tachometr Avatar answered Oct 17 '22 22:10

tachometr