Shadows add depth. They make cards appear to float above the page and buttons look pressable. Gradients add colour and energy with smooth blends for backgrounds, buttons and even text. Both are drawn by the browser in pure CSS, so you don't need any image files.
Used well, these effects guide the eye: a raised card looks clickable, and a subtle gradient can separate a header from the content below. Used too heavily, they make a page look dated and busy. This lesson covers the syntax and the restraint that makes them look professional.
box-shadow syntax
box-shadow: 0 8px 20px rgb(0 0 0 / 15%);
/* x y blur colour */- The x and y offsets move the shadow right and down. Negative values move it left and up.
- Blur sets how soft the edge is.
0gives a sharp edge. - Spread, an optional fourth length, makes the shadow larger or smaller.
- inset draws the shadow inside the box, which is useful for pressed buttons and input fields.
- Separate several shadows with commas to build realistic, layered depth.
A shadow follows the element's border-radius, so rounded cards get rounded shadows automatically.
text-shadow
text-shadow works the same way but without spread. text-shadow: 2px 2px 0 #15161b; gives a retro, hard-edged shadow, and 0 0 10px cyan creates a neon glow. Use it sparingly on body text, where it usually makes reading harder, and check that the text still has enough contrast.
Linear, radial and conic gradients
Gradients are images generated by the browser, so they go in background or background-image:
| Function | Looks like |
|---|---|
linear-gradient(90deg, red, blue) | A straight blend in one direction |
radial-gradient(circle, yellow, orange) | A blend spreading out from a centre |
conic-gradient(red, yellow, green, red) | A sweep around a centre, like a colour wheel or pie chart |
Add colour stops to control where colours change: linear-gradient(#17a865 0%, #17a865 50%, #4467ff 50%) gives a hard split instead of a blend. Gradients can also be layered over a photo to darken it, which keeps text on top readable.
CSS Shadows and Gradients Syntax
Shadows and gradients
.card { box-shadow: 0 4px 12px rgb(0 0 0 / 12%); }
.title { text-shadow: 1px 1px 2px #0004; }
.hero { background: linear-gradient(135deg, #17a865, #4467ff); }
.orb { background: radial-gradient(circle, #ffd23f, #f0592b); }CSS Shadows and Gradients Summary
box-shadowtakes x and y offsets, blur, optional spread and a colour.- Layer several shadows with commas for natural depth; use
insetfor inner shadows. text-shadowadds shadows or glows to text without a spread value.- Gradients are background images: linear, radial or conic.
- Colour stops control where gradient colours change.
CSS Shadows and Gradients Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Three shadow styles
<div class="row">
<div class="card soft">Soft</div>
<div class="card hard">Hard</div>
<div class="card glow">Glow</div>
</div>.row { display: flex; gap: 20px; padding: 20px; background: #fbf7f0; }
.card {
width: 100px;
padding: 26px 0;
text-align: center;
border-radius: 14px;
background: white;
font: 700 16px sans-serif;
}
.soft { box-shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 10px 24px rgb(0 0 0 / 12%); }
.hard { border: 2px solid #15161b; box-shadow: 5px 5px 0 #15161b; }
.glow { box-shadow: 0 0 0 3px #4467ff, 0 0 24px #4467ff; }Line-by-line explanation
- Line 12Two layered shadows, a tight one and a large soft one, look natural.
- Line 13Zero blur gives a bold, graphic "neo-brutalist" shadow.
- Line 14A spread-only ring plus a blurred glow.
Gradient backgrounds
<div class="banner">Linear</div>
<div class="sun">Radial</div>
<div class="pie"></div>
<div class="split">Hard stop</div>div { margin-bottom: 10px; font: 700 16px sans-serif; color: white; }
.banner { padding: 20px; border-radius: 12px; background: linear-gradient(135deg, #17a865, #4467ff); }
.sun { width: 120px; height: 120px; border-radius: 50%; display: grid; place-items: center; background: radial-gradient(circle at 35% 35%, #ffd23f, #f0592b); }
.pie { width: 120px; height: 120px; border-radius: 50%; background: conic-gradient(#4467ff 0 40%, #ffd23f 40% 75%, #f0592b 75%); }
.split { padding: 20px; background: linear-gradient(90deg, #15161b 50%, #7c3aed 50%); }Line-by-line explanation
- Line 3A diagonal blend from green to blue.
- Line 4A radial gradient whose centre is moved up and left, like light hitting a ball.
- Line 5Conic gradient with stops = a pie chart in one line.
- Line 6Two stops at the same position create a sharp edge instead of a blend.
Gradient text
<h1 class="grad">Make it pop</h1>.grad {
font: 800 3rem sans-serif;
background: linear-gradient(90deg, #f0592b, #7c3aed, #4467ff);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}Line-by-line explanation
- Line 3The gradient is the background…
- Lines 4 to 6…clipped to the shape of the text, with the text colour made transparent so it shows through.
Common CSS Shadows and Gradients Mistakes
Using gradient as a colour
.btn { background-color: linear-gradient(red, blue); }.btn { background: linear-gradient(red, blue); }Gradients are images, so background-color ignores them. Use background or background-image.
Heavy black shadows
.card { box-shadow: 0 0 20px black; }.card { box-shadow: 0 8px 24px rgb(0 0 0 / 12%); }Strong black shadows look dirty. Use transparency and a little vertical offset, like real light from above.
CSS Shadows and Gradients in Real Projects
Depth and colour in design
Modern design systems define "elevation" levels as shadows (raised cards, menus, modals). Gradients appear in hero sections, app icons, buttons and charts. Even this site's "Final CTA" and stat cards use these exact techniques.
CSS Shadows and Gradients Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Add a soft shadow that's 4px down with a 10px blur.
.card {
: 0 10px rgb(0 0 0 / 15%);
}
Which property adds a shadow to text?
text-shadow follows the shape of the letters.Where can you use linear-gradient()?
The button should have a left-to-right gradient from #17a865 to #4467ff and a hard 4px shadow with no blur in #15161b. Fix the CSS.
<button class="cta">Start free</button>.cta {
padding: 12px 22px;
border: 2px solid #15161b;
color: white;
background-color: linear-gradient(90deg, #17a865, #4467ff);
box-shadow: 4px #15161b;
}- Has a linear gradient
- Gradient uses the two colours
- Hard 4px shadow with no blur
CSS Shadows and Gradients Quiz
3 questions to check what you learned.
What does the third length in
box-shadow: 2px 2px 8px blackcontrol?- Spread
- Blur
- Opacity
- Inset
How do you put a shadow inside an element?
box-shadow: inside …inner-shadow: …box-shadow: inset …- Negative blur
Which gradient sweeps around a centre point like a pie chart?
- linear-gradient
- radial-gradient
- circle-gradient
- conic-gradient
CSS Shadows and Gradients Challenge
Design a glowing card
Style .card:
- A background
linear-gradientfrom#7c3aedto#4467ff - A layered
box-shadowwith two shadows (separated by a comma) - White text with a
text-shadow
<div class="card"><h3>Pro Member</h3><p>All lessons unlocked ✨</p></div>.card { width: 240px; padding: 20px; border-radius: 18px; font-family: sans-serif; }
- Gradient background with both colours
- Two layered box-shadows
- White text
- A text-shadow is set