Tailwind CSS Handbook v4.3

Backgrounds & Gradients

Background colours, images, sizing and repetition — plus linear, radial and conic gradients with the renamed v4 utilities.

Backgrounds cover solid fills, images, and the gradient utilities that were renamed in v4.

Background colour#

HTML
<div class="bg-white dark:bg-gray-900">…</div>
<div class="bg-indigo-600">…</div>
<div class="bg-gray-900/60">Translucent overlay</div>

See Colors for the palette and the opacity modifier.

Gradients#

Linear#

UtilityDirection
bg-linear-to-t / -tr / -r / -brUp, up-right, right, down-right
bg-linear-to-b / -bl / -l / -tlDown, down-left, left, up-left
bg-linear-45An arbitrary angle in degrees
bg-linear-[25deg,var(--color-lime-400),var(--color-teal-600)]Fully arbitrary

Stops are set with from-*, via-* and to-*:

Loading preview…

Stop positions#

HTML
<div class="bg-linear-to-r from-indigo-500 from-10% via-sky-500 via-30% to-emerald-500 to-90%">…</div>

Radial and conic#

Loading preview…
UtilityPurpose
bg-radialRadial gradient from the centre
bg-radial-[at_25%_25%]Custom origin
bg-conicConic gradient
bg-conic-180Conic starting at an angle

Interpolation#

HTML
<div class="bg-linear-to-r/oklch from-indigo-500 to-teal-400">…</div>

The /oklch or /srgb modifier controls the colour space used between stops. oklch avoids the muddy grey midpoint that sRGB produces between complementary colours.

Text gradients#

Loading preview…

bg-clip-text plus text-transparent is the whole trick.

Background images#

HTML
<div class="bg-[url('/hero.jpg')] bg-cover bg-center">…</div>
UtilityPurpose
bg-coverFill the box, cropping
bg-containFit entirely inside
bg-autoNative size
bg-center bg-top bg-left-bottomPosition
bg-no-repeat bg-repeat-x bg-repeat-roundRepetition
bg-fixed bg-local bg-scrollAttachment
bg-origin-border bg-origin-padding bg-origin-contentPositioning area
bg-clip-border bg-clip-padding bg-clip-content bg-clip-textPainting area

Layering an image and a gradient#

HTML
<div class="relative isolate overflow-hidden rounded-xl">
  <img class="absolute inset-0 -z-10 size-full object-cover" src="/hero.jpg" alt="">
  <div class="absolute inset-0 -z-10 bg-linear-to-t from-gray-900/80 to-gray-900/20"></div>
  <div class="p-8 text-white">…</div>
</div>

Layering separate elements is more flexible than stacking background-image values, and lets you animate each layer independently. Note isolate — see Overflow & Layering.

Common patterns#

Subtle section background#

HTML
<section class="bg-gray-50 dark:bg-gray-900/50">…</section>

Glassmorphism#

Loading preview…

Grid pattern background#

HTML
<div class="bg-[linear-gradient(to_right,theme(--color-gray-200)_1px,transparent_1px),linear-gradient(to_bottom,theme(--color-gray-200)_1px,transparent_1px)] bg-[size:24px_24px]">
  …
</div>

Or more readably, with a custom utility:

CSS
@utility bg-grid {
  background-image:
    linear-gradient(to right, var(--color-gray-200) 1px, transparent 1px),
    linear-gradient(to bottom, var(--color-gray-200) 1px, transparent 1px);
  background-size: 24px 24px;
}

Animated gradient border#

HTML
<div class="rounded-xl bg-linear-to-r from-indigo-500 to-fuchsia-500 p-px">
  <div class="rounded-[calc(0.75rem-1px)] bg-white p-6 dark:bg-gray-900">
    Gradient border via a 1px padded wrapper.
  </div>
</div>

Masks#

v4.1 Mask utilities fade content out using a gradient rather than a solid overlay.

UtilityPurpose
mask-t-from-* / mask-b-from-* / mask-l-from-* / mask-r-from-*Edge fades
mask-radial-from-* / mask-radial-to-*Radial masks
mask-radial-at-*Radial origin
HTML
<div class="mask-b-from-60% mask-b-to-100%">
  <img src="/photo.jpg" alt="">
</div>

That fades the bottom of the image to transparent — cleaner than overlaying a gradient in the background colour, because it works over any backdrop.

Common mistakes#

  • bg-gradient-to-r in new code. Prefer bg-linear-to-r.
  • Gradient with only from-*. You need to-* as well, or the gradient runs to transparent.
  • bg-cover without a height. A background image cannot give an element height; set h-*, aspect-* or add content.
  • bg-fixed on mobile. Support is poor on iOS and it hurts scroll performance.
  • bg-clip-text without text-transparent. The solid text colour paints over the gradient.
  • Text over an image with no scrim. Add a gradient overlay or text-shadow-* so contrast holds across the whole image.