Working With CSS Gradients
Tags: css, ui
Published: 2023-01-17 16:00:39

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/cssgradients_69bc3e426b1450d74bac7951d8d862db.jpg" alt="Stylized image of a gradient."/></figure>

One of the fantastic features that HTML5 has made available to web developers is the ability to establish gradients using CSS3. This feature has enabled the use of gradient effects for backgrounds instead of images. This article will explain what a gradient is, the different types of CSS gradients, and several ways you can use these gradients to create unique backgrounds for your websites. In addition, you’ll learn some tools for generating CSS gradients. So, let's delve into it!

## What Are Gradients?

[Gradients](https://elementor.com/blog/gradients-in-web-design/#:~:text=A%2520gradient%2520is%2520a%2520gradual,add%2520depth%2520to%2520an%2520image.) are a visual effect that appears as a gradual blend or change from one color into another. CSS gradients help you to build a smooth transition between the colors.

## Why Use Gradients?

There are several reasons we use gradients:

- To create an exciting user interface for our web pages.
- To allow us the privilege of being creative; we can make attractive and captivating designs by blending colors.
- To increase depth and dimension in our web page backgrounds instead of creating designs that look flat.
- To bypass the general rule of avoiding the use of more than two or three colors.
- To help draw attention to our websites.


## Types of CSS Gradients

All gradients have a central point where the color begins to blend into others**.** We will be looking at three different types of CSS gradients:

[CSS Linear Gradient](https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient)**:** This is the most common type of gradient and requires the direction and starting points of transition for the gradient effect. It uses two or three colors that blend evenly in a straight line. The color transition can go left, right, up, down or diagonally. Two colors are commonly used in linear gradients, although more than two colors can work, too.

[CSS Radial Gradient](https://www.freecodecamp.org/news/css-radial-gradient/)**:** This type of gradient starts at one point that radiates outward. The color begins at the element's center and fades at an equal rate as it transitions outward in all directions. The shape of the radial gradient is, by default, elliptical.

[CSS Conic Gradients](https://www.digitalocean.com/community/tutorials/css-conic-gradients)**:** In this type of gradient, the focal point is rotated around the center. The center starts by default at 50%, faces upwards, and the color stops are evenly spaced. However, you can change the proportion of the color stops as well as the starting position. Conic gradients are helpful in pie charts. Also, conic gradients are similar to radial gradients as they are circular and use their element's center as the point for color stops. The difference is that the color stops of the radial gradient arise from the circle's center, while the conic gradient has its color stops around the circle.

## How Gradients Work in CSS

CSS gradients make use of `background-image`. In CSS, you can control every area of how gradients work, from the colors and their transitions to directions. In addition, the gradients use a [CSS function](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Functions) to allow many color transitions. The colors can take different formats like HEX, HSLA, RGBA, or named colors.

In order to understand how gradients work, there are several points to consider:

- You must be intentional when choosing colors because not all colors go well together. Learn more about choosing [colors here](https://code.pieces.app/blog/choosing-colors-in-user-interface-design).
- To avoid a grayish center, choose three colors instead of two.
- The colors must blend well enough that users cannot see where the transition occurred.
- You need to understand the different types of gradients to know how gradients work in CSS.


Now, let’s look at the different properties of CSS gradients, with examples.

### Linear Gradient Effects Properties

We identify linear gradients using the `linear-gradient()` function in the `background` or `background-image` property, and it must include two or more color values. The default setting of linear gradients is from top to bottom of the element, giving a smooth transition from the first color value to the second. However, we can change this direction with keywords or define degree values before color values. Here are some of the effects linear gradients can have:

**Top to Bottom (Default):** This is the default effect of linear gradients and describes a linear gradient with the starting point at the top, ending at the bottom.

Example:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-style: italic;
      font-weight: bold;
      padding: 90px;
      height: 200px;
      background-color: brown;
      background-image: linear-gradient(red, yellow, green);
    }
  </style>
</head>

<body>

  <h1>Linear Gradient - Top to Bottom</h1>
  <p>A linear gradient with red transitioning smoothly into yellow and yellow into green:</p>
  <div class="icy">Icyqueen</div>


</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=03a048a3f4)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image10_ed329eddeeaf99927319b9f0fcff495a.jpg" alt="A vertical gradient from red to green."/></figure>

This default effect of a linear gradient shows how three different colors transition smoothly from top to bottom.

**Left to Right:** This property shows a color transition from the left to the right.

Example:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-weight: bold;
      font-style: italic;
      height: 200px;
      background-color: red;
      padding: 90px;
      background-image: linear-gradient(to right, red, yellow, green);
    }
  </style>
</head>

<body>

  <h1>Linear Gradient - Left to Right</h1>
  <p>A linear gradient with red transitioning smoothly from the left to yellow and green to the right</p>
  <div class="icy">Icyqueen</div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=34c94597e4)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image1_f8613cb7f7b902439e6120787458fce5.jpg" alt="A horizontal gradient from red to green."/></figure>

Observe how the transitioning flows from left to right.

**Diagonal:** This shows a diagonal transition from top-left to bottom-right:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-weight: bolder;
      font-style: italic;
      height: 200px;
      background-color: red;
      padding: 90px;
      background-image: linear-gradient(to bottom right, red, yellow, green);
    }
  </style>
</head>

<body>

  <h1>Linear Gradient - Diagonal</h1>
  <p>A linear gradient with red transitioning smoothly from the top-left to yellow and green to the bottom-right</p>
  <div class="icy">Icyqueen</div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=e2a241b824)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image11_6670b87d17c3cf51ff77dc6a7062c72a.jpg" alt="A diagonal gradient from red to green."/></figure>

**Angles:** With this property, you can implement different directions by setting an angle instead of using predefined directions like the ones discussed above (top, bottom, left, right, etc.).

Various angles have different effects. With gradient angles, the starting point of 0 degrees is equal to the top, and at each 90 degree interval in a clockwise circle is represented accordingly. For example, 180 degrees would be the bottom. In addition, you can transition colors from the zero starting point in positive or negative angles. A positive angle moves the gradient in a clockwise direction, while a negative angle moves it counterclockwise.

Example:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy1 {
      font-size: 35px;
      padding: 30px;
      height: 100px;
      background-color: red;
      background-image: linear-gradient(0deg, red, yellow, green);
    }

    .icy2 {
      font-size: 35px;
      padding: 30px;
      height: 100px;
      background-color: red;
      background-image: linear-gradient(90deg, red, yellow, green);
    }

    .icy3 {
      font-size: 35px;
      padding: 30px;
      height: 100px;
      background-color: red;
      background-image: linear-gradient(180deg, red, yellow, green);
    }
  </style>
</head>

<body>

  <h1>Linear Gradients - Using Different Angles</h1>
  <div class="icy1" style="text-align:center">0degree</div><br>
  <div class="icy2" style="text-align:center">90degree</div><br>
  <div class="icy3" style="text-align:center">180degree</div><br>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=91374b9aba)

Here is result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image2_b05d48f07538b86335344a5191c27a75.jpg" alt="Three gradients at different angles."/></figure>

**Transparency Effect:** You can also use CSS linear gradients to create a fading effect. To use transparency, the `rgba()` function plays a role in defining color stops, and the value 0 to 1 specifies the transparency of the color. A value of 0 shows full transparency, while 1 shows fully opaque color, with no transparency.

Example:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      padding: 90px;
      height: 200px;
      background-image: linear-gradient(to right, rgba(255, 0, 0, 1), rgba(255, 255, 0, 1), rgba(0, 128, 0, 0));
    }
  </style>
</head>

<body>

  <h1>Linear Gradient - Transparency</h1>
  <div class="icy">Icyqueen</div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=06e5479afb)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image3_255198fce0ddf339b389568f4fa0893e.jpg" alt="Horizontal gradient from red to transparent."/></figure>

**Repeating Linear Gradient:** This property makes it possible to use multiple linear gradients with a single function.

Example:

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy1 {
      height: 200px;
      background-color: red;
      background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
    }

    .icy2 {
      height: 200px;
      background-color: red;
      background-image: repeating-linear-gradient(90deg, red, yellow 7%, green 10%);
    }
  </style>
</head>

<body>

  <h1>Repeating Linear Gradient</h1>
  <div class="icy1"></div>
  <p>A repeating gradient with 90degrees starting red and ending with green:</p>
  <div class="icy2"></div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=b1c04bb4bf)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image7_3840ae2370c214c379bf2fd2079154fa.jpg" alt="Repeating gradients from red to green."/></figure>

### Radial Gradient Effects Properties

Radial gradients use the `radial-gradient()` function in `background` or `background-image` property instead of the `linear-gradient()` function. This effect’s properties include the following:

**Evenly Spaced (Default):** This is the default effect of a radial gradient, and creates evenly spaced color stops, as shown below.

```
!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-style: italic;
      height: 200px;
      width: 800px;
      background-image: radial-gradient(red, yellow, green);
    }
  </style>
</head>

<body>

  <h1>The default effect of Radial Gradient - Evenly Spaced Color Stops</h1>
  <div class="icy">Icyqueen</div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=333749af16)

Here is the result. Notice how all the color stops are evenly distributed:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image8_64c2c986bd753c36e4edd2f3bd1d8ed8.jpg" alt="A circular gradient from red out to green."/></figure>

**Differently Spaced:** This effect allows for radial gradients with color stops of varying spaces.

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-weight: bold;
      font-style: italic;
      height: 200px;
      width: 800px;
      background-image: radial-gradient(red 10%, yellow 20%, green 50%);
    }
  </style>
</head>

<body>

  <h3>Radial Gradient with Different color stops spacing</h3>
  <div class="icy">Icyqueen</div>
  <div id="grad1"></div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=94054f8f37)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image4_12ab8ea62f998dccd525ca8ef136b094.jpg" alt="A thicker circular gradient from red out to green."/></figure>

Observe the different spacing of the color stops, with green being larger than others. This is because green has 50% of the gradients.

**Repeating Radial Gradient:** This property makes it possible to replicate radial gradients with the `repeating-radial-gradient()` function, as shown below.

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-weight: bold;
      font-size: 35px;
      height: 200px;
      width: 800px;
      background-color: red;
      background-image: repeating-radial-gradient(red, blue 10%, yellow 5%, green 15%);
    }
  </style>
</head>

<body>

  <h1>Repeating Radial Gradient</h1>
  <div class="icy">Icyqueen</div>


</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=aa514cad2f)

Here is the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image6_2b787fcf7c5b7271c1f2b0cebd02dc6a.jpg" alt="A repeating circular gradient."/></figure>

### Conic Gradient Effects Properties

The gradient effects for conic gradients include the following:

**Multiple Color Conic Gradients:** The example below shows a conic gradient with many colors. Notice how four different colors show a conic gradient effect with the color stops around it.

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      text-align: center;
      font-size: 35px;
      font-style: italic;
      font-weight: bold;
      height: 200px;
      width: 300px;
      background-color: red;
      background-image: conic-gradient(red, yellow, green, blue);
    }
  </style>
</head>

<body>

  <h1>Multiple Color Conic Gradient</h1>

  <div class="icy">Icyqueen</div>


</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=7e9940b3ca)

See the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image5_612e777c988d6f9f507c8fc519cd40aa.jpg" alt="A conic gradient from red around to blue."/></figure>

**Repeating Conic Gradients:** A `repeating-conic-gradient()` function is what you use to show the following effect. Notice how repeated color gradients show a conic gradient effect with many color starts and stops.

```
<!DOCTYPE html>
<html>

<head>
  <style>
    .icy {
      height: 200px;
      width: 200px;
      background-color: red;
      background-image: repeating-conic-gradient(red 0deg 10deg, yellow 10deg 20deg, blue 20deg 30deg, green 40deg 40deg);
      border-radius: 50%;
    }
  </style>
</head>

<body>

  <h1>Repeating a Conic Gradient</h1>
  <div class="icy">Icyqueen</div>

</body>

</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=97d843acfe)

See the result:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image9_7306d0c76fa23d5992c4ea05d4eda499.jpg" alt="A repeating conic gradient."/></figure>

## CSS Gradient Generator Tools

Sometimes it can be tedious working with CSS gradients manually, especially if you’re new to them. Thankfully, some CSS gradient generator tools allow you to use CSS gradients with ease. These tools perform different functions and have a variety of features. Here are some CSS gradient generator tools to explore:

[Colorspace](https://mycolor.space/gradient?ori=to+right+top&hex=%2523051937&hex2=%2523A8EB12&sub=1)**:** This tool initially used two color generators, but a Beta feature of generating three color gradients has been added. With this tool, you can play with the colors to get the desired gradient effect, and then copy the generated CSS code for your project. This tool only generates linear gradients, but you can use the CSS colors in radial and conic gradients.

[Vivid Gradient Generator Tool](https://www.learnui.design/tools/gradient-generator.html): This CSS gradient tool allows you to generate three different color systems (HCL, HSB and HSL). In addition, you can also export a gradient as an SVG image and develop any of the three CSS gradient types by simply clicking "Copy CSS."

[Gradient Generator](https://www.joshwcomeau.com/gradient-generator/): This tool uses an easing curve for color distribution control, but it only has the linear gradient feature. You can copy the CSS code for the gradient you create.

[Online Gradient Generator](https://angrytools.com/gradient/): This is an excellent tool; you can blend two or more colors smoothly. In addition, you can generate four types of gradients with it: linear, radial, elliptical and conical. You can also generate CSS code and download the image of the gradient.

## Conclusion

You can now use the CSS gradients effortlessly. Try using them in your projects regularly and see the fantastic things you can create. Explore these tools to master CSS gradients for your new designs.