CSS Radial Gradient

CSS radial-gradient() and repeating-radial-gradient() functions allow to compute amazing radial gradients without need to use external graphics software. In this post I will show you multiple ideas how to use CSS radial gradient, CSS conic gradient and create gradient background CSS.

Youtube Video – CSS Radial / Conic Gradient

Demo and code (tl;dr)

See the Pen CSS Linear Gradient | linear-gradient() by Artur Karczmarczyk (@arturkarczmarczyk) on CodePen.

CSS radial-gradient() basic usage

Let’s jump right into the code. To create a basic radial gradient, simply use radial-gradient function and provide two colors. Here I am using the color names, but you can use everything that is available for colors, so you can use hexadecimal values or RGB etc.

CSS
<div style="background: radial-gradient(black, darkcyan)"><span>basic</span></div>

As the first parameter, we can provide the position of the center of the gradient. By default it is center. Other possibilities include top, right, left and bottom for sides and a mix of those for corners.

HTML
<div style="background: radial-gradient(at center, black, darkcyan)"><span>center</span></div>
<div style="background: radial-gradient(at left, black, darkcyan)"><span>left</span></div>
<div style="background: radial-gradient(at right, black, darkcyan)"><span>right</span></div>
<div style="background: radial-gradient(at top, black, darkcyan)"><span>top</span></div>
<div style="background: radial-gradient(at bottom, black, darkcyan)"><span>bottom</span></div>
<div style="background: radial-gradient(at top left, black, darkcyan)"><span>corner</span></div>

Effect:

Examples of basic radial-gradient() CSS function usage. Various way to position the center of the radial gradient.
Examples of basic radial-gradient() CSS function usage. Various way to position the center of the radial gradient.

Next to each side name, we can provide the length, how much to move the center from it. You can go with pixels or percents:

HTML
<div style="background: radial-gradient(at top 30px left 30px, black, darkcyan)"><span>transitioned</span></div>
<div style="background: radial-gradient(at top 30% left 30%, black, darkcyan)"><span>percents</span></div>

Effect:

Changing position of the center of a radial gradient with pixels or percents.
Changing position of the center of a radial gradient with pixels or percents.

Adjusting CSS Radial Gradient Shape and Size

We can provide more colors if needed. The size of the gradient can also be adjusted:

HTML
<div style="background: radial-gradient(at top 30% left 30%, black, darkcyan)"><span>percents</span></div>
<div style="background: radial-gradient(at center, black, cyan, darkcyan)"><span>3 colors</span></div>

Effect:

Effects of adding a third color and setting fixed size of the CSS radial gradient.
Effects of adding a third color and setting fixed size of the CSS radial gradient.

The gradient size can also be set using keywords closest-side, closest-corner, farthest-side, farthest-corner. Defaults to farthest-corner.

HTML
<div style="width: 200px; background: radial-gradient(closest-side at center, black, cyan, darkcyan)"><span>closest-side</span></div>
<div style="width: 200px; background: radial-gradient(farthest-side at center, black, cyan, darkcyan)"><span>farthest-side</span></div>
<div style="width: 200px; background: radial-gradient(closest-corner at center, black, cyan, darkcyan)"><span>closest-corner</span></div>
<div style="width: 200px; background: radial-gradient(farthest-corner at center, black, cyan, darkcyan)"><span>farthest-corner</span></div>

By default, the gradient has an ellipse shape, but we can change it to circle.

HTML
<div style="width: 200px; background: radial-gradient(ellipse closest-side at center, black, cyan, darkcyan)"><span>ellipse</span></div>
<div style="width: 200px; background: radial-gradient(circle closest-side at center, black, cyan, darkcyan)"><span>circle</span></div>

Effect:

Setting the shape of CSS radial gradient to either ellipse or to circle.
Setting the shape of CSS radial gradient to either ellipse or to circle.

We can specify the stops for each color, to obtain for example a kind of ring. If we wanted the gradient to repeat, we can do that easily by changing the gradient function to repeating one:

HTML
<div style="background: radial-gradient(black, darkcyan 10px, black 20px)"><span>ring</span></div>
<div style="background: repeating-radial-gradient(black, darkcyan 10px, black 20px)"><span>repeating rings</span></div>

Effect:

Setting fixed positions of CSS radial gradient color stops. Repeating radial CSS gradient.
Setting fixed positions of CSS radial gradient color stops. Repeating radial CSS gradient.

Finally, let’s use the background image layers feature to put three partially transparent radial gradients on top of each other.

HTML
<div style="background:
                radial-gradient(circle at bottom, #FF0000AA, transparent),
                radial-gradient(circle at top left, #00FF00AA, transparent),
                radial-gradient(circle at top right, #0000FFAA, transparent)
;"><span>layers</span></div>

Effect:

Three CSS radial gradients put on top of each other for color blending.
Three CSS radial gradients put on top of each other for color blending.

Summary

In this post I showed you multiple ideas how to use CSS radial gradients. I hope that it will help you create custom CSS gradient backgrounds. I encourage you to check out the Youtube video of CSS radial & conic gradients. In the video I show you all the ideas above in detail, and also show you how to use radial gradient & font-awesome icon to create a logo!

Teaser how to create gradient logo with CSS
Teaser how to create gradient logo with CSS.

If you’d like to learn more on HTML, CSS and LESS in a structured, no-nonsense, hands-on manner, try my Udemy HTML & CSS 2022: Hands-on No-nonsense Guide!

Leave a Reply

%d bloggers like this: