Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › box-shadow

CSS Reference

box-shadow

Adds a shadow effect around an element. The foundation of elevation and depth in modern UI.

Last updated: October 2026

Definition: The box-shadow property draws a shadow behind or inside an element. The syntax is offset-x offset-y blur-radius spread-radius color. It doesn't affect layout — it's purely visual, so it's safe to use freely for elevation.

🎬 Live Examples

Five shadow intensities and a glow effect.

sm
md
lg
xl
inset
glow

💡 Shadows create visual hierarchy — a large shadow feels closer to the user, a small one feels further away.

📝

Basic Syntax

/* Full syntax */
box-shadow: offset-x offset-y blur-radius spread-radius color;

/* Example */
.card {
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.3);
}

/* Inner shadow */
.inset {
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
}

Only offset-x and offset-y are required. Everything else defaults.

🎯 The 5 Values Explained

Value Meaning Example
offset-xHorizontal offset0 = centered
offset-yVertical offset4px = shadow below
blur-radiusHow soft the shadow is8px = soft
spread-radiusHow much the shadow grows0 = no growth
colorShadow color (with alpha)rgba(0,0,0,0.3)
1

An Elevation System

Define a set of shadows as CSS variables for consistent depth across your site.

:root {
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 20px rgba(0, 0, 0, 0.4);
    --shadow-xl: 0 20px 40px rgba(0, 0, 0, 0.5);
    --shadow-2xl: 0 25px 50px rgba(0, 0, 0, 0.6);
}

.card        { box-shadow: var(--shadow-sm); }
.dropdown    { box-shadow: var(--shadow-md); }
.modal       { box-shadow: var(--shadow-xl); }
.toast       { box-shadow: var(--shadow-2xl); }
2

Inset Shadows

Add the inset keyword to draw the shadow inside the element.

/* Pressed input field */
input {
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* Inset top highlight for glassmorphism */
.glass {
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.1),
        0 4px 8px rgba(0, 0, 0, 0.3);
}
3

Multiple Shadows

Chain shadows with commas for realistic layering.

/* Realistic multi-layer shadow */
.card {
    box-shadow:
        0 1px 2px rgba(0, 0, 0, 0.1),
        0 2px 4px rgba(0, 0, 0, 0.1),
        0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Colored glow with hard shadow */
.neon {
    box-shadow:
        0 0 10px rgba(59, 130, 246, 0.6),
        0 0 20px rgba(59, 130, 246, 0.4),
        0 0 40px rgba(59, 130, 246, 0.2);
}

Multiple shadows create depth that a single shadow can't match. This is how modern design systems simulate realistic elevation.

4

Focus Rings with box-shadow

Box-shadow doesn't affect layout, so it's ideal for focus indicators.

button:focus-visible {
    outline: 2px solid transparent;  /* Forced-colors mode */
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}

/* Multiple focus rings */
.input:focus {
    border-color: #3b82f6;
    box-shadow:
        0 0 0 1px #3b82f6,
        0 0 0 4px rgba(59, 130, 246, 0.15);
}

💡 Why not just outline? Outline can't have rounded corners that follow border-radius on all browsers. Box-shadow respects border-radius and doesn't shift layout.

5

Hover Lift Effect

.card {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 24px rgba(0, 0, 0, 0.3);
}

Combine transform and box-shadow to create a realistic "lift" effect. The shadow grows and moves down while the element moves up.

🎯 Real-World Shadow Patterns

Element Typical shadow
Card (resting)0 1px 3px rgba(0,0,0,0.2)
Card (hover)0 10px 25px rgba(0,0,0,0.3)
Dropdown menu0 4px 12px rgba(0,0,0,0.3)
Modal0 20px 60px rgba(0,0,0,0.6)
Toast0 25px 50px rgba(0,0,0,0.5)
Input (inset)inset 0 1px 2px rgba(0,0,0,0.3)
Focus ring0 0 0 3px rgba(59,130,246,0.5)

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS box-shadow property?

The box-shadow property adds a shadow effect around an element. The syntax is offset-x, offset-y, blur-radius, spread-radius, and color. Prefix with 'inset' for an inner shadow. Multiple shadows can be chained with commas.

What is the syntax order for box-shadow?

box-shadow: offset-x offset-y blur-radius spread-radius color. Only offset-x and offset-y are required. The inset keyword goes at the start for inner shadows.

Does box-shadow affect layout?

No. Box-shadow does not affect layout — it doesn't take up space and isn't included in the box model. It's purely visual and can be used freely for elevation without breaking layouts.

How do I create an elevation system with box-shadow?

Define a set of CSS variables for increasing shadow intensity — like --shadow-sm, --shadow-md, --shadow-lg, --shadow-xl. Use them across cards, dropdowns, and modals to create visual hierarchy.

Can box-shadow be used for focus rings?

Yes. box-shadow can create a focus ring without affecting layout, unlike border. Example: box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5). This is a common modern technique.

🎯 What's Next?

← All CSS Reference