Depth, Shadow & Lighting — UI/UX

A shadow is written box-shadow: x y blur colour: the move across, the move down, how soft, and its colour. Light comes from above, so the y offset (the move…

A phone screen is flat, yet cards and buttons in an app can look lifted off the page. That sense is called depth. On screen, depth comes from a shadow drawn by CSS with box-shadow.

A convincing shadow copies a real one. Picture a cup of coffee on the table on Mrs. Sri's terrace at noon, with the sun overhead. Right under the cup there's a small, dark, sharp shadow, where the cup touches the table. Around it there's a wide, faint shadow, from the light of the sky coming from every direction. Every object on the table casts its shadow the same way, because there's only one sun. And if you lift the cup, the sharp shadow disappears and the faint one spreads. On screen, the cup is the card and the table is the page. These three things from the table are what we copy in CSS:

How to read box-shadow

One shadow is written with four values. The comment in this code names each value:

.mist {
  /*          x    y    blur  colour */
  box-shadow: 0    6px  16px  rgba(0, 0, 0, 0.08);
}

One light source, from above

In the real world light usually comes from above (the sun, a ceiling lamp), so shadows fall downwards. That's why the y offset is bigger than x: y positive, x zero or small.

The second rule: one page, one sun. Every shadow uses the same direction. If the room card uses 4px 6px (a shadow to the bottom right) and the guest menu above it uses -4px 6px (to the bottom left), the eye picks up two light sources, and the page feels wrong even if nobody can explain why. The safest way: x is always 0.

Two layers: contact and ambient

One shadow can't copy both of the cup's shadows at the same time. So we write two shadows, separated by a comma, in one box-shadow declaration (one pair of a property and its value):

.mist {
  box-shadow:
    0 1px 3px rgba(0, 0, 0, 0.12),   /* contact: close, sharp */
    0 6px 16px rgba(0, 0, 0, 0.08);  /* ambient: far, faint */
}

The contact layer copies the sharp shadow where the cup touches the table. The ambient layer copies the faint shadow from the light all around. Guess first. If the Terrace Room card got only the ambient layer, 0 6px 16px rgba(0, 0, 0, 0.08), would its shadow be darker or lighter right under the card's edge than the contact layer 0 1px 3px rgba(0, 0, 0, 0.12)?

This table shows the red values (0 to 255, smaller is darker; in a grey, red, green and blue are equal) of the pixels under a white card on a white background, measured from the card's bottom edge downwards. The numbers come from the Gaussian blur formula. Chrome, Safari and Firefox draw them within 3 levels of it:

Distance from the card's edge 0px 1px 2px 4px 8px 12px 16px 20px 24px
Contact only 236 244 250 255 255 255 255 255 255
Ambient only 240 240 241 243 247 251 253 254 255
Both layers 221 230 237 243 247 251 253 254 255

Lighter. At the card's edge the ambient shadow is only #f0f0f0, while the contact one is #ececec. But the contact shadow is gone by 4px, while the ambient one is still visible to about 22px. Each one gives only half the picture. Contact gives the firm line where the card touches the page, and ambient gives the sense of height. Together, the darkest pixel is #dddddd (1.36:1 against white), and the shadow fades smoothly. This trick is called layered shadows.

Elevation as tokens

Elevation is how high a surface sits above the page, as in the dark UI lesson. The higher the surface, the bigger the ambient layer's offset and blur, so the shadow reaches further. Store each level as a token: a CSS variable whose name says its job, like the colour token --action:

:root {
  --shadow-raised:   0 1px 3px rgba(0, 0, 0, 0.12), 0 6px 16px rgba(0, 0, 0, 0.08);
  --shadow-floating: 0 2px 4px rgba(0, 0, 0, 0.12), 0 12px 24px rgba(0, 0, 0, 0.12);
  --shadow-overlay:  0 4px 8px rgba(0, 0, 0, 0.12), 0 24px 48px rgba(0, 0, 0, 0.16);
}
.mist   { box-shadow: var(--shadow-raised); }    /* room card */
.menu    { box-shadow: var(--shadow-floating); }  /* guest count picker */
.dialog  { box-shadow: var(--shadow-overlay); }   /* booking confirmation */

This table shows where each token is used, its darkest pixel, and how far from the card's edge its shadow is gone:

Token Used for Darkest pixel Gone at
no shadow the page background, the night buttons inside the card
--shadow-raised the Terrace, Attic and Garden Room cards #dddddd (1.36:1) 22px
--shadow-floating the open "2 guests" menu over the card #d0d0d0 (1.54:1) 38px
--shadow-overlay the "Confirm booking" dialog #c7c7c7 (1.69:1) 78px

Components at the same level use the same token: all three room cards use --shadow-raised. If a card is clickable, it may rise one level to --shadow-floating on hover.

Shadows on dark surfaces

In the dark mode of Mrs. Sri's page (background #111827, as in the dark UI lesson), the same --shadow-raised gives a darkest pixel of #0f1522. That's only 1.03:1 against the background. By 14px from the card's edge, the shadow can't be told apart from the background at all. Darkening a colour that is already dark makes no difference.

On dark surfaces, elevation is shown with a lighter surface: a #1f2937 card on a #111827 background (1.21:1), and a menu above the card at #374151 (1.42:1 against the card). The shadow may stay, but it mustn't be the only thing that separates them.

⚠️ Why does the shadow look fake?

TL;DR: A shadow is written box-shadow: x y blur colour: the move across, the move down, how soft, and its colour. Light comes from above, so the y offset (the move down) is bigger than x, and x = 0 is safest. Every shadow on the page uses the same direction: one light source. A natural shadow has two layers (layered shadows) in one box-shadow, separated by a comma. The sharp contact layer (0 1px 3px rgba(0, 0, 0, 0.12), the shadow where the card touches the page) is gone 4px below the card's edge. The faint ambient layer (0 6px 16px rgba(0, 0, 0, 0.08), from the light all around) is visible to about 22px. Store elevation levels (how high a surface sits above the page) as tokens (CSS variables whose names say their job): --shadow-raised for cards, --shadow-floating for menus, --shadow-overlay for dialogs. The higher, the further the shadow reaches. On dark surfaces a shadow is barely visible (1.03:1), so elevation is shown with a lighter surface (#1f2937 on #111827).

⚡ Light from above

Guess first: in box-shadow, the Y offset moves the shadow down and the X offset moves it sideways. Why should Y be bigger than X?

Because light comes from above, like the sun or a ceiling lamp, so shadows fall downwards.

Meaning: One page, one light source (one sun). Every shadow uses the same direction, and the safest x is always 0.

⚡ Two layers: contact and ambient

Write:

.mist {
  box-shadow:
    0 1px 3px rgba(0, 0, 0, 0.12),   /* contact: close, sharp */
    0 6px 16px rgba(0, 0, 0, 0.08);  /* ambient: far, faint */
}

Meaning: This .mist card's shadow has two layers. The contact layer (0 1px 3px) gives the firm line where the card touches the page, and is gone 4px below the card's edge. The ambient layer (0 6px 16px) gives the sense of height, and stays visible to about 22px. Write both layers in one box-shadow, separated by a comma. This trick is called layered shadows.

⚡ Elevation as tokens

Write:

:root {
  --shadow-raised:   0 1px 3px rgba(0, 0, 0, 0.12), 0 6px 16px rgba(0, 0, 0, 0.08);
  --shadow-floating: 0 2px 4px rgba(0, 0, 0, 0.12), 0 12px 24px rgba(0, 0, 0, 0.12);
}

Meaning: Elevation is how high a surface sits above the page. Store each level as a token (a CSS variable whose name says its job). The higher the surface, the further its shadow reaches: cards get --shadow-raised, menus --shadow-floating. In dark mode a shadow differs from the background by only 1.03:1, so elevation is shown with a lighter surface.

What you will learn