CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



background-clip

The background-clip property in CSS is used to specify the area of an element to which the background image or color should be clipped. It is a part of the background shorthand property and can be used to create interesting visual effects on web pages.

The background-clip property can take one of the following values:

  • border-box: The background is clipped to the border box of the element.
  • padding-box: The background is clipped to the padding box of the element.
  • content-box: The background is clipped to the content box of the element.
  • text: The background is clipped to the foreground text of the element.

Let's take a look at some code examples to see how the background-clip property works:

Example 1: border-box

In this example, we have a div element with a background image and a border. The background-clip property is set to border-box, which means that the background image is clipped to the border box of the element.

  <div style="background-image: url('image.jpg'); border: 10px solid black; background-clip: border-box;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>

Example 2: padding-box

In this example, we have a div element with a background color and some padding. The background-clip property is set to padding-box, which means that the background color is clipped to the padding box of the element.

  <div style="background-color: red; padding: 20px; background-clip: padding-box;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>

Example 3: content-box

In this example, we have a div element with a background image and some padding. The background-clip property is set to content-box, which means that the background image is clipped to the content box of the element.

  <div style="background-image: url('image.jpg'); padding: 20px; background-clip: content-box;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>

Example 4: text

In this example, we have a h1 element with a background color and some text. The background-clip property is set to text, which means that the background color is clipped to the foreground text of the element.

  <h1 style="background-color: yellow; background-clip: text;">Lorem ipsum dolor sit amet</h1>

As you can see, the background color is only visible behind the text and not in the empty space around it.

The background-clip property can be used in combination with other background properties such as background-color, background-image, and background-size to create interesting visual effects on web pages.

Conclusion

The background-clip property in CSS is a powerful tool for creating interesting visual effects on web pages. By specifying the area of an element to which the background image or color should be clipped, you can create unique and eye-catching designs that will make your website stand out from the crowd.

References

Activity