En radial gradient er defineret af dens centrum.
For at skabe en radial gradient skal du også definere mindst to farvestop.
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
Som standard er formen ellipse, størrelsen er det fjerneste hjørne, og positionen er i midten.
Radial gradient - jævnt fordelte farvestop (dette er standard)
Følgende eksempel viser en radial gradient med jævnt fordelte farvestop:
#grad {
background-image: radial-gradient(red, yellow, green);
}
Prøv det selv →
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(red, yellow, green);
}
</style>
</head>
<body>
<h1>Radial Gradient - Evenly Spaced Color Stops</h1>
<div id="grad1"></div>
</body>
</html>
Radial gradient - Farvestop med forskellig afstand
Følgende eksempel viser en radial gradient med farvestop med forskellig afstand:
#grad {
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
Prøv det selv →
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
</style>
</head>
<body>
<h1>Radial Gradient - Differently Spaced Color Stops</h1>
<div id="grad1"></div>
</body>
</html>
Formparameteren definerer formen. Det kan tage værdicirklen eller ellipsen. Standardværdien er ellipse.
Følgende eksempel viser en radial gradient med form som en cirkel:
#grad {
background-image: radial-gradient(circle, red, yellow, green);
}
Prøv det selv →
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(red, yellow, green);
}
#grad2 {
height: 150px;
width: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(circle, red, yellow, green);
}
</style>
</head>
<body>
<h1>Radial Gradient - Shapes</h1>
<h2>Ellipse (this is default):</h2>
<div id="grad1"></div>
<h2><strong>Circle:</strong></h2>
<div id="grad2"></div>
</body>
</html>
Parameteren size
definerer størrelsen af gradienten. Det kan tage fire værdier:
closest-side
farthest-side
closest-corner
farthest-corner
En radial gradient med nøgleord i forskellige størrelser:
#grad1 {
background-image: radial-gradient(closest-side at 60% 55%, red, yellow,
black);
}
#grad2 {
background-image: radial-gradient(farthest-side at 60%
55%, red, yellow, black);
}
Prøv det selv →
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 150px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}
#grad2 {
height: 150px;
width: 150px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(farthest-side at 60% 55%, red, yellow, black);
}
#grad3 {
height: 150px;
width: 150px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(closest-corner at 60% 55%, red, yellow, black);
}
#grad4 {
height: 150px;
width: 150px;
background-color: red; /* For browsers that do not support gradients */
background-image: radial-gradient(farthest-corner at 60% 55%, red, yellow, black);
}
</style>
</head>
<body>
<h1>Radial Gradients - Different size keywords</h1>
<h2>closest-side:</h2>
<div id="grad1"></div>
<h2>farthest-side:</h2>
<div id="grad2"></div>
<h2>closest-corner:</h2>
<div id="grad3"></div>
<h2>farthest-corner (default):</h2>
<div id="grad4"></div>
</body>
</html>
Funktionen repeating-radial-gradient()
bruges til at gentage radiale gradienter:
En gentagen radial gradient:
#grad {
background-image:
repeating-radial-gradient(red, yellow 10%, green 15%);
}
Prøv det selv →
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
background-color: red; /* For browsers that do not support gradients */
background-image: repeating-radial-gradient(red, yellow 10%, green 15%);
}
</style>
</head>
<body>
<h1>Repeating Radial Gradient</h1>
<div id="grad1"></div>
</body>
</html>