The background-clip Property

The background-clip property determines how a background fill or background image will be placed relative to the element: under the border, only inside the padding, or only over the content.

Features

Browser Support

All modern browsers support border-box, padding-box, and content-box. The text value requires the -webkit- prefix and is supported in Chrome, Safari, Edge.

Syntax

cssselector {
  background-clip: border-box | padding-box | content-box | text;
}

Values

Value Description
border-box The background extends to the outer edge of the border (under the border).
padding-box The background is clipped to the inner edge of the border (does not go under border).
content-box The background is displayed only over the content (padding is clipped).
text The background is clipped to the text.

Default value: border-box.

Example . border-box value

The background extends under the semi-transparent border:

html<div id="elem"></div>
css#elem {
  background-clip: border-box;
  background-color: #ffd6d6;
  background-repeat: no-repeat;
  border: 10px dashed rgba(0,0,0,0.3);
  padding: 30px;
  width: 250px;
  height: 150px;
}

Результат:

Example . padding-box value

The background does not extend under the border:

html<div id="elem"></div>
css#elem {
  background-clip: padding-box;
  background-color: #ffd6d6;
  background-repeat: no-repeat;
  border: 10px dashed rgba(0,0,0,0.5);
  padding: 30px;
  width: 250px;
  height: 150px;
}

Результат:

Example . content-box value

The background does not extend into the padding:

html<div id="elem">
  text text text text text
  text text text text text
  text text text text text
</div>
css#elem {
  background-clip: content-box;
  background-color: #ffd6d6;
  background-repeat: no-repeat;
  border: 10px dashed rgba(0,0,0,0.3);
  padding: 30px;
  width: 250px;
  height: 150px;
}

Результат:

Example . text value

The background is clipped to the text (requires -webkit-text-fill-color with the transparent value):

html<div id="elem">text text text</div>
css#elem {
  display: inline-block;
  background: linear-gradient(to right, red, blue);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  
  font-size: 40px;
  font-weight: bold;
}

Результат:

See Also

  • the background property,
    which is a shorthand property for backgrounds
  • the background-origin property,
    which defines the origin position of the background