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.
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:
optional · inside shadow
horizontal shift
vertical shift
softness (0 = hard)
size expansion
rgba for transparency
hard edge
soft edge
bottom only
glow effect
larger shadow
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.
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.
focused
0 blur, +4px
/* 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:
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:
realistic
soft diffuse
depth
layers
glow
shadow
outer
+ outer drop
soft
+ ambient
/* ── 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:
: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:
/* 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 …
|
Try It Yourself
All shadow types in one editor — edit values to see them change live. Hover the cards for elevation transitions:
Quick Check — Chapter 46
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-rightbox-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 colorbox-shadow: 0 4px 8px rgba(0,0,0,.2);
— the standard pattern with blurThe parser distinguishes blur from spread by position — the third length value is blur, the fourth is spread.
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.
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);
: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.
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); }
box-shadow: 0 0 0 3px #264de4
create?
box-shadow: A, B, C,
which shadow appears on top (visually in front)?
inset keyword in
box-shadow do?
-
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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.