Trong CSS, thuộc tính background-clip
kiểm soát cách background mở rộng bên trong một phần tử. Bạn có thể chỉ định phạm vi của background bằng thuộc tính này.
Cú pháp:
background-clip: border-box | padding-box | content-box | text | initial | inherit;
Giá trị thuộc tính:
- border-box:
border-box
được dùng để đặt màu background lan ra toàn bộ phần tử. - padding-box:
padding-box
được dùng để đặt background bên trong đường viền (border). - content-box:
content-box
được dùng để đặt màu background chỉ đến nội dung. - initial: Đây là giá trị mặc định. Nó được dùng để đặt background lan ra toàn bộ phần tử.
Ví dụ về thuộc tính background-clip trong CSS
Ví dụ: Trong ví dụ này, chúng ta sử dụng thuộc tính background-clip: border-box
.
<!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:
Ví dụ: Trong ví dụ này, chúng ta sử dụng thuộc tính background-clip: padding-box
.
<!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:
Ví dụ: Trong ví dụ này, chúng ta sử dụng thuộc tính background-clip: content-box
.
<!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:
Ví dụ: Trong ví dụ này, chúng ta sử dụng thuộc tính background-clip: initial-box
.
<!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:
Trình duyệt được hỗ trợ:
- Google Chrome 1.0 trở lên
- Edge 12.0 trở lên
- Internet Explorer 9.0 trở lên
- Firefox 4.0 trở lên
- Opera 10.5 trở lên
- Safari 14.0 trở lên