Chuyển đến nội dung

Bài 9 · Utility Basics

Backgrounds and Gradients

Use Tailwind v4 background utilities: colors with opacity, images, size and position, linear, radial and conic gradients with from, via and to stops.

Backgrounds give surfaces their identity: flat colors, tinted overlays, hero images and gradients. Tailwind v4 has a complete set for each, plus a gradient system that is more powerful than v3's. This lesson covers solid colors first, then images, then the three gradient types.

Background color

bg-{color}-{shade} sets a background from the palette. Shades run from 50 (very light) to 950 (almost black). Besides the palette there are bg-white, bg-black and bg-transparent.

Add opacity with a slash and a percentage, and it works for every color utility:

HTML
<div class="bg-black/50"></div>      <!-- 50% black -->
<div class="bg-indigo-600/20"></div>  <!-- 20% indigo -->

This is much better than using the opacity-* utility, which would fade the element's text and children too. Chapter 4 covers colors more deeply in Colors and Opacity.

Color shades and opacity

Background images

Most images in real pages are <img> tags, but decorative ones are better as backgrounds. Tailwind gives you:

  • bg-[url(/images/hero.jpg)] for an arbitrary URL (the brackets make it a one-off value),
  • bg-cover or bg-contain for sizing,
  • bg-center, bg-top, bg-left-bottom and so on for position,
  • bg-no-repeat, bg-repeat, bg-repeat-x for tiling,
  • bg-fixed for a parallax-like fixed background, bg-scroll for normal.

A typical hero:

HTML
<section class="bg-[url(/images/hero.jpg)] bg-cover bg-center bg-no-repeat"> ... </section>

To keep text readable on a photo, put a colored overlay on top. Here we use a gradient image from our own samples, with a dark overlay:

Image background with overlay

absolute inset-0 stretches the overlay to cover the section, and relative on the heading lifts it above the overlay. (Positioning is covered in the next chapter.)

Linear gradients

A gradient needs a direction and color stops. Directions use bg-linear-to-{side} (v4's new name for bg-gradient-to-{side}); stops use from-*, via-* and to-*:

HTML
<div class="bg-linear-to-r from-indigo-500 via-purple-500 to-pink-500"></div>

Sides are t, tr, r, br, b, bl, l, tl. v4 can also take an angle: bg-linear-45 from-sky-500 to-emerald-500, and negative angles with -bg-linear-45.

You can set stop positions with a percentage: from-10%, via-30%, to-90%. v4 blends the colors in the OKLAB color space by default, which avoids muddy gray middles. Switch the interpolation mode with a modifier: bg-linear-to-r/srgb for the old classic look, /oklch for more vivid hue sweeps, /hsl, or /longer to take the long way around the color wheel.

Radial and conic gradients

New in v4: bg-radial (circles of color from a center) and bg-conic (colors sweeping around a point, like a pie or color wheel):

HTML
<div class="bg-radial from-white to-slate-300"></div>
<div class="bg-radial-[at_25%_25%] from-white to-zinc-900 to-75%"></div>
<div class="bg-conic from-red-500 via-yellow-500 to-red-500"></div>
Linear, radial and conic gradients

Gradient text and tricks

A gradient is a background image, so you can use it for text by clipping the background to the text and making the fill transparent:

HTML
<h1 class="bg-linear-to-r from-sky-500 to-fuchsia-500 bg-clip-text text-5xl font-extrabold text-transparent">
  Gradient text
</h1>

bg-clip-text clips the background to the glyphs, text-transparent lets it show through. Other helpers: bg-origin-*, bg-blend-multiply and bg-clip-padding.

Gradients stack with a solid color too. A subtle top-to-bottom fade into the page color is a common way to soften the edge of an image or a scrolling area: bg-linear-to-b from-transparent to-white.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is the v4 name of the class that was bg-gradient-to-r in v3?

    v4 renamed bg-gradient-to-* to bg-linear-to-* so linear, radial and conic gradients have parallel names.
  2. 2. Which class makes a background 40% transparent without affecting the text?

    The slash opacity modifier applies only to that color; opacity-* fades the whole element.
  3. 3. What two classes do you need to show a gradient inside text?

    bg-clip-text restricts the background to the text shape and text-transparent reveals it.
  4. 4. What does bg-conic produce?

    A conic gradient rotates colors around a center like a color wheel or pie chart.

Summary

  • Solid backgrounds use bg-{color}-{shade}; use /50 style modifiers for transparency.
  • Images use bg-[url(...)] with bg-cover, bg-center and bg-no-repeat; add an overlay for text contrast.
  • v4 gradients: bg-linear-to-* or bg-linear-45, plus bg-radial and bg-conic, with from-*, via-*, to-*.
  • bg-clip-text text-transparent creates gradient text.

Next: Arbitrary Values and !important

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.