77 of 313 menu

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

  • When using border-box with transparent borders, the background will be visible beneath them
  • The text value requires vendor prefixes for full support
  • The property works with gradients and multiple backgrounds
  • With border-radius, the background is clipped to the rounded corners

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

selector { 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:

<div id="elem"></div> #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:

<div id="elem"></div> #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:

<div id="elem"> text text text text text text text text text text text text text text text </div> #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):

<div id="elem">text text text</div> #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
byenru