CSS radial-gradient()
function creates a smooth color transition that expands from an origin. For example,
div {
background-image: radial-gradient(orange, red);
}
Browser Output
![CSS Radial Gradient Example CSS Radial Gradient Example](http://devcdn.programiz.com/cdn/farfuture/2GWIFTzaGMOwh-ZG7PHh2Fhs8UPrK9ERyMEMqbKNYcI/mtime:1687247250/sites/tutorial2program/files/css-radial-gradient-introduction-example.png)
Here, the radial-gradient()
function creates a radial gradient that radiates from orange
to red
.
CSS radiant-gradient Syntax
The syntax of the radial-gradient()
function is as follows,
background: radial-gradient(shape size at position, start-color, ..., last-color);
Here,
shape
: defines the shape of the gradientsize
: defines the size of the shapeposition
: defines the position of the center of the gradient in pixels or percentagesstart-color
: defines the starting color of the gradientlast-color
: defines the ending color of the gradient
Radial Gradient With Different Shape
The shape of the radial gradient can be adjusted with a circle
or ellipse
value. For example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient with ellipse</h2>
<div class="ellipse"></div>
<h2>Radial gradient with circle</h2>
<div class="circle"></div>
</body>
</html>
div {
height: 250px;
width: 400px;
}
div.ellipse {
/* creates an elliptical radial gradient, default value */
background-image: radial-gradient(ellipse, blue, red);
}
div.circle {
/* creates a circular radial gradient */
background-image: radial-gradient(circle, blue, red);
}
Browser Output
![CSS Radial Gradient Shape Example CSS Radial Gradient Shape Example](http://devcdn.programiz.com/cdn/farfuture/ZEi_ZsOKYLGW4qzJQu4fk6yrI-UmhzpzYUD5AFYcT3s/mtime:1687247272/sites/tutorial2program/files/css-radial-gradient-shape-example.png)
Radial Gradient With Different Size
The size of the radial gradient can be adjusted by the keyword values and length units. It can be farthest-side
(default value), farthest-corner
, closest-side
, and closest-corner
.
Let's see an example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient with farthest side (default value)</h2>
<div class="farthest_side"></div>
<h2>Radial gradient with closest side</h2>
<div class="closest_side"></div>
</body>
</html>
div {
width: 450px;
height: 250px;
}
div.farthest_side {
/* radial gradient stretches to the farthest side */
background-image: radial-gradient(circle farthest-side, blue, red);
}
div.closest_side {
/* radial gradient shrinks to fit the closest-side */
background-image: radial-gradient(circle closest-side, blue, red);
}
Browser Output
![CSS Radial Gradient having Keyword Size CSS Radial Gradient having Keyword Size](http://devcdn.programiz.com/cdn/farfuture/WR49r3ejlUnp9rEIrE_D8jUFpmB4m1wpHxNLJzSzkSo/mtime:1687247233/sites/tutorial2program/files/css-radial-gradient-having-keyword-size-example.png)
Radial Gradient Size With Length Units
The size of the radial gradient can also be adjusted with the length units. For example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient with 100px size</h2>
<div class="first"></div>
<h2>Radial gradient with 200px size</h2>
<div class="second"></div>
</body>
</html>
div {
height: 250px;
width: 300px;
}
div.first {
/* defines circular radial gradient with 100px size */
background-image: radial-gradient(circle 100px, blue, red);
}
div.second {
/* defines circular radial gradient with 200px size */
background-image: radial-gradient(circle 200px, blue, red);
}
Browser Output
![CSS Radial Gradient Having Length Units Size CSS Radial Gradient Having Length Units Size](http://devcdn.programiz.com/cdn/farfuture/gj2JM_8DcwpNRavlnoI4s38G0aMDnha4Ce10Z1dgu58/mtime:1687247244/sites/tutorial2program/files/css-radial-gradient-having-length-units-size-example.png)
Radial Gradient With Different Position
The origin position of the radial gradient can be adjusted with the pixel or percentage length values.
Let's see an example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient at 100px</h2>
<div class="position-pixel"></div>
<h2>Radial gradient at 80% 20%</h2>
<div class="position-percentage"></div>
</body>
</html>
div {
height: 250px;
width: 400px;
}
div.position-pixel {
/* positions radial gradient 100px from the left, and vertically center by default */
background-image: radial-gradient(circle 100px at 100px, blue, red);
}
div.position-percentage {
/* positions radial gradient 80% from the left and 20% from the top */
background-image: radial-gradient(circle 100px at 80% 20%, blue, red);
}
Browser Output
![CSS Radial Gradient Position Example CSS Radial Gradient Position Example](http://devcdn.programiz.com/cdn/farfuture/rpPjCgdE37l4bMxleH7ADS6Ypf8dfGnf_-ZSRhly3Y8/mtime:1687247264/sites/tutorial2program/files/css-radial-gradient-position-example.png)
Radial Gradient With Multiple Color
We can provide the multiple comma-separated color values for the radial gradient. For example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient with red, blue, and black</h2>
<div></div>
</body>
</html>
div {
height: 300px;
width: 300px;
/* defines circular radial gradient with red, blue, and black color */
background-image: radial-gradient(circle, red, blue, black);
}
Browser Output
![CSS Radial Gradient Multiple Colors Example CSS Radial Gradient Multiple Colors Example](http://devcdn.programiz.com/cdn/farfuture/u44rCcU2sV_nR8D7y0Eyu_fPQpRE6L4gJnUmm8QIKpI/mtime:1687247257/sites/tutorial2program/files/css-radial-gradient-multiple-colors-example.png)
Example: Radial Gradient With Color Stop Value
We can also provide the length values for each color stops to control the radial gradient. The value can be provided in the pixel or percentage. For example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS radial-gradient</title>
</head>
<body>
<h2>Radial gradient with red 20%, blue 40%, black 60%</h2>
<div></div>
</body>
</html>
div {
width: 300px;
height: 300px;
/* defines circular radial gradient with different color stop values*/
background-image: radial-gradient(circle, red 20%, blue 40%, black 60%);
}
Browser Output
![CSS Radial Gradient With Color Stop Value Example CSS Radial Gradient With Color Stop Value Example](http://devcdn.programiz.com/cdn/farfuture/yQ4UgjsL-So0a6ICFnPRmDrgttB8JOGJgO5f7hPvTPw/mtime:1687247282/sites/tutorial2program/files/css-radial-gradient-with-color-stop-value-example.png)
In the above example,
radial-gradient(circle, red 20%, blue 40%, black 60%);
defines a circular radial gradient with three color stops: red
at 20%
of the distance from the center, blue
at 40%
, and black
at 60%
.
CSS Repeating Radial Gradient
The repeating-radial-gradient()
property is used to create the pattern of the radial gradient. For example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>CSS repeating-radial-gradient</title>
</head>
<body>
<h2>Repeating Radial Gradient</h2>
<div><!--Adding a background using repeating-radial-gradient()--></div>
</body>
</html>
div {
width: 300px;
height: 300px;
background-image: repeating-radial-gradient(circle, red 20%, blue 40%);
}
Browser Output
![CSS Repeating Radial Gradient Example CSS Repeating Radial Gradient Example](http://devcdn.programiz.com/cdn/farfuture/ykNrKXrKJkwMcw9Xyy2tPPKl6mNeD3yq3fArQexz0Vg/mtime:1687247288/sites/tutorial2program/files/css-repeating-radial-gradient-example.png)
In the above example,
repeating-radial-gradient(circle, red 20%, blue 40%);
creates a repeating circular radial gradient with red
and blue
.