Color scheme

Advanced Gradients and @property

The core readings covered linear-gradient(), radial-gradient(), and conic-gradient() as single-use background effects. This choose topic pushes gradients further in two directions. First, repeating gradients turn a small pattern definition into tileable stripes, grids, and textures that scale through background-size alone. Second, the @property at-rule lets you register typed custom properties so the browser can interpolate gradient values that are normally impossible to animate. Together, these two techniques are the modern gradient power stack: patterns and motion without a single image file.

Repeating Gradients

The repeating-linear-gradient(), repeating-radial-gradient(), and repeating-conic-gradient() functions work exactly like their non-repeating counterparts, but the color stop pattern tiles infinitely instead of stretching to fill the element. The final color stop defines where the pattern ends and the next repetition begins:


        /* Diagonal stripes: 10px dark, 10px light, repeating */
        .stripes {
            background: repeating-linear-gradient(
                45deg,
                oklch(0.3 0 0) 0px 10px,
                oklch(0.4 0 0) 10px 20px
            );
        }
    

The double color stop syntax (0px 10px) is shorthand for placing both the start and end of a color at specific positions, which creates hard-edged stripes with no transition between them. This is the foundation for every repeating gradient pattern: define a small unit of color stops, and the browser tiles it across the element.

background-size as the Control Knob

The real power of pattern making comes from combining a regular linear-gradient() (not repeating) with background-size. You define a gradient that fills one tile, set the tile size, and the browser's default background-repeat: repeat handles the rest. This approach often renders more cleanly than repeating-linear-gradient(), which can produce sub-pixel rendering artifacts at certain angles and stripe widths:


        /* Horizontal stripes via background-size tiling */
        .h-stripes {
            background: linear-gradient(
                to bottom,
                oklch(0.35 0.15 260) 50%,
                oklch(0.45 0.12 260) 50%
            );
            background-size: 100% 20px;
        }

        /* Checkerboard: two offset gradients stacked */
        .checkerboard {
            background:
                linear-gradient(45deg,
                    oklch(0.3 0 0) 25%, transparent 25%,
                    transparent 75%, oklch(0.3 0 0) 75%),
                linear-gradient(45deg,
                    oklch(0.3 0 0) 25%, transparent 25%,
                    transparent 75%, oklch(0.3 0 0) 75%);
            background-size: 40px 40px;
            background-position: 0 0, 20px 20px;
        }

        /* Dot grid: radial gradient tiled */
        .dots {
            background: radial-gradient(
                circle, oklch(0.5 0.15 260) 2px, transparent 2px
            );
            background-size: 20px 20px;
        }
    

Notice that changing background-size alone scales the entire pattern. A 20px tile makes tight stripes. A 60px tile makes wide ones. The gradient definition stays the same. This is what the scaffold means by background-size being the main control knob: you define the pattern once and resize it by changing a single value.

Lea Verou's CSS3 Patterns Gallery

The CSS3 Patterns Gallery is the definitive collection of gradient-only patterns, from simple stripes to complex tartan and Japanese cube designs. Study how each pattern uses multiple stacked gradients, background-size, and background-position to build complexity from simple building blocks.

Animating Gradients with @property

Try transitioning a background gradient and nothing happens. The color snaps instantly from one state to the next. This is because the browser treats unregistered custom properties (and gradient values in general) as opaque strings. It has no way to know that --stop-color is a color or that --angle is an angle, so it cannot interpolate between them.

The @property at-rule solves this by registering a custom property with an explicit type. Once the browser knows the type, it can interpolate the value smoothly through transitions and keyframe animations:


        @property --gradient-angle {
            syntax: "<angle>";
            inherits: false;
            initial-value: 0deg;
        }

        .shimmer {
            background: linear-gradient(
                var(--gradient-angle),
                oklch(0.5 0.2 260),
                oklch(0.7 0.15 310)
            );
            animation: rotate-gradient 4s linear infinite;
        }

        @keyframes rotate-gradient {
            to {
                --gradient-angle: 360deg;
            }
        }
    

Without the @property registration, the animation would snap between 0deg and 360deg on each frame. With it, the browser interpolates the angle smoothly, rotating the gradient direction continuously. The same technique works for animating color stops (register with syntax: "<color>") or stop positions (register with syntax: "<percentage>" or syntax: "<length>").

The Three Required Descriptors

Every @property rule needs exactly three descriptors or it is invalid and silently ignored:

  • syntax: A string describing the value type. Common values include "<color>", "<angle>", "<length>", "<percentage>", "<number>", and "*" (any value, but then it cannot be animated).
  • inherits: A boolean (true or false) controlling whether the property inherits from parent elements. For animation-only properties, false is usually correct.
  • initial-value: The default value. It must match the declared syntax.

If any descriptor is missing or the initial-value does not match the syntax, the entire rule is silently dropped. No error, no warning. Check DevTools if your animation is not working; the most common cause is a mismatched type.

Reduced Motion

Animated gradients are visually striking but can be disorienting for users who have requested reduced motion. Always wrap gradient animations in a prefers-reduced-motion guard:


        @media (prefers-reduced-motion: reduce) {
            .shimmer {
                animation: none;
            }
        }
    

Guided Demo

This demo shows three gradient patterns whose density you control with a single slider. The slider adjusts background-size while the gradient definitions stay the same. Toggle the color scheme to see how the patterns adapt:

Gradient Patterns

Color scheme
20px

Diagonal Stripes

Dot Grid

Line Grid

background-size: 20px 20px;

Teaching Video Checklist

When you record your teaching video, make sure your teammates come away understanding these concepts:

  • How repeating-linear-gradient() tiles a color stop pattern and how it differs from using linear-gradient() with background-size
  • At least three distinct patterns built with gradients (stripes, dots, checkerboard, grid lines, or similar) with background-size as the primary control
  • Why plain transition on background does not animate gradient values, explained in one clear sentence
  • The @property at-rule: what its three required descriptors do and why a missing or mismatched initial-value silently breaks the rule
  • A live animation demo using @property to animate a gradient angle, color stop, or position (a hero shimmer, border glow, or similar)
  • A prefers-reduced-motion guard on the animation

Start by sweeping through one pattern's background-size in DevTools so your teammates can see the tile scaling live. Then show the @property registration in the stylesheet and edit the initial-value in DevTools to demonstrate the silent failure when the type mismatches.

Resources