π What Is linear-gradient()?
The linear-gradient() function creates a smooth transition between two or more colors along a straight line. It is commonly used for backgrounds, buttons, borders, overlays, and UI effects.
Note
β Supports angles, directions, repeating patterns
β Can blend unlimited colors
π¦ Syntax
Basic Syntax
linear-gradient(direction, color-stop1, color-stop2, ...);A linear gradient is treated as an **image**, often used inside background.
π 1. Basic Two-Color Gradient
Simple gradient
background: linear-gradient(red, blue);Creates a top-to-bottom gradient from red β blue.
π 2. Gradient Direction
β Using Keywords
Direction keywords
background: linear-gradient(to right, red, blue);Directions include:to right, to left, to top, to bottom,to top right, to bottom left, etc.
β Using Degrees
Angle direction
background: linear-gradient(45deg, red, blue);0deg = upward,
90deg = right,
180deg = down,
270deg = left.
π 3. Multiple Color Stops
Multi-color gradient
background: linear-gradient(to right, red, orange, yellow, green);Add as many colors as you want β CSS blends them smoothly.
π 4. Custom Color Stop Positions
Custom stops
background: linear-gradient(
to right,
red 0%,
yellow 40%,
green 80%
);You can control exactly where each color begins/ends.
π 5. Transparent Gradients
Transparency example
background: linear-gradient(
to bottom,
rgba(0,0,0,0) 0%,
rgba(0,0,0,0.7) 100%
);Perfect for overlays on images.
π 6. Gradient on Buttons
Button gradient
.btn {
background: linear-gradient(to right, #4facfe, #00f2fe);
color: white;
padding: 10px 20px;
border-radius: 6px;
}π 7. Hard Stops (Sharp Transitions)
Hard stop gradient
background: linear-gradient(
to right,
red 0%,
red 50%,
blue 50%,
blue 100%
);Creates a sharp boundary between colors β no blending.
π 8. Using linear-gradient() with Background Images
Overlay image
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('banner.jpg');Common technique for darkening or coloring images for text readability.
π 9. Repeating Linear Gradients
Repeating stripes
background: repeating-linear-gradient(
to right,
black 0,
black 10px,
yellow 10px,
yellow 20px
);Creates patterns like stripes or lined paper.
π 10. Angled Background Sections
Diagonal background section
section {
background: linear-gradient(135deg, #6a11cb, #2575fc);
}π 11. Gradient Borders (Using Background Clip)
Gradient border
.gradient-border {
border: 5px solid transparent;
background: linear-gradient(to right, red, blue) border-box;
}π§ͺ Real-World Examples
1οΈβ£ Card Gradient Background
Card UI
.card {
padding: 20px;
background: linear-gradient(to bottom right, #ff7e5f, #feb47b);
border-radius: 12px;
}2οΈβ£ Gradient Divider Line
Divider
.divider {
height: 4px;
background: linear-gradient(to right, transparent, #000, transparent);
}3οΈβ£ Transparent Gradient Mask
Mask effect
.fade-bottom {
background: linear-gradient(to top, white, transparent);
}β οΈ Common Mistakes
- β Misunderstanding gradient angle direction
- β Forgetting that gradients are treated as images
- β Using too many color stops β muddy appearance
- β Adding a background color *after* gradient (overrides gradient)
Note
π₯ Summary
The linear-gradient() function is a powerful CSS tool for creating smooth or sharp color transitions. It's widely used for modern UI design β from buttons to backgrounds, overlays, borders, and text effects.
Learn more βMDN Docs π