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>
▶ Try it Yourself

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>
▶ Try it Yourself

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>
▶ Try it Yourself

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>
▶ Try it Yourself

❓ 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

📝 Exercises

  1. Create a gradient button — left-to-right color transition, direction changes on hover.
  2. Create a "floating card" — subtle shadow by default, deeper shadow + lift on hover.
  3. Use repeating-linear-gradient to create a striped background pattern.
  4. Add text-shadow or drop-shadow glow effect to a PNG icon or text.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏