EthicsComputerCourses
Join training +91 90532 10052

CSS Shadows

CSS · Lesson 106 of 240 Intermediate 10 min read Updated

By the end of this lesson you will know the three ways CSS can draw a shadow, pick the right one for a card, a heading or a transparent logo, and use shadows to show how "high" an element sits on the page. You will also learn what shadows cost the browser, so your pages stay smooth on budget phones.

You met box-shadow and text-shadow briefly in CSS Borders & Outlines. This lesson is the map of the whole topic. The next lessons go deeper: Box Shadow for cards and buttons, and Text Shadow for lettering.

Why shadows matter: light and depth

Hold your phone a few centimetres above a table under a tube light. Its shadow is sharp and close. Lift the phone higher and the shadow moves further away, grows bigger and gets softer at the edges. Your brain reads that change as height.

Web designers use the same idea. A flat screen has no real depth, but a shadow tells the reader "this card sits above the page" or "this pop-up is on top of everything". This sense of height is called elevation. Three rules make shadows look natural:

  • Light usually comes from above, so shadows fall downwards (a positive y offset).
  • Higher elements get a larger offset and a larger, softer blur.
  • Real shadows are never pure black. Use black at low opacity, such as rgb(0 0 0 / 0.15).

The three shadow tools

CSS has three different ways to draw a shadow. They look similar but follow different shapes.

ToolWhat the shadow followsSpreadInsetTypical use
box-shadowThe element's box, including rounded cornersYesYesCards, buttons, menus, inputs
text-shadowThe letters of the textNoNoHeadings on photos, glows
filter: drop-shadow()The visible pixels, ignoring transparent partsNoNoTransparent PNG or SVG icons, odd shapes

Spread grows or shrinks the shadow before it is blurred. Inset draws the shadow inside the box instead of outside. Only box-shadow has both.

box-shadow and text-shadow accept a comma-separated list of shadows. drop-shadow() takes one shadow, but you can chain several drop-shadow() functions inside one filter value.

Step 1: one card, three shadows

Here is a menu card for a chai shop in Pune. The card uses box-shadow, the heading on the coloured strip uses text-shadow, and the two avatar images show the difference between box-shadow and drop-shadow() on a round image with transparent corners.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Three shadow tools</title>
  <style>
    body { font-family: system-ui, sans-serif; background: #f4f1ec; padding: 24px; }
    .card {
      max-width: 340px;
      background: white;
      border-radius: 14px;
      overflow: hidden;
      box-shadow: 0 6px 16px rgb(0 0 0 / 0.15);
    }
    .strip { background: #b45309; padding: 16px; }
    .strip h1 {
      margin: 0;
      color: white;
      font-size: 1.6rem;
      text-shadow: 0 2px 3px rgb(0 0 0 / 0.45);
    }
    .people { display: flex; gap: 24px; padding: 16px; }
    .people figure { margin: 0; text-align: center; font-size: 0.85rem; }
    .people img { width: 80px; height: 80px; display: block; margin-bottom: 8px; }
    .with-box  { box-shadow: 4px 6px 6px rgb(0 0 0 / 0.35); }
    .with-drop { filter: drop-shadow(4px 6px 3px rgb(0 0 0 / 0.35)); }
  </style>
</head>
<body>
  <article class="card">
    <div class="strip"><h1>Meera's Chai Point</h1></div>
    <div class="people">
      <figure><img class="with-box" src="https://course.ethicscomputer.in/assets/samples/avatar.svg" alt="Owner photo"><figcaption>box-shadow</figcaption></figure>
      <figure><img class="with-drop" src="https://course.ethicscomputer.in/assets/samples/avatar.svg" alt="Owner photo"><figcaption>drop-shadow()</figcaption></figure>
    </div>
  </article>
</body>
</html>

What you should see: the whole card floats softly above the beige page. The white heading has a thin dark shadow that lifts it off the orange strip. The left avatar has a square shadow, because box-shadow follows the rectangle of the <img> element, even though the corners of the picture are transparent. The right avatar has a round shadow, because drop-shadow() looks at the actual pixels.

Choosing the right tool

Ask one question first: "What shape should the shadow have?"

  • A box (card, button, panel, input): use box-shadow. It is the cheapest to draw and the only one with spread and inset.
  • Letters: use text-shadow. It follows each letter and works on any text, including text inside a button.
  • A shape that is not a box: use filter: drop-shadow(). This covers transparent PNG and SVG images, a logo cut out of its background, and an element plus its pseudo-elements, such as a tooltip with a small arrow.

The last case is easy to miss. drop-shadow() works on everything the element paints, including its children and its ::before and ::after boxes. That lets one shadow cover a combined shape.

Step 2: a tooltip whose arrow gets a shadow too

A tooltip is often a rounded box plus a small triangle made with ::after. With box-shadow, the triangle has no shadow, so it looks pasted on. With drop-shadow() on the tooltip, the shadow wraps around the box and the arrow as one shape.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Tooltip shadows</title>
  <style>
    body { font-family: system-ui, sans-serif; background: #eef2f7; padding: 24px; display: flex; gap: 40px; flex-wrap: wrap; align-items: flex-start; }
    .tip {
      position: relative;
      background: white;
      padding: 12px 16px;
      border-radius: 10px;
      max-width: 200px;
    }
    .tip::after {
      content: "";
      position: absolute;
      left: 24px;
      bottom: -16px;
      border: 16px solid transparent;
      border-bottom: 0;
      border-top-color: white;
    }
    .box  { box-shadow: 0 8px 10px rgb(0 0 0 / 0.35); }
    .drop { filter: drop-shadow(0 8px 5px rgb(0 0 0 / 0.35)); }
  </style>
</head>
<body>
  <p class="tip box">box-shadow: the arrow has no shadow.</p>
  <p class="tip drop">drop-shadow(): one shadow for box and arrow.</p>
</body>
</html>

What you should see: the left tooltip's arrow looks flat. The right tooltip's arrow casts a shadow that joins smoothly with the box. Notice the blur values differ: drop-shadow() blurs about twice as strongly as box-shadow for the same number, so use roughly half the value to get a matching softness.

Step 3: an elevation scale

Instead of inventing a new shadow for every component, professional teams agree on a small elevation scale: three to five named shadow levels. A flat list item is level 0, a card is level 1, a dropdown menu is level 2 and a modal is level 3. Saving the levels in custom properties keeps the whole site consistent.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Elevation scale</title>
  <style>
    :root {
      --elevation-1: 0 1px 2px rgb(0 0 0 / 0.12), 0 1px 3px rgb(0 0 0 / 0.08);
      --elevation-2: 0 2px 4px rgb(0 0 0 / 0.10), 0 6px 12px rgb(0 0 0 / 0.10);
      --elevation-3: 0 4px 8px rgb(0 0 0 / 0.10), 0 14px 28px rgb(0 0 0 / 0.14);
    }
    body { font-family: system-ui, sans-serif; background: #f1f5f9; padding: 24px; }
    .row { display: flex; gap: 20px; flex-wrap: wrap; }
    .tile {
      width: 140px;
      padding: 20px 12px;
      background: white;
      border-radius: 12px;
      text-align: center;
      transition: box-shadow 0.2s, translate 0.2s;
    }
    .e0 { box-shadow: none; border: 1px solid #cbd5e1; }
    .e1 { box-shadow: var(--elevation-1); }
    .e2 { box-shadow: var(--elevation-2); }
    .e3 { box-shadow: var(--elevation-3); }
    .e1:hover { box-shadow: var(--elevation-2); translate: 0 -2px; }
  </style>
</head>
<body>
  <div class="row">
    <div class="tile e0">Level 0<br>list row</div>
    <div class="tile e1">Level 1<br>card (hover me)</div>
    <div class="tile e2">Level 2<br>dropdown</div>
    <div class="tile e3">Level 3<br>modal</div>
  </div>
</body>
</html>

What you should see: four white tiles that seem to sit at four different heights. Hovering the level 1 card raises it to level 2. Each level has two layers: a small sharp shadow close to the tile and a larger soft one further away. You will build layered shadows like these in detail in Box Shadow.

Shadows on dark backgrounds

A dark shadow on a dark page is almost invisible. In a dark theme, elevation is usually shown by making higher surfaces slightly lighter (for example #1e293b for a card on a #0f172a page), with a subtle border, and a stronger, darker shadow only for pop-ups. Keep this in mind when you reach Dark Mode Using CSS.

A performance note

Shadows are drawn during the paint step, when the browser fills in pixels. A few shadows are cheap. Problems start in three cases:

  • Very large blur values on big areas. Blurring is work for every pixel it touches.
  • Animating a shadow. Changing box-shadow on hover is fine, but a shadow that animates every frame forces a repaint every frame. On a long list this can make scrolling stutter. Box Shadow shows a cheaper trick using opacity.
  • drop-shadow() everywhere. A filter must first work out the element's shape. It usually costs more than box-shadow, so use it for icons and odd shapes, not for every card in a grid of 200 products.

Common mistakes

  • Using pure black shadows. 5px 5px 0 black looks dated. Use low-opacity colours and some blur.
  • Shadows pointing in different directions. If one card's shadow falls left and another's falls down, the page looks like it has two suns. Keep one light direction everywhere.
  • box-shadow on a transparent icon. You get a rectangle. Use drop-shadow().
  • A parent with overflow: hidden cutting off the shadow. Shadows draw outside the box, so a clipping parent hides them. Add padding to the parent or move the clip.
  • Inventing a new shadow for every component. Use a small elevation scale instead.

Interview questions

1. What is the difference between box-shadow and filter: drop-shadow()?

box-shadow always follows the element's border box and its border-radius. drop-shadow() follows the actual painted pixels, so it ignores transparent areas and includes children and pseudo-elements. box-shadow supports spread, inset and a comma-separated list; drop-shadow() supports none of these, although you can chain several drop-shadow() functions.

2. When would you choose text-shadow instead of drop-shadow() for text?

For plain text, text-shadow is the natural choice. It is made for text, accepts a list of shadows and is cheaper than a filter. drop-shadow() on a text element would also shadow the element's background and borders, which is usually not wanted.

3. What is elevation, and how do you show it with shadows?

Elevation is the apparent height of an element above the page. Higher elements get a larger y offset and a larger, softer blur, often built from two layered shadows. Teams keep a small scale of named levels, such as card, dropdown and modal, so depth is consistent.

4. Do shadows affect layout?

No. Shadows take up no space, do not push other elements and do not change the element's size. They can still be clipped by an ancestor with overflow: hidden.

5. Why can animating a shadow hurt performance?

Shadows are painted, so changing one forces the browser to repaint that area. Doing that every frame on many elements, or with large blurs, can drop frames on slower devices. A common fix is to keep the shadow fixed and animate the opacity of a pseudo-element that holds the bigger shadow.

Key points

  • CSS has three shadow tools: box-shadow for boxes, text-shadow for letters and filter: drop-shadow() for any visible shape.
  • drop-shadow() follows transparent PNG and SVG images and also covers children and pseudo-elements.
  • Only box-shadow has spread and inset.
  • Natural shadows fall downwards, use low opacity and get bigger and softer as elevation rises.
  • Keep a small elevation scale in custom properties instead of one-off shadows.
  • Shadows are painted: avoid huge blurs, per-frame shadow animation and drop-shadow() on large lists.

Exercise

Ananya runs a sweet shop in Jaipur and wants a product page with three parts. Build it in one HTML file: a product card with a level 1 shadow that rises to level 2 on hover, a "Bestseller" heading in white on a dark red strip with a subtle text shadow, and the sample icon.svg image shown at 64px with a shadow that follows its rounded shape. Store your shadow levels in custom properties.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Jaipur Sweets</title>
  <style>
    :root {
      --elevation-1: 0 1px 2px rgb(0 0 0 / 0.12), 0 2px 6px rgb(0 0 0 / 0.08);
      --elevation-2: 0 3px 6px rgb(0 0 0 / 0.10), 0 10px 20px rgb(0 0 0 / 0.12);
    }
    body { font-family: system-ui, sans-serif; background: #fdf6ec; padding: 24px; }
    .card {
      max-width: 300px;
      background: white;
      border-radius: 12px;
      overflow: hidden;
      box-shadow: var(--elevation-1);
      transition: box-shadow 0.2s;
    }
    .card:hover { box-shadow: var(--elevation-2); }
    .strip { background: #991b1b; padding: 12px 16px; }
    .strip h2 { margin: 0; color: white; text-shadow: 0 1px 2px rgb(0 0 0 / 0.5); }
    .body { padding: 16px; display: flex; gap: 12px; align-items: center; }
    .body img { width: 64px; filter: drop-shadow(0 3px 3px rgb(0 0 0 / 0.3)); }
  </style>
</head>
<body>
  <article class="card">
    <div class="strip"><h2>Bestseller</h2></div>
    <div class="body">
      <img src="https://course.ethicscomputer.in/assets/samples/icon.svg" alt="Star badge">
      <p>Ghewar, 500 g box, ₹480</p>
    </div>
  </article>
</body>
</html>

The card uses box-shadow because it is a box, the heading uses text-shadow because it is text, and the icon uses drop-shadow() so the shadow follows its rounded corners. Both elevation levels live in custom properties, so any other card can reuse them.

Quick Quiz

1. A transparent PNG logo needs a shadow only under the drawn logo. Which tool fits?

2. Which shadow tool supports both spread and the inset keyword?

3. A card should look higher above the page. What usually changes in its shadow?

4. Why does a tooltip arrow made with ::after get a shadow from drop-shadow() but not from box-shadow?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call