CSS / Reference / background-clip: Background Painting Areas and Text Clipping

CSS background-clip controls where an element’s background color and images are painted. Use border-box to paint underneath the border, padding-box to paint through the padding, or content-box to paint only inside the content area. background-origin separately sets the positioning area for a background image.

Where the background is painted

The default is border-box and the property is not inherited. Backgrounds are painted behind the border. A solid opaque border can therefore hide the difference between border-box and padding-box.

Value Area painted
border-box Paints to the outside edge of the border. Color can show through translucent borders or gaps in a dashed border.
padding-box Paints up to the inside edge of the border, including padding but leaving the border area clear.
content-box Paints the content area only, leaving padding and borders clear.
text Clips the background to the shape of the text. Opaque text can cover the background.

The W3C CSS Backgrounds and Borders specification defines the box-based values. The CSS Backgrounds Level 4 draft also defines border-area, which follows the border’s width and style. Check support in your target browsers before using it.

Compare all three boxes with a dashed border

Save the complete example as background-clip-example.html in UTF-8 and open it in a browser. The three cards share a border, padding, and background color. Only the clipping area changes. The text at the bottom is used in the last section.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare CSS background painting areas</title>
  <style>
    body { margin: 0; color: #20242b; background: #fff; font: 1rem/1.6 system-ui, sans-serif; }
    main { max-width: 52rem; margin: auto; padding: 24px; overflow-wrap: anywhere; }
    h1 { margin: 0 0 20px; font-size: 1.4rem; }
    .samples { display: grid; gap: 18px; }
    .label { margin: 0 0 4px; font-weight: 700; }
    .card {
      box-sizing: border-box;
      width: min(100%, 304px);
      min-height: 214px;
      border: 12px dashed #718096;
      padding: 20px;
      background-color: #79c7ed;
    }
    .border { background-clip: border-box; }
    .padding { background-clip: padding-box; }
    .content { background-clip: content-box; }
    .gradient { color: #174666; font-size: 2rem; font-weight: 700; }
    @supports (background-clip: text) or (-webkit-background-clip: text) {
      .gradient {
        background-color: #174666;
        background-image: linear-gradient(90deg, #174666, #742b59);
        -webkit-background-clip: text;
        background-clip: text;
        color: transparent;
        -webkit-text-fill-color: transparent;
      }
    }
    @media (forced-colors: active) {
      .gradient { background: none; color: CanvasText; -webkit-text-fill-color: currentColor; }
    }
    @media print {
      .gradient { background: none; color: #000; -webkit-text-fill-color: currentColor; }
    }
  </style>
</head>
<body>
  <main>
    <h1>Compare CSS background painting areas</h1>
    <section class="samples">
      <div>
        <p class="label">border-box</p>
        <div class="card border">Content area</div>
      </div>
      <div>
        <p class="label">padding-box</p>
        <div class="card padding">Content area</div>
      </div>
      <div>
        <p class="label">content-box</p>
        <div class="card content">Content area</div>
      </div>
    </section>
    <p class="gradient">Readable CSS text</p>
  </main>
</body>
</html>

The first card shows blue between the dashes. The second shows a white gap between the dashes and blue padding. The third leaves the padding white and paints only the content area.

Blue paint beneath a dashed border in the border-box example and only in the content area in the content-box example

Change only background-clip on the first card in developer tools. The edge of the paint moves while width, height, padding, and border thickness stay the same. Restore border-box afterward.

Position the image with background-origin

background-origin sets the reference area for positioning and sizing the image. background-clip sets where the result is painted. Add this rule just before </style> in the example to put a small dark rectangle on the first card.

.border {
  background-image: linear-gradient(#134d63, #134d63);
  background-repeat: no-repeat;
  background-size: 48px 20px;
  background-position: left top;
  background-origin: content-box;
}

Change background-origin from content-box to padding-box. The rectangle moves 20px up and 20px left. Then set background-clip: content-box. The image position stays the same, but the rectangle in the padding is hidden.

Clip layered backgrounds independently

With multiple backgrounds, the first image is on top. In this example a white layer covers the padding-box and a gradient covers the border-box, so the lower layer shows only through the transparent border.

.frame {
  border: 4px solid transparent;
  border-radius: .75rem;
  padding: 1rem;
  background: linear-gradient(#fff, #fff) padding-box,
              linear-gradient(90deg, #0a5b72, #6e3464) border-box;
}

Add this rule to the example and insert a paragraph with class="frame" inside main. When a background layer has one box value, that value sets both positioning and clipping. With two box values, the first sets background-origin and the second sets background-clip.

The background color follows the clipping area of the bottom layer. A later background shorthand can also reset background-clip, so inspect the computed value if the clip changes unexpectedly.

Clip a gradient to readable text

The text example makes the fill transparent only inside @supports, then clips a gradient to the letters. A dark background-color remains behind the gradient so the words stay visible if the image is unavailable.

  1. Set only background-image to none in developer tools. The letters retain a solid color. Restore the gradient and check that the text is readable at its lightest point.
  2. Enable your system’s high-contrast mode or emulate forced-colors in developer tools and check for the system text color. Print preview should show black text without a background.
  3. Test on a regular element. Page-wide backgrounds on elements such as html follow different painting rules.

See the CSS backgrounds guide for combining background colors and images. Use background-origin to change the positioning area.