The Wayback Machine - https://web.archive.org/web/20250108233339/https://www.geeksforgeeks.org/css-background-clip-property/
Open In App

CSS background-clip Property

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The background-clip property in CSS controls how a background (color or image) extends within an element, allowing you to dictate the reach of your background.

Syntax:

background-clip: border-box | padding-box | content-box | text | initial | inherit;

Property value: 

  • border-box: The border-box property is used to set the background color spread over the whole division.
  • padding-box: The padding-box property is used to set the background inside the border.
  • content-box: The content-box property is used to set the background color up to the content only.
  • initial: It is the default value. It is used to set the background color spread over the whole division.

Examples of CSS background-clip Property

Example: In this example, we are using the background-clip: border-box property.

html
<!DOCTYPE html>
<html>
<head>
    <title>Border Box</title>
    <style>
        .gfg {
            background-color: green;
            background-clip: border-box;
            text-align: center;
            border: 10px dashed black;
        }
    </style>
</head>

<body>
    <div class="gfg">
        <h2>
            GeeksforGeeks
        </h2>
        <p>
            background-clip: border-box;
        </p>
    </div>
</body>
</html>

Output:

Example: In this example, we are using the background-clip: padding-box property.

html
<!DOCTYPE html>
<html>
<head>
    <title>padding-box property</title>
    <style>
        .gfg {
            background-color: green;
            background-clip: padding-box;
            padding: 25px;
            border: 10px dashed black;
        }
    </style>
</head>

<body style="text-align:center">

    <div class="gfg">
        <h2>
            GeeksforGeeks
        </h2>
        <p>
            background-clip: padding-box;
        </p>
    </div>
</body>
</html>

Output:

Example: In this example, we are using the background-clip: content-box property.

html
<!DOCTYPE html>
<html>
<head>
    <title>content-box property</title>
    <style>
        .gfg {
            background-color: green;
            background-clip: content-box;
            padding: 15px;
            border: 10px dashed black;
        }
    </style>
</head>

<body style="text-align:center">

    <div class="gfg">
        <h2>
            GeeksforGeeks
        </h2>
        <p>
            background-clip: content-box;
        </p>
    </div>
</body>
</html>

Output: 

Example: In this example, we are using the background-clip: initial-box property.

html
<!DOCTYPE html>
<html>
<head>
    <title>Initial</title>
    <style>
        .gfg {
            background-color: green;
            background-clip: initial;
            padding: 15px;
            border: 10px dashed black;
        }
    </style>
</head>

<body style="text-align:center">

    <div class="gfg">
        <h2>
            GeeksforGeeks
        </h2>
        <p>
            background-clip: initial;
        </p>
    </div>
</body>
</html>

Output:

Supported Browsers:

  • Google Chrome 1.0 and above
  • Edge 12.0 and above
  • Internet Explorer 9.0 and above
  • Firefox 4.0 and above
  • Opera 10.5 and above
  • Safari 14.0 and above

CSS background-clip Property – FAQs

What is the background-clip property in CSS?

The background-clip property in CSS defines how far the background image or color extends within an element. It determines whether the background is clipped to the content box, padding box, or border box.

How can background-clip be used to create transparent borders?

By setting background-clip: padding-box; and applying a transparent border color, you can create transparent borders while keeping the background color within the padding area. This is a popular technique for creating stylish borders.

What are the differences between border-box, padding-box, and content-box in background-clip?

  • border-box: The background extends behind the border.
  • padding-box: The background is confined within the padding area, not extending into the border.
  • content-box: The background is only applied within the content area, excluding padding and border.

Can background-clip be applied to text?

Yes, using background-clip: text;, you can apply background images or gradients to text, creating visually striking designs. This is commonly combined with webkit prefixes for broader support.

Why isn’t background-clip working as expected?

Common issues include browser compatibility (especially with text clipping) and incorrect box model settings. Ensure that the element has appropriate padding, borders, and content box configurations.



Next Article

Similar Reads

three90RightbarBannerImg