🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

CSS Backgrounds: Gradients, Layers and Patterns

Web StylesđŸŸĸ Free Lesson

CSS Backgrounds: Gradients, Layers and Patterns

Backgrounds are the cheapest surface in your design system: one property paints full sections, cards and decorative shapes without a single image request. The modern background module layers gradients, images and colors on one element, blends those layers together, and repeats tiny tiles into seamless patterns. This lesson builds the gradient syntax from first principles, then covers tiling, positioning, layering, blending and the performance habits that keep large painted surfaces off the critical path. Everything here is plain CSS with no build step, so the patterns drop into any project that can serve a stylesheet — from a marketing page to a full application shell.

Gradient syntax from scratch

A gradient is an image value that the browser rasterizes at paint time. All three shapes share a family resemblance: colors listed along a line or shape, with optional stop positions expressed in percentages or lengths.

.hero-gradient {
  min-block-size: 18rem;
  display: grid;
  place-items: center;
  color: #ffffff;
  background: linear-gradient(
    135deg,
    #1e3a8a 0%,
    #4f46e5 45%,
    #7c3aed 100%
  );
}

.soft-vignette {
  background: radial-gradient(
    circle at 50% 30%,
    #ffffff 0%,
    #e2e8f0 60%,
    #cbd5e1 100%
  );
}

.conic-swatch {
  inline-size: 8rem;
  block-size: 8rem;
  border-radius: 50%;
  background: conic-gradient(
    from 0deg,
    #ef4444,
    #f59e0b,
    #22c55e,
    #3b82f6,
    #ef4444
  );
}

.repeating-stripes {
  background: repeating-linear-gradient(
    45deg,
    transparent,
    transparent 8px,
    rgb(37 99 235 / 0.12) 8px,
    rgb(37 99 235 / 0.12) 16px
  );
}

@supports (background: linear-gradient(in oklch, red, blue)) {
  .perceptual-gradient {
    background: linear-gradient(
      in oklch longer hue,
      #3b82f6 0%,
      #ec4899 100%
    );
  }
}

Linear gradients take an angle or a keyword direction, then color stops; equal spacing needs no percentages at all, while asymmetric stops create hard edges and soft falloff. Radial gradients accept a shape (circle or ellipse), a size keyword such as farthest-corner, and a center point. Conic gradients sweep around a center like a color wheel — perfect for pie charts, conic swatches and spinner art. The in oklch color space, guarded above, produces gradients that interpolate perceptually instead of through raw sRGB, eliminating the muddy gray band that long sRGB ramps are famous for. When a gradient will carry brand weight, spend the extra minute converting the palette to a perceptual space — the difference is most visible on long, slow transitions between saturated hues.

Tiling patterns with background-size

The oldest trick in the background book still works: repeat a small tile across the surface. The pattern quality comes from matching the tile edges exactly, then choosing a background-size that divides cleanly into the element.

.dotted-canvas {
  background-color: #f8fafc;
  background-image: radial-gradient(#94a3b8 1px, transparent 1.5px);
  background-size: 18px 18px;
}

.grid-paper {
  background-image:
    linear-gradient(rgb(148 163 184 / 0.35) 1px, transparent 1px),
    linear-gradient(90deg, rgb(148 163 184 / 0.35) 1px, transparent 1px);
  background-size: 24px 24px;
  background-position: center;
}

.diagonal-tape {
  background-image: repeating-linear-gradient(
    -45deg,
    #fde047 0 12px,
    #0f172a 12px 24px
  );
}

.checkerboard {
  background-image:
    linear-gradient(45deg, #e2e8f0 25%, transparent 25%),
    linear-gradient(-45deg, #e2e8f0 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e2e8f0 75%),
    linear-gradient(-45deg, transparent 75%, #e2e8f0 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

.hero-photo {
  background-image: url('/img/hero.jpg');
  background-size: cover;
  background-position: center 30%;
  background-repeat: no-repeat;
}

The checkerboard pattern is the classic multi-offset construction: four gradient layers, each shifted by half a tile, compose into alternating squares with no image file at all. For photographic surfaces, cover plus a deliberate background-position keeps the subject framed; contain letterboxes, which is usually wrong for heroes and right for logos or product shots on constrained cards.

background-sizeBehaviourBest for
coverFills the box, crops overflowHeroes, feature banners
containFits entirely, letterboxesLogos, product imagery
autoNatural image dimensionsIcons, small decorations
100% 100%Stretches to the boxSVG art that distorts safely
Fixed px tileRepeats at exact sizePatterns, textures

Layering multiple backgrounds

One background property can stack many layers, painted in order with the first layer on top. Each layer carries its own image, size, position, repeat and attachment; the shorthand accepts them comma-separated, while longhands keep them individually addressable for overrides.

.frosted-section {
  background:
    linear-gradient(rgb(255 255 255 / 0.82), rgb(255 255 255 / 0.82)),
    url('/img/paper-texture.png') repeat,
    linear-gradient(160deg, #dbeafe, #ede9fe);
  background-size: auto, 220px 220px, auto;
  background-blend-mode: normal, multiply, normal;
  padding-block: 4rem;
}

.hero-with-overlay {
  background:
    linear-gradient(rgb(15 23 42 / 0.55), rgb(15 23 42 / 0.75)),
    url('/img/hero-team.jpg') center / cover no-repeat;
  color: #ffffff;
  min-block-size: 24rem;
  display: grid;
  place-items: center;
}

.watermark-panel {
  position: relative;
  background:
    url('/img/logo-watermark.svg') right 2rem bottom 2rem / 12rem no-repeat,
    var(--color-surface, #ffffff);
}

Layer order is the mental model to lock in: the first layer paints last on top. A dark scrim above a hero photograph is what guarantees white headline contrast regardless of the image underneath — a pattern you should reach for instead of baking text into images. When overriding in a media query, remember that changing any background longhand resets the whole stack, so mobile overrides restate the full layer list or use background-image alone.

Longhands also make progressive enhancement straightforward. Ship the gradient-only version first, then add the photographic layer inside a media query or behind an image-loading check so constrained devices skip the heavy layer entirely. Teams practicing sustainable design have gone further: an image-loading JavaScript hook that adds a class once bandwidth and data-saver preferences allow, with the photograph declared only for that class. The pattern scales from marketing heroes to product tiles, and it keeps the CSS the single source of truth for what the surface looks like in each mode.

background-blend-mode

Just as mix-blend-mode composites an element against its backdrop, background-blend-mode composites background layers against each other and against background-color. Multiply darkens a texture into a gradient; overlay adds contrast; luminosity and hue enable duotone treatments — all without opening an image editor or shipping a pre-composited asset.

.duotone-banner {
  background:
    linear-gradient(135deg, #f97316, #6366f1),
    url('/img/city-skyline.jpg') center / cover;
  background-blend-mode: color, normal;
}

.textured-card {
  background-color: #1e293b;
  background-image:
    radial-gradient(rgb(148 163 184 / 0.15) 1px, transparent 1.2px),
    linear-gradient(rgb(255 255 255 / 0.04), rgb(255 255 255 / 0.04));
  background-size: 16px 16px, auto;
  background-blend-mode: screen, normal;
  color: #f8fafc;
  border-radius: 14px;
  padding: 1.5rem;
}

@media (prefers-reduced-data: reduce) {
  .duotone-banner {
    background-image: linear-gradient(135deg, #f97316, #6366f1);
    background-blend-mode: normal;
  }
}

The prefers-reduced-data query above is an increasingly relevant courtesy: when users ask for lighter pages, dropping the photographic layer and keeping the gradient preserves the design's intent at a fraction of the bytes.

Responsive and performance-minded backgrounds

Large painted surfaces interact with performance in two ways: rasterization cost and request weight. A full-viewport gradient is essentially free — the browser composites it once and reuses it. A 4K background image decoded on a mid-range phone is not. The habits that keep backgrounds cheap: ship responsive image sets where the background is photographic using image-set() or a media query swap, cap decorative images behind content-visibility on long pages, and prefer gradients or CSS patterns for texture because they scale with zero bytes.

.responsive-hero {
  background-image: image-set(
    url('/img/hero-800.avif') type('image/avif'),
    url('/img/hero-1600.avif') type('image/avif'),
    url('/img/hero-1600.jpg') type('image/jpeg')
  );
  background-size: cover;
  background-position: center;
}

@media (max-width: 40rem) {
  .responsive-hero {
    background-image: url('/img/hero-mobile.jpg');
    background-position: 60% center;
  }
}

.fixed-backdrop {
  background: url('/img/backdrop.jpg') center / cover fixed;
}

@supports (background-attachment: fixed) {
  @media (max-width: 60rem) {
    .fixed-backdrop {
      background-attachment: scroll;
    }
  }
}

background-attachment: fixed creates a parallax illusion cheaply, but it forces expensive repaints during scroll on mobile browsers and is disabled outright on many of them — hence the scroll fallback above. image-set() lets you negotiate AVIF or WebP at multiple resolutions from one declaration, with JPEG as the universal backstop. Pair either technique with explicit background-position values so the focal point stays consistent across the different source sizes the browser may choose during negotiation.

Decorative recipes worth keeping

A handful of patterns earn permanent places in a team's stylesheet. The hero-with-scrim for photographic headers; the dotted or grid canvas for empty states and kanban boards; the striped tape for warning banners; the frosted section for layered marketing pages; the duotone banner for brand treatments. Each is a few lines, each replaces an image asset, and each remains editable at design-review time because it is CSS rather than pixels. Assemble them as tokens and utility classes early — once a team has a shared pattern library of backgrounds, design consistency improves without any extra review effort, and new pages assemble themselves from parts that are already known to render fast across the device fleet your analytics reports.

Common Mistakes

Baking headline text into hero images instead of layering a gradient scrim above the photo

Fix: Stack a translucent linear-gradient over the image layer so text contrast is controllable and localization-friendly.

Overriding one background longhand in a media query and losing the rest of the layer stack

Fix: Restate the full layer list or target background-image alone, since setting any background longhand resets the shorthand.

Using background-attachment: fixed on mobile heroes

Fix: Keep fixed attachment for desktop only and fall back to scroll below your tablet breakpoint.

Shipping a single 3MB hero background to every device

Fix: Use image-set() with AVIF sources and a mobile media query swap to serve appropriately sized files.

Assuming repeating patterns tile seamlessly without checking the edges

Fix: Design tiles so opposite edges match exactly, then verify at several background-size values.

Forgetting background-repeat on decorative tiles, so they unexpectedly repeat across cards

Fix: Set no-repeat explicitly for photographic layers and repeat only for intentional textures.

Key Takeaways

  • Linear, radial and conic gradients share one mental model: colors placed along a shape, with optional stop positions.
  • Perceptual color spaces such as oklch remove the muddy midpoints from long gradient ramps.
  • background-size and background-position turn tiny gradient tiles into dots, grids, stripes and checkerboards with zero image requests.
  • Multiple background layers paint first-on-top; combine a scrim with a photo for reliable text contrast.
  • background-blend-mode composites layers together, enabling duotones and textured surfaces entirely in CSS.
  • Keep large surfaces fast: image-set with modern formats, no fixed attachment on mobile, and gradients whenever texture will do.
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-12

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement