Color scheme

Shadows and Gradients

Color tokens give your site a consistent palette. Dark mode makes it adaptive. But a flat page with uniform backgrounds still feels like a prototype. Shadows and gradients are what bring an interface to life. A well placed shadow lifts a card off the page and tells the user it is interactive. A subtle gradient gives a header depth without adding a single image. These effects create the illusion of light, surface, and dimension that makes a design feel real and three dimensional rather than printed on paper.

This page covers the CSS properties that produce those effects: box-shadow, text-shadow, linear-gradient(), radial-gradient(), and conic-gradient(). You will learn how to layer multiple shadows for realistic elevation, how to choose the right gradient type for a given situation, and how to use these tools with the oklch() colors and tokens you built in Part A. Everything here feeds directly into the Visual Effects assignment, where you will apply shadows and gradients to your own portfolio components.

How Shadows Create Depth

In the physical world, objects that sit above a surface cast shadows. The higher the object, the larger and softer its shadow becomes. CSS lets you simulate this with box-shadow, and the trick to making shadows look realistic is understanding that a single shadow almost never looks right. Real light produces multiple layers of shadow: a tight, dark shadow close to the object where light is fully blocked, and a softer, wider shadow further out where light diffuses around the edges. Stacking two or three shadows together produces the kind of depth that makes a card, a button, or a modal feel like it genuinely floats above the page.

The box-shadow Property

The box-shadow property accepts a comma separated list of shadow layers. Each layer takes up to six values:


        box-shadow: <offset-x> <offset-y> <blur> <spread> <color>;
    
  • offset-x and offset-y control how far the shadow shifts from the element. Positive x moves right, positive y moves down.
  • blur controls how soft the shadow edge is. A value of 0 produces a hard edge. Larger values create a more diffuse glow.
  • spread expands or contracts the shadow before blurring. A positive value makes the shadow larger than the element, and a negative value makes it smaller.
  • color sets the shadow color. Use a semi-transparent value so the shadow blends naturally with whatever sits behind it.

An optional inset keyword placed before the offsets moves the shadow inside the element, creating a pressed or recessed appearance instead of an elevated one.

Single Shadows versus Layered Shadows

A single shadow can work for simple states like a subtle border replacement or a focus ring, but it tends to look flat and artificial for elevation. Compare these two approaches:


        /* Single shadow: functional but flat */
        .card {
            box-shadow: 0 4px 12px oklch(0.2 0 0 / 0.15);
        }

        /* Layered shadow: realistic depth */
        .card {
            box-shadow:
                0 1px 2px oklch(0.2 0 0 / 0.12),
                0 4px 8px oklch(0.2 0 0 / 0.08),
                0 12px 24px oklch(0.2 0 0 / 0.06);
        }
    

The layered version uses three shadows at different distances. The first layer is tight and relatively dark, simulating the shadow directly under the element where almost no light reaches. The second layer is wider and lighter, representing the penumbra where light partially wraps around the edge. The third layer is very wide and barely visible, adding an ambient glow that grounds the element on the page. Together, these three layers mimic how light actually behaves around a raised surface.

Elevation as a System

Rather than inventing a new shadow for every component, define a small set of elevation levels and reuse them through custom properties. This keeps your depth language consistent across the entire site. A common approach uses three to five levels:


        :root {
            /* Level 1: buttons, inputs, low cards */
            --shadow-sm:
                0 1px 2px oklch(0.2 0 0 / 0.1),
                0 2px 4px oklch(0.2 0 0 / 0.06);

            /* Level 2: raised cards, dropdowns */
            --shadow-md:
                0 2px 4px oklch(0.2 0 0 / 0.1),
                0 6px 12px oklch(0.2 0 0 / 0.07),
                0 16px 32px oklch(0.2 0 0 / 0.05);

            /* Level 3: modals, popovers, dialogs */
            --shadow-lg:
                0 4px 8px oklch(0.2 0 0 / 0.1),
                0 12px 24px oklch(0.2 0 0 / 0.08),
                0 24px 48px oklch(0.2 0 0 / 0.06);
        }

        .card       { box-shadow: var(--shadow-md); }
        .modal      { box-shadow: var(--shadow-lg); }
        .btn        { box-shadow: var(--shadow-sm); }
    

Notice that the shadow color uses oklch(0.2 0 0) with zero chroma, which produces a neutral dark tone. Using a low lightness value with transparency lets the shadow blend naturally regardless of what sits behind the element. If your design calls for colored shadows (a blue card casting a blue tinted shadow, for example), replace the zero chroma with a small amount of the element's accent color. This is a subtle detail, but it adds a level of realism that users notice even if they cannot articulate why.

Shadows and Dark Mode

Shadows behave differently on dark backgrounds. A black shadow on a near-black surface is invisible, so dark mode shadows often need higher opacity or a slight color shift to remain visible. One approach is to define separate shadow tokens for light and dark modes using the light-dark() function you learned in Part A, or to increase the opacity values in your dark mode overrides.

Text Shadows

The text-shadow property works similarly to box-shadow but applies to the text content of an element rather than its box. It takes offset, blur, and color values, but does not support spread or inset:


        text-shadow: <offset-x> <offset-y> <blur> <color>;
    

Text shadows serve two very different purposes depending on how you use them. A subtle, barely visible shadow can improve legibility when text sits on a busy or photographic background by adding a faint halo that separates the letterforms from the noise behind them. A more dramatic shadow can create decorative effects like embossing, letterpress, or neon glow. The key distinction is intent: if the shadow makes the text easier to read, it is serving the content. If it is purely decorative, it should be used sparingly and never at the expense of readability.


        /* Subtle legibility shadow for text over images */
        .hero-title {
            text-shadow: 0 1px 3px oklch(0.1 0 0 / 0.5);
        }

        /* Layered glow effect */
        .neon-text {
            text-shadow:
                0 0 4px oklch(0.7 0.2 250 / 0.8),
                0 0 12px oklch(0.7 0.2 250 / 0.4),
                0 0 24px oklch(0.7 0.2 250 / 0.2);
        }
    

Like box-shadow, text-shadow accepts a comma separated list of layers. The neon example above stacks three glow layers at increasing blur radii with decreasing opacity, creating the soft falloff that makes neon signage look convincing. Text shadows can also be layered with box-shadow on the same element to build complex visual treatments, but restraint matters. A shadow that calls attention to itself rather than to the content it supports is working against you.

Linear Gradients

A gradient is a smooth transition between two or more colors rendered as a background image. Unlike solid background colors, gradients occupy the background-image property (though the background shorthand works too), which means they can be layered with other background images and positioned independently.

The linear-gradient() function draws a color transition along a straight line. By default, the gradient runs from top to bottom, but you can change the direction with an angle or a keyword:


        /* Top to bottom (default) */
        background: linear-gradient(oklch(0.95 0.02 250), oklch(0.85 0.04 250));

        /* Left to right */
        background: linear-gradient(to right, oklch(0.95 0.02 250), oklch(0.85 0.04 250));

        /* Diagonal at 135 degrees */
        background: linear-gradient(135deg, oklch(0.95 0.02 250), oklch(0.85 0.04 250));
    

Each color in the list is called a color stop. You can add as many stops as you need, and you can control exactly where each stop begins by adding a percentage or length value after the color:


        /* Three-stop gradient with explicit positions */
        background: linear-gradient(
            to right,
            oklch(0.45 0.2 260) 0%,
            oklch(0.6 0.18 300) 50%,
            oklch(0.75 0.15 340) 100%
        );

        /* Hard stop: no transition, creates a stripe */
        background: linear-gradient(
            to right,
            oklch(0.45 0.2 260) 50%,
            oklch(0.75 0.15 340) 50%
        );
    

When two adjacent color stops share the same position (like the 50% hard stop above), the transition between them is instant, creating a sharp edge instead of a smooth blend. This technique is useful for creating striped patterns, progress bars, and decorative dividers without any images.

Because gradients are background images, you can use background-size and background-repeat to tile them into repeating patterns. The repeating-linear-gradient() function does this automatically. The Advanced Gradients choose topic explores these techniques in depth.

Radial Gradients

Where linear gradients follow a straight line, radial-gradient() radiates outward from a center point. The default shape is an ellipse that fills the element, but you can specify a circle or control the size and position explicitly:


        /* Default: ellipse from center */
        background: radial-gradient(oklch(0.95 0.01 80), oklch(0.85 0.03 60));

        /* Circle from the top-left corner */
        background: radial-gradient(circle at top left, oklch(0.95 0.01 80), oklch(0.85 0.03 60));

        /* Sized circle from a specific position */
        background: radial-gradient(circle 200px at 30% 40%, oklch(0.95 0.01 80), oklch(0.85 0.03 60));
    

Radial gradients are particularly effective for creating spotlight or vignette effects. A semi-transparent radial gradient layered over a background image can draw the eye toward the center of a hero section by darkening the edges. They also work well for creating soft glows behind icons, avatars, or call to action buttons, giving those elements a sense of luminance without resorting to images.


        /* Vignette overlay on a hero section */
        .hero {
            background:
                radial-gradient(
                    ellipse at center,
                    transparent 40%,
                    oklch(0.1 0 0 / 0.6) 100%
                ),
                url('/images/hero.jpg') center / cover;
        }
    

Notice that the gradient is listed first in the background shorthand. Because CSS paints background layers in stack order (first listed is on top), the gradient sits over the image and darkens the edges while leaving the center clear.

Conic Gradients

The conic-gradient() function sweeps color around a center point like the hands of a clock, rather than along a line or outward from a point. The gradient starts at the top by default and rotates clockwise:


        /* Simple two-color sweep */
        background: conic-gradient(oklch(0.5 0.2 260), oklch(0.7 0.15 340));

        /* Pie chart: hard stops create distinct slices */
        background: conic-gradient(
            oklch(0.55 0.2 260) 0% 40%,
            oklch(0.65 0.18 140) 40% 70%,
            oklch(0.7 0.12 50) 70% 100%
        );
    

Conic gradients are less common in everyday UI work than linear or radial gradients, but they have specific strengths. They are the natural choice for pie and donut charts rendered entirely in CSS, for color wheel pickers, and for certain decorative patterns like sunbursts or checkerboards when combined with repeating-conic-gradient(). If you are interested in exploring these patterns further, the Advanced Gradients and @property choose topic covers repeating patterns and gradient animation in detail.

Choosing the Right Gradient

With three gradient types available, the question becomes which one to reach for. The choice usually comes down to the visual metaphor you are trying to create:

  • Linear is the workhorse. Use it for directional transitions like a header that fades from one brand color to another, a card background that subtly shifts from light to slightly lighter, or a decorative stripe pattern. If the effect follows a straight line, linear is the right tool.
  • Radial creates a sense of focus or illumination. Use it when you want to draw attention to a center point, create a spotlight or vignette, or simulate a soft glow. If the effect radiates outward from a point, radial is the right tool.
  • Conic rotates around a point. Use it for circular data visualizations, color wheels, or sweeping decorative effects. If the effect wraps around a center, conic is the right tool.

Most day to day UI work uses linear and radial gradients. Conic gradients tend to appear in more specialized contexts. Regardless of which type you choose, subtle gradients almost always serve the design better than dramatic ones. A two percent lightness shift across a card background adds dimension that users feel without consciously noticing. A rainbow gradient across a navigation bar demands attention and usually distracts from the content it is supposed to support.

When Effects Support Content Hierarchy

Shadows and gradients are powerful, but they are also easy to overuse. The purpose of every visual effect should be to reinforce the content hierarchy, not to decorate for decoration's sake. A shadow tells the user that something is elevated, interactive, or important. A gradient creates a sense of surface or direction. When effects are applied intentionally, they guide the eye and clarify structure. When they are applied everywhere, they create visual noise that makes everything compete for attention.

A useful test is to ask what information the effect communicates. A shadow on a card says "this is a distinct, interactive surface." A shadow on every paragraph says nothing, because if everything is elevated, nothing is. The same principle applies to gradients: a subtle gradient on a page header creates a sense of depth and polish, but five different gradients on the same page create chaos. The restraint you show in applying these effects is just as important as the technique itself.

A design principle worth remembering: every visual effect should have a job. If you cannot explain what a shadow or gradient communicates to the user, it probably should not be there. This does not mean effects should be rare. It means each one should earn its place by making the interface clearer, more usable, or more inviting.

Guided Demos

The demos below let you experiment with layered shadows and gradient types. Use the color scheme toggle inside each demo to switch between light and dark mode and see how the effects adapt:

Shadow Elevation

Three cards at different elevation levels. Click any card to cycle through low, medium, and high so you can feel how layered shadows change the perception of depth. Toggle dark mode to see how the shadow strategy changes on dark surfaces:

Shadow Elevation Levels

Color scheme

Click a card to cycle: low → medium → high → low

Level 1

Low Elevation

Buttons, inputs, flat cards

Level 2

Medium Elevation

Raised cards, dropdowns

Level 3

High Elevation

Modals, dialogs, popovers

Gradient Types

Linear, radial, and conic gradients side by side using the same pair of colors. Drag the hue slider to shift the palette and see how each gradient type renders the same color transition differently:

Gradient Type Comparison

Color scheme
260°

Linear

Radial

Conic

Resources

  • MDN: box-shadow — Full syntax reference including spread, inset, and multiple shadow layers
  • MDN: text-shadow — Property reference with examples for legibility and decorative use
  • MDN: CSS Gradients — Overview of linear, radial, conic, and repeating gradient functions
  • Smooth Shadow Generator — Interactive tool for generating layered shadow stacks with realistic depth
  • CSS-Tricks: box-shadow — Practical recipes for elevation, colored shadows, and inset effects