Skip to content

Guide

CSS box-shadow, gradients and clip-path: a practical guide

By Vishnu KumarPublished 5 min read

Four CSS features produce most of the depth and shape on modern interfaces: box-shadow, gradients, clip-path and backdrop-filter. Each has a compact syntax that hides a lot of behaviour. This is a reference with the syntax spelled out, examples you can paste, and the mistakes that cost performance or accessibility.

box-shadow: syntax

/* offset-x | offset-y | blur-radius | spread-radius | color */
box-shadow: 0 4px 12px -2px rgb(0 0 0 / 0.25);

/* inset draws inside the border */
box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.15);
  • Offsets move the shadow; positive y moves it down, as if lit from above.
  • Blur softens the edge. It cannot be negative.
  • Spread grows (positive) or shrinks (negative) the shadow before blurring. A negative spread with a larger y offset keeps the shadow under the element instead of around it, which reads as elevation.
  • Colour should be translucent black or a dark tint of the background, not grey: grey shadows look muddy on coloured surfaces.

Layered shadows look real

Real shadows are sharp near the object and soft further away. One box-shadow cannot do that; several, comma-separated, can. Each layer doubles the offset and blur of the one before:

.card {
  box-shadow:
    0 1px 1px rgb(15 23 42 / 0.08),
    0 2px 2px rgb(15 23 42 / 0.08),
    0 4px 4px rgb(15 23 42 / 0.08),
    0 8px 8px rgb(15 23 42 / 0.08),
    0 16px 16px rgb(15 23 42 / 0.08);
}

The first layer listed is painted on top. Keep each layer faint; the darkness comes from the stack.

Animating box-shadow (for example on hover) forces the browser to repaint the blur on every frame. For smooth transitions, put the larger shadow on a pseudo-element and animate its opacity, which the compositor handles cheaply.

.card { position: relative; box-shadow: 0 2px 4px rgb(0 0 0 / 0.1); }
.card::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  box-shadow: 0 12px 24px rgb(0 0 0 / 0.2);
  opacity: 0; transition: opacity 0.2s;
  pointer-events: none;
}
.card:hover::after { opacity: 1; }

Linear gradients

/* direction or angle, then colour stops */
background: linear-gradient(to right, #f97316, #db2777);
background: linear-gradient(135deg, #0ea5e9 0%, #6366f1 50%, #a855f7 100%);

/* hard stops: two stops at the same position make a sharp edge */
background: linear-gradient(90deg, #111 50%, #eee 50%);

/* a colour hint (a bare percentage between stops) moves the midpoint */
background: linear-gradient(90deg, #f97316, 20%, #db2777);

Angles start at the top and go clockwise: 0deg points up, 90deg to the right. to right and 90deg are the same; to bottom right adapts to the box's aspect ratio, while 135deg does not.

Gradients between two saturated colours can pass through a dull grey middle when interpolated in sRGB. Interpolating in OKLCH keeps the middle vivid; current major browsers support it, and older ones ignore the declaration, so put a plain gradient first as a fallback:

background: linear-gradient(90deg, #06b6d4, #f43f5e);
background: linear-gradient(in oklch 90deg, #06b6d4, #f43f5e);

Radial and conic gradients

/* a soft glow from the top-left */
background: radial-gradient(circle at 20% 20%, rgb(99 102 241 / 0.35), transparent 60%);

/* ellipse sized to the nearest side */
background: radial-gradient(ellipse closest-side at center, #fff, #ddd);

/* a pie chart: 40% / 35% / 25% with hard stops */
background: conic-gradient(#22c55e 0 40%, #eab308 0 75%, #ef4444 0);
border-radius: 50%;

/* a checkerboard from one repeating conic gradient */
background: repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 20px 20px;

Writing 0 as a stop position means "start where the previous stop ended", which is the trick that makes hard-edged pie segments short to write. Several gradients can be layered in one background, separated by commas, first on top.

clip-path: cutting shapes

clip-path hides everything outside a shape. The element keeps its full box for layout; only what is drawn, and where it can be clicked, changes.

clip-path: circle(50% at 50% 50%);
clip-path: ellipse(60% 40% at 50% 50%);
clip-path: inset(10% 5% round 1rem);          /* a rounded rectangle cut-out */
clip-path: polygon(50% 0, 100% 100%, 0 100%);  /* a triangle */
clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%); /* a slanted section edge */
clip-path: path("M0 0 H200 V150 Q100 200 0 150 Z"); /* any SVG path, in pixels */
  • Percentages in polygon() are relative to the element's box, so the shape scales with it; path() uses absolute units, so it does not.
  • Two polygons animate smoothly into each other only if they have the same number of points. Add duplicate points to the simpler one to match.
  • Clipping also clips box-shadow and outlines. To shadow a clipped shape, wrap it and use filter: drop-shadow() on the wrapper, which follows the visible shape.
  • Clipped-away areas do not receive clicks, which is usually what you want for shaped buttons.

Glass surfaces with backdrop-filter

"Glassmorphism" blurs whatever is behind a translucent panel. The essential pieces are a partly transparent background and backdrop-filter:

.glass {
  background: rgb(255 255 255 / 0.12);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  backdrop-filter: blur(16px) saturate(140%);
  border: 1px solid rgb(255 255 255 / 0.25);
  border-radius: 1rem;
}
  • Keep the -webkit- line: Safari required the prefix before version 18, and some versions still behave better with it.
  • Blur is computed every frame the content behind it changes. Large blurred areas over video or scrolling content cost real GPU time on low-end phones; keep them small.
  • Check contrast. Text on glass sits on an unpredictable background; a slightly more opaque panel or a text shadow keeps it readable, and WCAG asks for at least 4.5:1 for body text.

Performance and accessibility at a glance

What each effect costs and what to watch
EffectCost to animateWatch for
box-shadowRepaint each frameAnimate a pseudo-element's opacity instead
GradientsRepaint when changedBanding on large areas; text contrast over the lightest stop
clip-pathUsually cheap for simple shapesClipped shadows and focus outlines
backdrop-filterExpensive over moving contentContrast; low-end GPUs
filter: drop-shadowRepaint each frameFollows the shape; heavier than box-shadow

Respect prefers-reduced-motion for any decorative animation built from these, and never rely on a shadow or gradient alone to show focus: keep a visible outline for keyboard users.

For experimenting, CSSForge has generators for layered shadows, all three gradient types, clip paths and glass surfaces, with presets and the CSS shown live. Shapes beyond what clip-path can express are often easier as an inline SVG: see SVG by hand.