Tutorials CSS Box Shadow
Ch 46 / 60
Chapter 46 Beginner 8 min read CSS

CSS Box Shadow

The box-shadow property draws one or more shadows around an element's box. It controls six aspects: horizontal and vertical offset, blur softness, spread size, colour, and whether the shadow sits outside or inside the element. Layering multiple shadows unlocks depth systems, coloured glow effects, inner-highlight effects and complex three-dimensional looks — all in pure CSS.

What you'll learn
All 6 box-shadow values explained
offset-x, offset-y, blur, spread
inset — inner shadows
Multiple layered shadows
Elevation design system
Glow, neumorphism, real-world UI patterns

box-shadow Syntax

The syntax takes up to 6 values. Only the two offsets are required. The inset keyword can appear anywhere in the list:

box-shadow:  inset  4px  8px  16px  2px  rgba(0,0,0,.2);
inset
optional · inside shadow
offset-x
horizontal shift
offset-y
vertical shift
blur-radius
softness (0 = hard)
spread-radius
size expansion
color
rgba for transparency
Hard
0 blur
hard edge
Soft
8px blur
soft edge
Below
0 offset-x
bottom only
Glow
0,0 offsets
glow effect
Spread
+4px spread
larger shadow
Inset
inset
inner shadow

Each Value in Depth

offset-x & offset-y — Position

Positive offset-x moves the shadow right; negative moves it left. Positive offset-y moves down; negative moves up. A zero offset centres the shadow around the element for a glow effect.

↖
↑
↗
←
○
→
↙
↓
↘

All hard shadows (blur:0) showing 8 directions + centred glow

blur-radius — Softness

Controls how soft or sharp the shadow edge is. 0 = hard-edged. Larger values = more diffuse. Cannot be negative.

0px
4px
8px
16px
28px
48px

spread-radius — Size

Positive spread makes the shadow larger than the element. Negative spread shrinks it — useful for creating focused shadows that don't bleed out beyond the edges.

-6px
focused
-2px
0
+4px
focus ring
0 blur, +4px
CSS — box-shadow values
/* offset-x | offset-y | blur | spread | color */
.shadow-basic   { box-shadow: 0 4px 6px rgba(0,0,0,.1); }
.shadow-hard    { box-shadow: 4px 4px 0 rgba(0,0,0,.3); }
.shadow-glow    { box-shadow: 0 0 20px rgba(38,77,228,.5); }

/* Negative spread — focused, contained shadow */
.shadow-focused { box-shadow: 0 8px 16px -4px rgba(0,0,0,.25); }

/* Spread as border / focus ring (no blur needed) */
.focus-ring  { box-shadow: 0 0 0 3px rgba(38,77,228,.5); }
:focus-visible{ box-shadow: 0 0 0 3px rgba(38,77,228,.6); outline: none; }

/* Inset — shadow inside the element */
.pressed     { box-shadow: inset 0 3px 8px rgba(0,0,0,.2); }
.well        { box-shadow: inset 0 2px 4px rgba(0,0,0,.15), inset 0 -1px 0 rgba(255,255,255,.4); }

/* Color with rgba — transparency key for quality shadows */
.card        { box-shadow: 0 4px 16px rgba(0,0,0,.12); }
.card-blue   { box-shadow: 0 4px 16px rgba(38,77,228,.35); }
.card-dark   { box-shadow: 0 4px 16px rgba(0,0,0,.6); }

inset — Inner Shadows

The inset keyword reverses the shadow direction — it appears inside the element's border box. Inset shadows create the impression of a recessed or pressed element:

Input field
input well
Neumorphic
pressed state
Dark card
inner highlight + shadow
Selected
inset border ring
Inner glow
inset glow

Multiple Shadows — Layering

Separate shadows with commas. They stack front-to-back — the first listed is on top. Layering creates physically realistic depth and complex visual effects:

2-layer
realistic
tight ambient +
soft diffuse
3-layer
depth
3 falloff
layers
Coloured
glow
brand-coloured
shadow
Inner +
outer
top highlight
+ outer drop
Hard +
soft
retro hard
+ ambient
CSS — multiple layered shadows
/* ── Realistic 2-layer shadow ── */
.card {
  box-shadow:
    0 1px  3px rgba(0,0,0,.08),    /* close ambient */
    0 4px 16px rgba(0,0,0,.10);    /* diffuse penumbra */
}
.card:hover {
  box-shadow:
    0 2px  6px rgba(0,0,0,.08),
    0 10px 28px rgba(0,0,0,.12);
}

/* ── 3-layer cinematic depth ── */
.modal {
  box-shadow:
    0 1px  3px rgba(0,0,0,.08),
    0 4px 12px rgba(0,0,0,.06),
    0 12px 32px rgba(0,0,0,.04);
}

/* ── Coloured brand shadow ── */
.btn-primary {
  background: #264de4;
  box-shadow:
    0 1px  3px rgba(0,0,0,.15),
    0 4px 14px rgba(38,77,228,.45);  /* brand colour shadow */
}
.btn-primary:hover {
  box-shadow:
    0 2px  6px rgba(0,0,0,.15),
    0 8px 20px rgba(38,77,228,.55);
}

/* ── Inner highlight + outer shadow ── */
.card-lit {
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.8),   /* top inner highlight */
    0 4px 12px rgba(0,0,0,.12);             /* outer drop shadow */
}

/* ── Pressed / active button ── */
.btn:active {
  box-shadow:
    inset 0 3px 6px rgba(0,0,0,.2),       /* pushed in */
    0 1px 2px rgba(0,0,0,.1);              /* remaining drop */
  transform: translateY(1px);             /* physical press */
}

Elevation System

A consistent shadow scale creates visual hierarchy — cards at the same "height" feel related, while modals and dropdowns sit clearly above. Hover each card to see its elevation rise:

Level 0
no shadow
Level 1
list item
Level 2
card
Level 3
raised card
Level 4
dropdown
Level 5
modal
CSS — elevation scale as custom properties
:root {
  --shadow-0: none;
  --shadow-1: 0 1px 2px rgba(0,0,0,.08);
  --shadow-2: 0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.10);
  --shadow-3: 0 2px 6px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.12);
  --shadow-4: 0 4px 8px rgba(0,0,0,.08), 0 16px 36px rgba(0,0,0,.14);
  --shadow-5: 0 8px 16px rgba(0,0,0,.08), 0 24px 48px rgba(0,0,0,.16);
}

/* Usage */
.list-item { box-shadow: var(--shadow-1); }
.card      { box-shadow: var(--shadow-2); }
.dropdown  { box-shadow: var(--shadow-4); }
.modal     { box-shadow: var(--shadow-5); }

/* Raise on hover — just change the variable level */
.card       { box-shadow: var(--shadow-2); transition: box-shadow .2s ease, transform .2s ease; }
.card:hover { box-shadow: var(--shadow-4); transform: translateY(-4px); }

Glow Effects & Coloured Shadows

Using a semi-transparent brand colour as the shadow colour — instead of black — creates vivid glow effects that make buttons and cards feel luminous. Hover each to see the glow intensify:

Blue glow
rgba(38,77,228,.7)
Green glow
rgba(34,197,94,.7)
Purple glow
rgba(168,85,247,.7)
Orange glow
rgba(249,115,22,.7)
Red glow
rgba(239,68,68,.7)
CSS — glow effects and coloured shadows
/* Brand-coloured shadows */
.btn-blue    { box-shadow: 0 4px 14px rgba(38,77,228,.45); }
.btn-green   { box-shadow: 0 4px 14px rgba(34,197,94,.45); }
.btn-purple  { box-shadow: 0 4px 14px rgba(168,85,247,.45); }

/* Glow on hover */
.btn-glow {
  background:  #264de4;
  box-shadow:  0 2px 8px rgba(38,77,228,.3);
  transition:  box-shadow .3s ease;
}
.btn-glow:hover {
  box-shadow: 0 0 0 4px rgba(38,77,228,.2), 0 4px 20px rgba(38,77,228,.5);
}

/* Text / neon glow effect (use text-shadow, not box-shadow) */
.neon-text {
  color:       #4a8af4;
  text-shadow: 0 0 8px rgba(38,77,228,.8), 0 0 20px rgba(38,77,228,.4);
}

/* Neumorphism (light background only) */
.neuro {
  background:  #e8eaf0;
  box-shadow:
     6px  6px 12px rgba(0,0,0,.15),
    -6px -6px 12px rgba(255,255,255,.7);
}
.neuro-pressed {
  box-shadow:
    inset  4px  4px 8px rgba(0,0,0,.15),
    inset -4px -4px 8px rgba(255,255,255,.7);
}

box-shadow Value Reference

Value Required? What it controls Example
inset optional Shadow renders inside element instead of outside inset 0 2px 6px …
offset-x ✓ required Horizontal position. Positive = right, negative = left 4px, -4px
offset-y ✓ required Vertical position. Positive = down, negative = up 8px, 0
blur-radius optional (def 0) Softness of edges. 0 = hard, higher = softer. Cannot be negative. 16px
spread-radius optional (def 0) Grows or shrinks shadow. Positive = larger, negative = smaller. 4px, -2px
color optional Shadow colour. Use rgba for transparency. rgba(0,0,0,.15)
multiple — Comma-separate for multiple layers. First listed = on top. 0 1px 3px …, 0 8px 24px …
Free Resource
CSS3 Cheat Sheet — All Properties & Shadow Patterns
Complete reference covering all CSS box-shadow values, elevation systems and glow patterns.
Download PDF

Try It Yourself

All shadow types in one editor — edit values to see them change live. Hover the cards for elevation transitions:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 46

Questions & Answers
The full syntax is: box-shadow: [inset] offset-x offset-y [blur-radius] [spread-radius] [color]

Required: offset-x and offset-y only. Everything else is optional.

Defaults:
— blur-radius: 0 (hard-edged shadow)
— spread-radius: 0 (same size as element)
— color: inherits from the element's color property (usually black)
— inset: absent by default (shadow is outside)

Minimal valid declarations:
box-shadow: 4px 4px; — hard black shadow, bottom-right
box-shadow: 0 4px rgba(0,0,0,.2); — soft bottom shadow (no blur = still default 0)… wait, actually with just 3 values: offset-x offset-y color
box-shadow: 0 4px 8px rgba(0,0,0,.2); — the standard pattern with blur

The parser distinguishes blur from spread by position — the third length value is blur, the fourth is spread.
A negative spread-radius shrinks the shadow to be smaller than the element. This creates a focused, contained shadow that doesn't bleed out sideways beyond the element edges.

Use case example: a card that should only cast a shadow directly below it (like a floating element):
box-shadow: 0 10px 20px -8px rgba(0,0,0,.4);

— The 10px offset-y moves the shadow down
— The 20px blur spreads it softly
— The -8px spread shrinks it so it only appears beneath the bottom edge, not around all sides

This is a staple technique for making cards or buttons appear to "float" above the surface without casting a wide ambient shadow. Contrast this with a positive spread which expands the shadow larger than the element in all directions.
Multiple shadows are comma-separated and stack front-to-back — the first shadow listed renders on top of subsequent ones:

box-shadow: [FIRST/TOP], [SECOND], [THIRD/BOTTOM];

Common 2-layer realistic shadow pattern:
box-shadow: 0 1px 3px rgba(0,0,0,.08), 0 8px 24px rgba(0,0,0,.12);
— First layer: tight close shadow (like contact shadow at the base)
— Second layer: soft diffuse shadow (like ambient light scatter)

This mimics how real-world shadows work — objects cast both a crisp contact shadow directly below and a soft penumbra further away. Using two layers with different blur radii looks significantly more natural than a single shadow.

You can also mix inset and outer shadows in the same list, enabling effects like an inner highlight on top combined with an outer drop shadow:
box-shadow: inset 0 1px 0 rgba(255,255,255,.8), 0 4px 12px rgba(0,0,0,.12);
Use zero offsets, zero blur, and a positive spread with a semi-transparent colour:
:focus-visible { box-shadow: 0 0 0 3px rgba(38,77,228,.5); outline: none; }

Why box-shadow instead of outline:
— outline does not respect border-radius in older browsers — it renders as a rectangle around a rounded element
— box-shadow follows the element's border-radius, making a perfectly rounded focus ring on rounded buttons
— You can add a transparent white inner ring for better visibility on both light and dark backgrounds: 0 0 0 2px white, 0 0 0 5px rgba(38,77,228,.5)

Accessibility note: modern browsers now respect border-radius in outline, so outline with outline-offset is also a valid approach — and better for Windows High Contrast mode which strips box-shadow. Using both is safest.
Shadows in the real world are never a solid colour — they are the absence or reduction of light, which means they take on properties of the surface they fall on. A solid black shadow looks flat and unnatural on coloured or dark backgrounds.

Using rgba(0,0,0,.1) — 10% opaque black — the shadow blends with whatever is behind the element, creating a natural look on any background colour.

Benefits of rgba:
— Works on any background colour — the shadow tints the background colour, not just darkens
— Looks natural in dark mode (semi-transparent black is barely visible on dark backgrounds, which is correct behaviour)
— Scales — opacity can be adjusted without changing the hue

For dark mode: consider using rgba(0,0,0,.4) with higher opacity since dark backgrounds absorb less shadow. Or use CSS custom properties:
--shadow-color: rgba(0,0,0,.1);
@media (prefers-color-scheme: dark) { --shadow-color: rgba(0,0,0,.4); }
Multiple Choice Questions
Question 1 of 5
Which two values are always required in a box-shadow declaration?
Question 2 of 5
What does box-shadow: 0 0 0 3px #264de4 create?
Question 3 of 5
In box-shadow: A, B, C, which shadow appears on top (visually in front)?
Question 4 of 5
What does the inset keyword in box-shadow do?
Question 5 of 5
Which shadow declaration creates a realistic card that appears to lift on hover?
Chapter Summary
  • Syntax: box-shadow: [inset] offset-x offset-y [blur] [spread] [color]. Only offset-x and offset-y are required.
  • offset-x / offset-y: shift direction. Positive x = right, positive y = down. Zero both for centred glow effect.
  • blur-radius: 0 = hard sharp edge. Higher = softer. Cannot be negative.
  • spread-radius: positive = larger than element, negative = smaller. Use negative spread for focused under-element shadows.
  • color: use rgba(0,0,0,.1) — semi-transparent blends with any background naturally. Solid colours look flat.
  • inset: shadow renders inside element. Creates pressed, recessed, well, inner-glow effects.
  • Multiple shadows: comma-separate. First listed is on top. Use 2 layers (tight ambient + soft diffuse) for realistic depth.
  • Focus ring: box-shadow: 0 0 0 3px rgba(38,77,228,.5) — no offsets, no blur, positive spread = clean ring following border-radius.
  • Elevation system: store shadow levels as CSS custom properties (--shadow-1 → --shadow-5). Consistent visual hierarchy for UI components.
  • Coloured shadows: use the element's brand colour in rgba for vivid glow effects on buttons and cards.
  • Neumorphism: two shadows — one dark bottom-right (outer shadow), one light top-left (highlight). Inset versions for pressed state.
~8 min read
Beginner level
Chapter 46 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel