CSS Gradients & Shadows
Gradients
Gradients create smooth transitions between colors. CSS provides two main gradient functions.
linear-gradient
Creates a straight-line gradient:
Example
HTML
<style>
.gradient-1 {
background: linear-gradient(to right, #3498db, #2ecc71);
height: 80px;
margin-bottom: 10px;
}
.gradient-2 {
background: linear-gradient(135deg, #e74c3c, #f39c12, #2ecc71);
height: 80px;
}
</style>
<div class="gradient-1">Left to right gradient</div>
<div class="gradient-2">Diagonal multi-color gradient</div>
radial-gradient
Creates a circular/elliptical gradient:
Example
HTML
<style>
.radial {
background: radial-gradient(circle, #3498db, #2c3e50);
height: 150px;
border-radius: 50%;
}
</style>
<div class="radial">Radial gradient circle</div>
Shadows
box-shadow
Adds shadow effects to elements:
Example
HTML
<style>
.card {
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
padding: 20px;
background: white;
border-radius: 8px;
}
.card:hover {
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
</style>
<div class="card">Card with shadow</div>
text-shadow
Adds shadow to text:
Example
HTML
<style>
h1 {
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
</style>
<h1>Heading with shadow</h1>
❓ FAQ
Q Any tools for creating gradients?
A Recommended: cssgradient.io and webgradients.com — drag-and-drop gradient generators. Copy code directly. Studying others' gradient schemes builds intuition for angles and color stops.
Q Can box-shadow have multiple shadows?
A Yes, separate with commas: `box-shadow: 0 2px 8px rgba(0,0,0,0.1), 0 8px 32px rgba(0,0,0,0.08);` creates a double-shadow effect. Shadows stack in declaration order — earlier ones on top.
Q What's the difference between text-shadow and box-shadow?
A `text-shadow` acts on text only, no spread parameter, no inset support. `box-shadow` acts on the entire element box, has spread, supports inset. Similar syntax but different capabilities.
📖 Summary
- Gradients and shadows are two powerful tools for visual quality
linear-gradientis most common for buttons, backgrounds, overlaysbox-shadowadds depth; card hover shadows are the most common usetext-shadowdecorates text;drop-shadowfollows actual element contours- Combining both creates more polished, layered designs
📝 Exercises
- Create a gradient button — left-to-right color transition, direction changes on hover.
- Create a "floating card" — subtle shadow by default, deeper shadow + lift on hover.
- Use
repeating-linear-gradientto create a striped background pattern. - Add
text-shadowordrop-shadowglow effect to a PNG icon or text.



