CSS: CSS Gradients & Shadows
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 Yourself2. Shadows
(1) box-shadow
Adds shadow effects to elements:
▶ Example
▶ Try it Yourself(2) text-shadow
Adds shadow to text:
▶ Example
▶ Try it Yourself❓ 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
- 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.
🧩 Related Examples
Gradient Glow ButtonThree pure-CSS gradient buttons: linear-gradient fills, a glowing box-shadow that lifts on hover, and a ::be…
Neon Glow ButtonThree neon buttons on a dark stage: transparent fill, bright outlines, and twin text/box-shadow glows breath…
Gradient TextThe three-piece combo that paints text with a gradient: background gradient + background-clip: text + color:…