CSS: CSS Gradients & Shadows

🗓 Last updated: 2026-11-30👤 Written by: Blake✓ Reviewed by: Gray🏷 Category: CSS Tutorial

Properties in this section: text-shadow box-shadow inset

1. Gradients

Gradients create smooth transitions between colors. CSS provides two main gradient functions.

(1) linear-gradient

Creates a straight-line gradient:

▶ Example

▶ Try it Yourself

(2) radial-gradient

Creates a circular/elliptical gradient:

▶ Example

▶ Try it Yourself

2. Shadows

(1) box-shadow

Adds shadow effects to elements:

▶ Example

▶ Try it Yourself

(2) text-shadow

Adds shadow to text:

▶ Example

▶ Try it Yourself

Properties in this section: text-shadow box-shadow inset

❓ FAQ

Q How do I write a CSS linear gradient, and what do linear-gradient's parameters mean?
A The basic structure is background: linear-gradient(to right, #ff7e5f, #feb47b); — the first argument is the direction (to right goes left to right), followed by at least two colors. Add percentages to control where each color lands: linear-gradient(to right, #ff7e5f 30%, #feb47b 70%). List more colors for multi-stop gradients. Remember a gradient is background-image, not background-color.
Q How does radial-gradient work, and how is it different from linear-gradient?
A linear-gradient transitions along a straight line; radial-gradient radiates outward from a center point, with a circle or ellipse shape: background: radial-gradient(circle, #ffffff, #333333);. Typical uses are glows, button highlights, and sphere-like shading. The argument order matches linear-gradient — you just swap the direction for a shape plus position (centered by default).
Q I can't come up with a gradient color scheme — which online tools should I use?
A Try cssgradient.io: drag color stops and copy the generated code directly. For ready-made palettes, webgradients.com and uigradients.com offer hundreds of copy-paste gradients. Studying existing schemes builds the intuition for angles and color stops that hand-writing gradients requires.
Q How do I add a shadow in CSS? What do box-shadow's four values mean?
A The order is horizontal offset, vertical offset, blur radius, spread radius, then color: box-shadow: 2px 4px 8px 0px rgba(0,0,0,0.2);. The first two decide which way the shadow leans (negative values flip), more blur means softer edges, and spread grows or shrinks the whole shadow. For everyday cards, 0 2px 8px rgba(0,0,0,0.1) is a natural magnitude.
Q Can box-shadow stack multiple shadows?
A Yes, separate them with commas: box-shadow: 0 2px 8px rgba(0,0,0,0.1), 0 8px 32px rgba(0,0,0,0.08); builds a two-layer shadow — crisp nearby, diffuse far away. This is the standard trick behind Material Design style cards. 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 the text itself — no spread parameter and no inset support. box-shadow acts on the entire element box, has all four values, and accepts inset for inner shadows. Similar syntax, different capabilities: text-shadow for type, box-shadow for cards and buttons.
Q How do I create glowing or neon text?
A Stack multiple same-colored text-shadows with growing blur: text-shadow: 0 0 10px #0ff, 0 0 20px #0ff, 0 0 40px #0ff;. More layers mean a denser glow, and dark backgrounds make it pop. For a stronger neon feel, add one or two small-offset white shadows as highlights.
Q My box-shadow isn't showing — what are the common causes?
A Check in this order: the element has a transparent background so the shadow sits behind nothing visible (give it a background color); a parent's overflow: hidden clips the shadow that extends outside; a syntax error like a missing comma between stacked shadows; or the shadow color's alpha is too low — below about 0.05 it's invisible on light pages. Opening DevTools to see which declaration got struck out is the fastest diagnosis.
Q How do I control a gradient's direction — to right versus 45deg?
A to right, to bottom right and friends are keyword directions pointing at an edge or corner; 45deg is an angle where 0deg points straight up, 90deg straight right, increasing clockwise. So to right equals 90deg and to top equals 0deg. Use angles for precise diagonal gradients, keywords for simple horizontal/vertical ones — they read better.

📖 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

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

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