CSS Conic Gradients

Conic gradients are a powerful feature in CSS that allow you to create smooth transitions between multiple colors arranged in a circular manner. They provide a way to achieve visually appealing effects such as radial color transitions, pie charts, loading indicators, and more.

Understanding Basic Syntax

Conic gradients are defined using the conic-gradient() function in CSS. The basic syntax consists of specifying color stops along with their positions within the conic gradient.

				
					.element {
  background-image: conic-gradient(color1 0%, color2 25%, color3 50%, color4 75%, color5 100%);
}

				
			

In the above code:

  • color1, color2, etc., represent the colors used in the gradient.
  • 0%, 25%, 50%, 75%, and 100% are the positions of color stops.

Creating Simple Conic Gradients

Let’s create a simple conic gradient background for a div element.

				
					.simple-gradient {
  width: 200px;
  height: 200px;
  background-image: conic-gradient(red, blue, green);
}

				
			

In this example, the conic gradient transitions smoothly from red to blue to green in a circular manner.

Adding Color Stops

Color stops allow you to control where each color begins and ends within the gradient circle.

				
					.color-stops {
  width: 200px;
  height: 200px;
  background-image: conic-gradient(red 0%, blue 25%, green 50%, yellow 75%, orange 100%);
}

				
			

Here, we’ve added color stops at specified positions, creating a gradient with five distinct colors.

Advanced Techniques

Repeating Gradients

You can create repeating conic gradients by adding the repeating-conic-gradient() function.

				
					.repeating-gradient {
  width: 200px;
  height: 200px;
  background-image: repeating-conic-gradient(red, blue 45deg, green 90deg);
}

				
			

This creates a repeating gradient that starts with red, transitions to blue every 45 degrees, and then transitions to green every 90 degrees.

Radial Gradients

Conic gradients can be combined with radial gradients to achieve complex effects.

				
					.radial-conic {
  width: 200px;
  height: 200px;
  background-image: radial-gradient(circle, white, black), conic-gradient(red, blue);
}

				
			

Here, we have a radial gradient from white to black combined with a conic gradient from red to blue.

Using Angles for Direction

While the basic syntax of conic gradients relies on color stops, you can also define gradients using angles to specify the direction of the gradient. This technique allows for more precise control over the orientation of the gradient.

				
					.element {
  width: 200px;
  height: 200px;
  background-image: conic-gradient(from 45deg, red, blue, green);
}

				
			

In this example, the gradient starts from a 45-degree angle, transitioning from red to blue to green in a clockwise direction. Experimenting with different angles enables you to create unique visual effects.

Creating Pie Charts

Conic gradients are particularly useful for generating pie charts dynamically in CSS. By assigning percentages to color stops, you can represent data visually with pie slices of varying sizes.

				
					.pie-chart {
  width: 200px;
  height: 200px;
  background-image: conic-gradient(
    #FF5733 0% 20%,  /* 20% */
    #F0C419 20% 45%, /* 25% */
    #A9DFBF 45% 90%, /* 45% */
    #5DADE2 90% 100% /* 10% */
  );
}

				
			

In this example, each color stop represents a different data segment of the pie chart. Adjusting the percentages allows you to modify the size of each segment accordingly.

Dynamic Loading Indicators

Conic gradients can be employed to create dynamic loading indicators, providing visual feedback to users during content loading or processing.

				
					.loading-indicator {
  width: 100px;
  height: 100px;
  border: 4px solid transparent;
  border-radius: 50%;
  border-top-color: #3498db;
  animation: rotate 1s linear infinite;
}

@keyframes rotate {
  to {
    transform: rotate(360deg);
  }
}

				
			

In this example, a simple loading spinner is created using a conic gradient applied to a circular element. By animating the rotation, the spinner gives the illusion of movement, indicating ongoing activity.

Layering with Multiple Gradients

Advanced applications of conic gradients often involve layering multiple gradients to achieve complex visual effects. This technique allows for the creation of intricate patterns and textures.

				
					.layered-gradients {
  width: 200px;
  height: 200px;
  background-image: 
    radial-gradient(circle, #FFF, #000),
    conic-gradient(from 45deg, red, blue, green);
}

				
			

Here, a radial gradient is combined with a conic gradient, resulting in a visually captivating background with both radial and angular color transitions.

Conic gradients in CSS offer a versatile tool for creating visually stunning effects in web design. By mastering the basic syntax and exploring advanced techniques, you can leverage conic gradients to enhance the aesthetics of your web projects. Whether you're creating simple color transitions or complex repeating patterns, conic gradients provide endless possibilities for creativity and expression. Happy coding! ❤️

Table of Contents