On the web

A screen is flat. Every bit of depth you see on it is a shadow somebody drew on purpose.

Then: shadows were pictures

Until CSS could draw them, every soft edge on a web page was an image file: a corner, an edge, a gradient, each one more request to the server. Since about 2011, all current browsers draw shadows themselves, at any size, in any colour, at no cost in downloads.

Three shadows in CSS

box-shadow
A hard offset shadow, no blur: 6px 6px 0
Shadow
text-shadow follows the shape of the letters.
filter: drop-shadow() follows the transparent outline of an image; box-shadow would draw a rectangle.

Shadow lab

In nature, a shadow's direction and length come from the position of the light. CSS needs an x and a y offset. The two are connected by trigonometry, which CSS now has: cos() and sin().

box-shadow:
  calc(cos(var(--angle)) * var(--dist))
  calc(sin(var(--angle)) * var(--dist))
  var(--blur) var(--c-shadow);

A bigger blur means a bigger light source: the same physics as the penumbra.

Elevation

Design systems use shadows as a language: the higher an element seems to float, the more important or temporary it is. One shadow looks flat; several layered ones, each twice as large and as far as the last, look like real light.

pressed in
inset: a hollow
raised
one soft shadow
floating
five layered shadows

On this site the cards rise a little when you point at them: their shadow grows from --shadow-1 to --shadow-2. If your system asks for reduced motion, they only change shadow and do not move.

Shadows in dark mode

A dark shadow on a dark background disappears. That is why, in dark mode, this site makes its shadow colour much more opaque and lightens the surfaces that float, instead of darkening what lies beneath. Try the theme button at the top of the page and watch the cards above.

The Shadow DOM

Web developers have a shadow of their own. A shadow root is a hidden part of the page's document tree that belongs to one element: its styles do not leak out, and the page's styles do not leak in. The browser's own <video> controls and the slider above are built this way.

The word below is a custom element, <shadow-word>. Without JavaScript you see plain text. With it, the element draws its letters and their shadow inside its own shadow root, with styles no other rule on this page can touch:

shadow
class ShadowWord extends HTMLElement {
  connectedCallback() {
    const root = this.attachShadow({ mode: 'open' });
    root.innerHTML = `<style>…</style><span>${this.textContent}</span>`;
  }
}
customElements.define('shadow-word', ShadowWord);

Sources