CSS Gradients
Create beautiful color transitions and effects
๐ What are CSS Gradients?
CSS gradients create smooth transitions between multiple colors. They can be linear, radial, or conic, and are perfect for modern web design backgrounds and effects.
/* Simple linear gradient */
.gradient-box {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
padding: 20px;
color: white;
}
Output:
Types of Gradients
Linear Gradient
Colors transition in a straight line
linear-gradient(45deg, red, blue)
Radial Gradient
Colors radiate from a center point
radial-gradient(circle, red, blue)
Conic Gradient
Colors rotate around a center point
conic-gradient(red, blue, red)
Repeating Gradients
Gradients that repeat their pattern
repeating-linear-gradient(...)
๐น Linear Gradients
Linear gradients create smooth, straight-line color transitions and are defined using the linear-gradient() function within properties like background or background-image. You specify a direction (angle or keyword like to bottom right) and a list of color stops. For instance, linear-gradient(90deg, red, yellow) creates a horizontal gradient from red to yellow. They are incredibly versatile for creating backgrounds, overlays, and decorative effects without image files, improving page load performance. Advanced techniques involve using multiple color stops, hard stops, and transparency to create stripes, depth, and sophisticated visual layers that enhance modern web design aesthetics.
/* Basic linear gradient */
.linear-basic {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
/* Diagonal gradient */
.linear-diagonal {
background: linear-gradient(45deg, #667eea, #764ba2);
}
/* Multiple colors */
.linear-multiple {
background: linear-gradient(90deg, #ff9a9e, #fecfef, #fecfef, #ff9a9e);
}
/* With color stops */
.linear-stops {
background: linear-gradient(
to right,
#ff6b6b 0%,
#4ecdc4 50%,
#45b7d1 100%
);
}
Output:
๐น Radial Gradients
Radial gradients generate circular or elliptical color transitions radiating outward from a central point, created with the radial-gradient() function. Syntax controls shape (circle or ellipse), size, position, and color stops. Example: radial-gradient(circle at top left, white, blue). They are ideal for simulating light sources, creating vignette effects, stylized buttons, or attention-grabbing backgrounds. As vector-based effects, they scale perfectly for responsive design and high-DPI screens. Mastering radial gradients allows designers to build more organic, immersive visual experiences directly in CSS, reducing reliance on static image assets and contributing to faster, more SEO-friendly websites.
/* Basic radial gradient */
.radial-basic {
background: radial-gradient(circle, #ff6b6b, #4ecdc4);
}
/* Elliptical gradient */
.radial-ellipse {
background: radial-gradient(ellipse, #667eea, #764ba2);
}
/* Positioned gradient */
.radial-positioned {
background: radial-gradient(
circle at top left,
#ff9a9e,
#fecfef
);
}
/* With size */
.radial-sized {
background: radial-gradient(
circle 100px at center,
#a8edea,
#fed6e3,
transparent
);
}
Output:
๐น Conic Gradients
Conic gradients create color transitions rotated around a center point, like a color wheel or pie chart, using the conic-gradient() function. Colors are specified with an angle. For example, conic-gradient(red, yellow, green, blue). They are perfect for creating statistical pie charts, color wheels, stylized loading spinners, and dynamic background patterns. This gradient type adds a new dimension to CSS-based data visualization and decorative elements. While browser support is now excellent, they offer a unique tool for developers to create complex, angle-based color effects without SVG or Canvas, keeping implementations lightweight and highly customizable through CSS variables and animations.
/* Basic conic gradient */
.conic-basic {
background: conic-gradient(#ff6b6b, #4ecdc4, #45b7d1, #ff6b6b);
border-radius: 50%;
}
/* Rainbow wheel */
.conic-rainbow {
background: conic-gradient(
red, orange, yellow, green, blue, indigo, violet, red
);
border-radius: 50%;
}
/* Pie chart style */
.conic-pie {
background: conic-gradient(
#ff6b6b 0deg 120deg,
#4ecdc4 120deg 240deg,
#45b7d1 240deg 360deg
);
border-radius: 50%;
}
/* From specific angle */
.conic-angled {
background: conic-gradient(
from 45deg,
#667eea,
#764ba2,
#667eea
);
border-radius: 50%;
}
Output:
๐น Repeating Gradients
Repeating gradients (repeating-linear-gradient, repeating-radial-gradient, repeating-conic-gradient) create seamless tiled patterns by repeating defined color stops. Unlike standard gradients that transition once across the element, repeating gradients continue the pattern indefinitely. For instance, repeating-linear-gradient(45deg, blue 0px, blue 10px, white 10px, white 20px) creates striped patterns. This technique is powerful for generating backgrounds with stripes, checks, polka dots, or complex geometric designs purely in CSS, eliminating the need for background image files. This reduces HTTP requests, improves performance scores, and allows for dynamic pattern adjustments through code, enhancing both design flexibility and site speed.
/* Repeating linear stripes */
.repeating-stripes {
background: repeating-linear-gradient(
45deg,
#ff6b6b,
#ff6b6b 10px,
#4ecdc4 10px,
#4ecdc4 20px
);
}
/* Repeating radial circles */
.repeating-circles {
background: repeating-radial-gradient(
circle,
#667eea,
#667eea 10px,
#764ba2 10px,
#764ba2 20px
);
}
/* Checkerboard pattern */
.checkerboard {
background:
repeating-linear-gradient(
0deg,
#ff9a9e,
#ff9a9e 20px,
#fecfef 20px,
#fecfef 40px
),
repeating-linear-gradient(
90deg,
#ff9a9e,
#ff9a9e 20px,
#fecfef 20px,
#fecfef 40px
);
background-blend-mode: multiply;
}
Output:
๐น Advanced Gradient Techniques
Advanced gradient techniques combine multiple gradients, blend modes, CSS variables, and animations to create stunning, dynamic visual effects that were once only possible with image editors. Examples include animated gradient backgrounds, gradient text, complex mesh patterns, and simulating glass morphism or neumorphism effects. By layering gradients and using properties like background-blend-mode, you can achieve depth and texture. These techniques push CSS to its creative limits, resulting in visually impressive, performant websites that engage users. Since they are code-based, they are infinitely customizable, responsive, and contribute to faster loading times compared to using heavy image assets, positively impacting SEO and user experience.
/* Gradient text */
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 2em;
font-weight: bold;
}
/* Animated gradient */
.animated-gradient {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1);
background-size: 300% 300%;
animation: gradientShift 3s ease infinite;
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* Gradient overlay */
.gradient-overlay {
position: relative;
background-image: url('image.jpg');
}
.gradient-overlay::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(
rgba(0,0,0,0.3),
rgba(0,0,0,0.7)
);
}