Look at almost any modern interface: cards seem to float above the page, buttons appear pressed in when clicked, images turn grey until you hover over them, and menus look like frosted glass. None of these need image editing software. They come from a handful of CSS properties: box-shadow, text-shadow, filter and backdrop-filter.
In this chapter we'll learn exactly what each value in a shadow means (with the arithmetic), how to build realistic "elevation", every filter function with what it really does to pixels, and the side effects that catch developers off guard.
Why Shadows?
A screen is flat. Shadows are our brain's main clue that one object is above another. Designers use them to show:
Elevation: a dialog sits above the page, a dropdown above the content.
Interactivity: a card that lifts its shadow on hover invites a click.
State: an inner shadow looks pressed in, like a pushed button or an input field.
Focus: a coloured "ring" shows which element has keyboard focus.
box-shadow
box-shadowdraws one or more shadows around (or inside) an element's border box. Shadows are purely visual: they don't take up space or affect layout.
/* offset-x | offset-y | blur-radius | spread-radius | color */
box-shadow: 4px 6px 10px 0 rgb(0 0 0 / 0.3);| Value | Meaning | Required? |
|---|---|---|
| offset-x | Horizontal shift. Positive = right, negative = left. | Yes |
| offset-y | Vertical shift. Positive = down, negative = up. | Yes |
| blur-radius | How soft the edge is. 0 = sharp edge. Can't be negative. | No (default 0) |
| spread-radius | Grows (positive) or shrinks (negative) the shadow before blurring. | No (default 0) |
| color | Shadow colour; usually semi-transparent black. | No (default currentColor) |
inset | Keyword: draws the shadow inside the box instead. | No |
The lengths must be in that order: the first two are always the offsets, the third is always the blur and the fourth is always the spread. The colour and inset may go at the start or the end.
Seeing each value
.soft { box-shadow: 4px 6px 10px rgb(0 0 0 / 0.3); } /* soft, slightly down-right */
.hard { box-shadow: 4px 6px 0 rgb(0 0 0 / 0.3); } /* blur 0: a solid offset copy */
.ring { box-shadow: 0 0 0 4px #2563eb; } /* no offset, no blur: a 4px "border" */
.pressed { box-shadow: inset 0 2px 6px rgb(0 0 0 / 0.4); } /* looks pushed in */
.bottom { box-shadow: 0 12px 10px -8px rgb(0 0 0 / 0.6); } /* shadow only below */Output (on white cards over a light grey page):
.soft: a gentle blurred shadow down and to the right; the card seems to float..hard: a crisp grey rectangle peeking out from behind the card, a "flat design" or retro look..ring: a blue 4px outline that follows the rounded corners exactly..pressed: darkness along the inside top edge, like a dent..bottom: the negative spread shrinks the shadow so it's narrower than the card, and the 12px downward offset makes it peek out only at the bottom.
The geometry, step by step
Let's calculate exactly where a shadow is drawn. A card is 120px × 70px with
box-shadow: 4px 6px 10px 5px black;Start with a copy of the card's border box: 120 × 70.
Spread 5px grows it on all four sides: (120 + 2×5) × (70 + 2×5) = 130 × 80. Measured from the card's top-left corner, it now runs from (−5, −5) to (125, 75).
Offset moves it 4px right and 6px down: it now runs from (−1, 1) to (129, 81).
Blur 10px softens the edges. The fade from full colour to transparent is about as wide as the blur value, centred on the shadow's edge: roughly 5px inside and 5px outside each edge.
The part directly under the card is hidden by the card (for an outer shadow, the browser actually cuts out the shadow under the box, so it doesn't show through a semi-transparent background).
So on the right side, the shadow reaches about 129 + 5 = 134px from the card's left edge, i.e. it peeks out about 14px. On the left side it starts at −1, so with blur it only peeks out about 6px. That's why it looks lit from the top-left.
For the curious (and some graphics exams): the specification says the blur should look like a Gaussian blur with a standard deviation equal to half the blur radius. So a 10px shadow blur uses σ = 5px.
Shadows follow border-radius
A shadow has the same shape as the border box, including rounded corners. A circle with border-radius: 50% gets a circular shadow automatically. (This is also why the .ring trick above has rounded corners, which an outline didn't in older browsers.)
Multiple Shadows and Realistic Elevation
Separate shadows with commas. The first shadow is drawn on top.
.two-rings {
box-shadow: 0 0 0 4px #2563eb, /* inner blue ring (on top) */
0 0 0 8px #f59e0b; /* bigger orange ring underneath */
}Output: a 4px blue ring immediately around the card, then a 4px orange ring outside it (8px spread, minus the 4px hidden under the blue one).
Real objects cast a sharp, dark shadow close to them and a soft, faint one further away. Layering a few shadows imitates this much better than one big shadow:
.card {
background: white;
border-radius: 12px;
box-shadow:
0 1px 2px rgb(0 0 0 / 0.08),
0 4px 8px rgb(0 0 0 / 0.08),
0 16px 32px rgb(0 0 0 / 0.12);
}Output: a card that looks gently lifted, with a crisp edge and a wide soft glow below, closer to how design systems like Material Design describe "elevation".
A lifting card on hover
.card {
box-shadow: 0 2px 4px rgb(0 0 0 / 0.1);
transition: box-shadow 200ms ease-out, transform 200ms ease-out;
}
.card:hover {
box-shadow: 0 12px 24px rgb(0 0 0 / 0.15);
transform: translateY(-4px);
}Shadows interpolate smoothly when both states have the same number of shadows with matching inset keywords (a missing shadow is treated as a transparent zero shadow). Animating a big blurred shadow means repainting every frame, which can be slow on weak phones. A common trick is to put the "hover" shadow on a pseudo-element and transition only its opacity, which is much cheaper (see Transitions).
text-shadow
text-shadow works like box-shadow but follows the shape of each letter:
/* offset-x | offset-y | blur | color */
h1 {
text-shadow: 2px 2px 4px rgb(0 0 0 / 0.4);
}
.neon {
color: white;
background: #111;
text-shadow: 0 0 8px #0ff, 0 0 16px #0ff;
}Output: the heading gets a soft dark shadow behind each letter; the neon text glows cyan on black thanks to two stacked, unshifted blurred shadows.
Differences from box-shadow:
box-shadow | text-shadow | |
|---|---|---|
| Shape | The element's border box | The glyphs (letters) |
| Spread value | Yes | No |
inset | Yes | No |
| Inherited? | No | Yes (children's text gets it too) |
Use text shadows lightly. A subtle shadow can improve the readability of white text over a photo; heavy shadows make text harder to read.
The filter Property
filterapplies graphical effects (like blur or colour changes) to an element after it has been drawn, including all its children, text, borders and background.
Think of it as a photo editing filter applied to a screenshot of the element.
img.old-photo {
filter: sepia(1);
}All the filter functions
| Function | Effect | "No change" value | Typical value |
|---|---|---|---|
blur(r) | Gaussian blur; r is the standard deviation | 0 | blur(4px) |
brightness(n) | Multiplies brightness; 0 = black, >1 = brighter | 1 | brightness(1.2) |
contrast(n) | 0 = flat grey, >1 = more contrast | 1 | contrast(1.5) |
grayscale(n) | 1 = fully grey | 0 | grayscale(1) |
sepia(n) | 1 = fully brownish "old photo" | 0 | sepia(0.6) |
saturate(n) | 0 = grey, >1 = more vivid | 1 | saturate(2) |
hue-rotate(angle) | Rotates every colour around the colour wheel | 0deg | hue-rotate(90deg) |
invert(n) | 1 = negative image | 0 | invert(1) |
opacity(n) | Transparency, like the opacity property | 1 | opacity(0.5) |
drop-shadow(x y blur color) | Shadow that follows the visible pixels | — | drop-shadow(0 4px 6px black) |
Amounts can be numbers or percentages: grayscale(1) = grayscale(100%). For grayscale, sepia, invert and opacity, values above 1 are clamped to 1; brightness, contrast and saturate accept values above 1. Negative values are invalid for all of them.
We applied each filter to a rainbow-gradient box in Chrome. grayscale(1) turned it into a grey ramp; sepia(1) into tan and brown; blur(3px) made the edges fuzzy and spread slightly outside the box; brightness(0.5) darkened it to muddy tones; hue-rotate(180deg) turned the red corner blue-green and the blue corner orange; and invert(1) made the white text black.
Combining filters: order matters
Write several functions separated by spaces. They are applied left to right, each one working on the result of the previous one:
.a { filter: grayscale(1) sepia(1); } /* grey first, then tint brown → brown image */
.b { filter: sepia(1) grayscale(1); } /* tint brown, then remove colour → grey image */In our test, .a came out in brownish tones and .b came out plain grey. The last step wins in each case.
Practical uses
/* Logos grey until hovered */
.partner-logo {
filter: grayscale(1) opacity(0.6);
transition: filter 300ms;
}
.partner-logo:hover {
filter: none;
}
/* Disabled look */
.btn:disabled {
filter: grayscale(1) brightness(1.1);
}
/* Darken a hero image so white text on top is readable */
.hero img {
filter: brightness(0.6);
}drop-shadow() vs box-shadow
This is a favourite question. box-shadow always uses the element's rectangular (or rounded) box. filter: drop-shadow() uses the actual visible pixels, so it follows transparent PNGs, SVG icons and shapes made with borders.
/* a triangle made with borders (the box itself is transparent) */
.triangle {
width: 0;
height: 0;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
border-bottom: 60px solid #2563eb;
}
.with-box-shadow { box-shadow: 6px 6px 6px rgb(0 0 0 / 0.5); }
.with-drop-shadow { filter: drop-shadow(6px 6px 6px rgb(0 0 0 / 0.5)); }Output: with box-shadow, a grey rectangle appears behind the triangle, the shape of its 80 × 60 border box. With drop-shadow(), the shadow is a triangle matching the blue shape.
box-shadow | filter: drop-shadow() | |
|---|---|---|
| Shape | The box (with border-radius) | The visible pixels (alpha channel) |
| Spread | Yes | No |
inset | Yes | No |
| Multiple shadows | Comma-separated list | Chain several drop-shadow() functions |
| Performance | Cheaper | More expensive (filters the whole element) |
| Side effects | None | Creates stacking context and containing block (see below) |
Careful with blur values: in
box-shadowanddrop-shadow(), the blur value is twice the Gaussian standard deviation. In theblur()filter, the value is the standard deviation. Sofilter: blur(10px)is noticeably blurrier than a shadow with a 10px blur.
backdrop-filter: Frosted Glass
filter changes the element itself. backdrop-filter changes what is behind the element, seen through it.
<div class="hero">
<div class="glass">Frosted glass</div>
</div>.hero {
position: relative;
background: linear-gradient(135deg, #ef4444, #f59e0b, #22c55e, #3b82f6);
padding: 30px;
}
.glass {
background: rgb(255 255 255 / 0.25); /* must be partly transparent */
backdrop-filter: blur(8px);
border: 1px solid rgb(255 255 255 / 0.4);
border-radius: 12px;
padding: 30px;
}Output: the colourful background and any text behind the panel appear blurred only inside the panel, like frosted glass, while the panel's own text "Frosted glass" stays sharp. In our test, background text that passed behind the panel became unreadable smudges inside it and stayed crisp outside.
Key points:
The element's background must be transparent or semi-transparent; with a solid background you can't see the backdrop at all.
All the
filterfunctions work here too:backdrop-filter: blur(10px) brightness(0.8).It is Baseline (newly available) since September 2024, when Safari 18 supported it without the
-webkit-prefix. For older Safari versions you'll still see-webkit-backdrop-filterwritten alongside.It is relatively expensive. A few glass panels are fine; dozens of large ones may slow scrolling. Also check the text contrast: frosted glass over a busy background can make text hard to read.
Side Effects of filter
Like transform (see Transforms), any filter other than none:
Creates a stacking context, so children's
z-indexvalues only compete inside the filtered element (see z-index & Stacking Context).backdrop-filterdoes too.Becomes the containing block for
position: fixedandposition: absolutedescendants. A "fixed" header inside a filtered wrapper will scroll with the wrapper.Affects all children. You can't un-blur a child of a blurred parent:
filter: noneon the child does nothing because the blur is applied to the parent's whole rendered result.
That third point is why "blur the background but not the modal" is done by blurring a sibling (the page content) or by using backdrop-filter on an overlay, not by blurring a parent of the modal.
Common Mistakes
Mixing up blur and spread. In
0 4px 8px 2px, 8px is blur and 2px is spread. Blur is always the third length.Using solid black shadows.
box-shadow: 0 4px 8px blacklooks harsh. Real shadows are faint: use something likergb(0 0 0 / 0.1)to0.3.Expecting shadows to take space. They overlap neighbours and can be clipped by a parent with
overflow: hidden, which cuts off anything drawn outside the parent's box.Using box-shadow on transparent images or icons and getting a rectangle. Use
filter: drop-shadow().Putting backdrop-filter on an opaque element, which hides the effect completely.
Blurring a parent and trying to keep a child sharp. Restructure the HTML instead.
Forgetting the side effects: a
filterbreaksposition: fixedchildren and changes stacking.
Interview / Exam Questions
1. Explain each value in box-shadow: 2px 4px 6px 8px rgba(0,0,0,0.2).
2px horizontal offset (right), 4px vertical offset (down), 6px blur radius, 8px spread radius (the shadow is 8px bigger on every side before blurring), and a 20% opaque black colour. There is no inset, so it's an outer shadow.
2. A 200px × 100px box has box-shadow: 10px 10px 0 5px black. What is the size of the shadow and where is it?
Spread 5px makes it (200 + 10) × (100 + 10) = 210 × 110. Relative to the box's top-left corner, it starts at (−5 + 10, −5 + 10) = (5, 5) and ends at (215, 115). With 0 blur it has sharp edges, so it sticks out 15px on the right and bottom and is hidden on the top and left.
3. What is the difference between box-shadow and filter: drop-shadow()?
box-shadow follows the element's box (including border-radius) and supports spread and inset. drop-shadow() follows the actual non-transparent pixels, so it works on transparent PNGs and SVG shapes, but has no spread or inset and is more expensive. As a filter, it also creates a stacking context and a containing block.
4. How do you create an inner shadow? Give a use case.
Add the inset keyword, e.g. box-shadow: inset 0 2px 4px rgb(0 0 0 / 0.3). It's used for pressed buttons, input fields and wells that should look recessed.
5. What is the difference between filter and backdrop-filter?
filter applies effects to the element itself and its children. backdrop-filter applies effects to the area behind the element, visible through its transparent or semi-transparent background, which is used for frosted-glass effects.
6. Does filter: grayscale(1) sepia(1) give the same result as filter: sepia(1) grayscale(1)?
No. Filters are applied left to right. The first gives a brownish (sepia) image, because sepia is applied last to the grey result. The second gives a grey image, because grayscale removes the brown tint at the end.
7. Why does a position: fixed header scroll away inside an element that has filter: blur(0px)?
Any filter value other than none (even blur(0px)) makes the element the containing block for fixed-position descendants, so the header is fixed relative to that element, not the viewport.
Summary
box-shadow: x y blur spread color(+ optionalinset) draws shadows that follow the box shape and never affect layout.Spread grows the shadow before blur; blur softens a band about as wide as the blur value, centred on the edge.
Multiple comma-separated shadows (first on top) create realistic elevation and ring effects.
text-shadowfollows letters; it has no spread or inset and is inherited.filterapplies effects likeblur,grayscale,brightness,hue-rotateto the whole rendered element, left to right.drop-shadow()follows visible pixels;box-shadowfollows the box.backdrop-filterfilters what's behind a semi-transparent element: frosted glass.filterandbackdrop-filtercreate a stacking context and a containing block for fixed/absolute descendants.
Shadows and blurred edges often spill outside their boxes, and sometimes they get cut off. In Overflow & Scrolling we'll learn exactly what happens when content doesn't fit inside its box, and how to control scrolling.