šØ CSS Gradients: Painting with Code!
The Story of Colors That Flow
Imagine you have a magical paintbrush. But this isnāt any ordinary brushāwhen you dip it in one color and drag it across paper, it slowly changes into another color! Thatās exactly what CSS gradients do. They let you blend colors together, creating smooth, beautiful transitions.
Think of a sunset. The sky starts orange near the sun, then fades to pink, then purple, and finally dark blue at the top. Nature doesnāt just stop one color and start anotherāit flows. CSS gradients work the same way!
š The Five Gradient Superpowers
| Gradient Type | What It Does | Real-Life Example |
|---|---|---|
| Linear | Colors flow in a straight line | A sunset on the horizon |
| Radial | Colors spread from a center point | A glowing light bulb |
| Conic | Colors spin around like a wheel | A color wheel or pie chart |
| Repeating | Pattern repeats over and over | Striped candy cane |
| Color Stops | Control where colors appear | Rainbow with exact bands |
1ļøā£ Linear Gradients: The Straight Path
What Is It?
A linear gradient paints colors in a straight lineālike coloring a ruler from one end to the other.
The Basic Recipe
background: linear-gradient(
direction,
color1,
color2
);
Simple Example
.sunset-box {
background: linear-gradient(
to right,
orange,
pink
);
}
This creates a box that starts orange on the left and smoothly becomes pink on the right.
Direction Options
| Direction | What It Means |
|---|---|
to right |
Left ā Right |
to left |
Right ā Left |
to bottom |
Top ā Down |
to top |
Bottom ā Up |
to bottom right |
Diagonal! |
45deg |
45° angle |
180deg |
Straight down |
Multiple Colors Example
.rainbow-bar {
background: linear-gradient(
to right,
red,
orange,
yellow,
green,
blue
);
}
This creates a rainbow! Colors flow left to right: red ā orange ā yellow ā green ā blue.
graph TD A[Start Color] --> B[Middle Colors] B --> C[End Color] style A fill:#ff6b6b style B fill:#ffd93d style C fill:#6bcb77
2ļøā£ Radial Gradients: The Glowing Circle
What Is It?
A radial gradient starts from a center point and spreads outwardālike dropping a pebble in water and watching ripples grow!
The Basic Recipe
background: radial-gradient(
shape at position,
color1,
color2
);
Simple Example
.glowing-orb {
background: radial-gradient(
circle,
yellow,
orange
);
}
This creates a glowing sun! Yellow in the center, fading to orange at the edges.
Shape Options
| Shape | What It Looks Like |
|---|---|
circle |
Perfect round glow |
ellipse |
Stretched oval glow |
Position Examples
/* Glow from top-left corner */
background: radial-gradient(
circle at top left,
white,
blue
);
/* Glow from exact spot */
background: radial-gradient(
circle at 25% 75%,
pink,
purple
);
Spotlight Effect
.spotlight {
background: radial-gradient(
circle at center,
white 0%,
transparent 50%,
black 100%
);
}
This creates a spotlight in the middle of darkness!
3ļøā£ Conic Gradients: The Spinning Wheel
What Is It?
A conic gradient spins colors around a center pointālike a pizza with different colored slices, or a color wheel!
The Basic Recipe
background: conic-gradient(
from angle at position,
color1,
color2
);
Simple Example
.color-wheel {
background: conic-gradient(
red,
yellow,
green,
blue,
red
);
border-radius: 50%;
}
This creates a complete color wheel! Notice we end with red again so it connects smoothly.
Pie Chart Example
.pie-chart {
background: conic-gradient(
red 0deg 90deg,
blue 90deg 200deg,
green 200deg 360deg
);
border-radius: 50%;
}
This creates a pie chart with three slices!
graph TD A[Start at 0°] --> B[Spin Clockwise] B --> C[Colors Change as We Spin] C --> D[Return to Start at 360°]
Starting From Different Angles
/* Start from the top */
background: conic-gradient(
from 0deg,
red, yellow, green
);
/* Start from the right */
background: conic-gradient(
from 90deg,
red, yellow, green
);
4ļøā£ Repeating Gradients: Patterns Forever!
What Is It?
Repeating gradients take a small pattern and repeat it over and overālike wallpaper or a candy caneās stripes!
Three Types of Repeating
| Type | Creates |
|---|---|
repeating-linear-gradient |
Repeating stripes |
repeating-radial-gradient |
Repeating circles |
repeating-conic-gradient |
Repeating wheel segments |
Striped Pattern
.candy-stripes {
background: repeating-linear-gradient(
45deg,
red 0px,
red 10px,
white 10px,
white 20px
);
}
This creates diagonal red and white stripesālike a candy cane!
Bullseye Pattern
.bullseye {
background: repeating-radial-gradient(
circle,
red 0px,
red 10px,
white 10px,
white 20px
);
}
This creates a target with alternating red and white rings!
Sunburst Pattern
.sunburst {
background: repeating-conic-gradient(
gold 0deg,
gold 15deg,
orange 15deg,
orange 30deg
);
}
This creates alternating gold and orange rays spinning outward!
5ļøā£ Color Stops: Taking Control
What Are They?
Color stops tell the gradient exactly where each color should be. Without them, colors are spread evenly. With them, YOU control the mix!
The Basic Idea
background: linear-gradient(
to right,
red 0%, /* Red starts at 0% */
red 30%, /* Red stays until 30% */
blue 30%, /* Blue starts at 30% */
blue 100% /* Blue goes to the end */
);
This creates a hard line between red and blue at the 30% markāno blending!
Soft vs Hard Stops
Soft Stop (Smooth Blend):
background: linear-gradient(
to right,
red 0%,
blue 100%
);
Hard Stop (Sharp Line):
background: linear-gradient(
to right,
red 0%,
red 50%,
blue 50%,
blue 100%
);
Using Different Units
/* Percentages */
background: linear-gradient(
red 25%,
blue 75%
);
/* Pixels */
background: linear-gradient(
red 50px,
blue 150px
);
Color Stop Hint (Midpoint)
/* Move blend midpoint to 25% */
background: linear-gradient(
red,
25%,
blue
);
This makes the blend happen fasterāmore blue, less red!
graph TD A["0% - Color 1 Starts"] --> B["Color Stop Position"] B --> C["Blend Zone"] C --> D["100% - Color 2 Ends"]
šÆ Quick Reference Table
| What You Want | Code Pattern |
|---|---|
| Left to right blend | linear-gradient(to right, red, blue) |
| Top to bottom blend | linear-gradient(to bottom, red, blue) |
| Diagonal blend | linear-gradient(45deg, red, blue) |
| Center glow | radial-gradient(circle, white, black) |
| Color wheel | conic-gradient(red, yellow, green, blue, red) |
| Stripes | repeating-linear-gradient(45deg, red 0 10px, white 10px 20px) |
| Hard color edge | linear-gradient(red 50%, blue 50%) |
š Pro Tips
- Combine gradients! You can layer multiple gradients:
background:
linear-gradient(rgba(0,0,0,0.5), transparent),
radial-gradient(circle, yellow, orange);
- Use transparent for fading effects:
background: linear-gradient(
red,
transparent
);
-
Make patterns with repeating gradients for textures without images.
-
Conic gradients + border-radius: 50% = instant pie charts!
š You Did It!
Now you know how to:
- Make colors flow in straight lines with linear gradients
- Create glowing circles with radial gradients
- Spin colors like a wheel with conic gradients
- Build endless patterns with repeating gradients
- Control exactly where colors appear with color stops
Go paint your web pages with the power of gradients! š